|
| 1 | +from datetime import datetime |
| 2 | +from importlib.util import module_from_spec, spec_from_file_location |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import sqlalchemy as sa |
| 6 | +from alembic.migration import MigrationContext |
| 7 | +from alembic.operations import Operations |
| 8 | + |
| 9 | +MIGRATION_PATH = ( |
| 10 | + Path(__file__).resolve().parents[2] |
| 11 | + / 'migrations' |
| 12 | + / 'versions' |
| 13 | + / '138_backfill_initial_superadmin.py' |
| 14 | +) |
| 15 | +spec = spec_from_file_location('migration_138', MIGRATION_PATH) |
| 16 | +assert spec is not None and spec.loader is not None |
| 17 | +migration_138 = module_from_spec(spec) |
| 18 | +spec.loader.exec_module(migration_138) |
| 19 | + |
| 20 | + |
| 21 | +def _run_upgrade(connection: sa.Connection) -> None: |
| 22 | + context = MigrationContext.configure(connection) |
| 23 | + with Operations.context(context): |
| 24 | + migration_138.upgrade() |
| 25 | + |
| 26 | + |
| 27 | +def _database() -> tuple[sa.Engine, sa.Table, sa.Table]: |
| 28 | + engine = sa.create_engine('sqlite://') |
| 29 | + metadata = sa.MetaData() |
| 30 | + role = sa.Table( |
| 31 | + 'role', |
| 32 | + metadata, |
| 33 | + sa.Column('id', sa.Integer(), primary_key=True), |
| 34 | + sa.Column('name', sa.String(), nullable=False, unique=True), |
| 35 | + ) |
| 36 | + user = sa.Table( |
| 37 | + 'user', |
| 38 | + metadata, |
| 39 | + sa.Column('id', sa.String(), primary_key=True), |
| 40 | + sa.Column('role_id', sa.Integer(), sa.ForeignKey('role.id')), |
| 41 | + sa.Column('accepted_tos', sa.DateTime()), |
| 42 | + sa.Column('first_login_at', sa.DateTime()), |
| 43 | + ) |
| 44 | + metadata.create_all(engine) |
| 45 | + return engine, role, user |
| 46 | + |
| 47 | + |
| 48 | +def test_upgrade_promotes_oldest_user_when_no_superadmin_exists(): |
| 49 | + engine, role, user = _database() |
| 50 | + |
| 51 | + with engine.begin() as connection: |
| 52 | + connection.execute( |
| 53 | + role.insert(), |
| 54 | + [{'id': 1, 'name': 'admin'}, {'id': 2, 'name': 'user'}], |
| 55 | + ) |
| 56 | + connection.execute( |
| 57 | + user.insert(), |
| 58 | + [ |
| 59 | + { |
| 60 | + 'id': 'newer', |
| 61 | + 'role_id': 2, |
| 62 | + 'accepted_tos': datetime(2025, 2, 1), |
| 63 | + }, |
| 64 | + { |
| 65 | + 'id': 'oldest', |
| 66 | + 'role_id': None, |
| 67 | + 'accepted_tos': datetime(2025, 1, 1), |
| 68 | + }, |
| 69 | + ], |
| 70 | + ) |
| 71 | + |
| 72 | + _run_upgrade(connection) |
| 73 | + roles = dict( |
| 74 | + connection.execute(sa.select(user.c.id, user.c.role_id)).tuples().all() |
| 75 | + ) |
| 76 | + |
| 77 | + assert roles == {'newer': 2, 'oldest': 1} |
| 78 | + |
| 79 | + |
| 80 | +def test_upgrade_is_idempotent_when_superadmin_exists(): |
| 81 | + engine, role, user = _database() |
| 82 | + |
| 83 | + with engine.begin() as connection: |
| 84 | + connection.execute( |
| 85 | + role.insert(), |
| 86 | + [{'id': 1, 'name': 'admin'}, {'id': 2, 'name': 'user'}], |
| 87 | + ) |
| 88 | + connection.execute( |
| 89 | + user.insert(), |
| 90 | + [ |
| 91 | + {'id': 'admin', 'role_id': 1}, |
| 92 | + { |
| 93 | + 'id': 'older-non-admin', |
| 94 | + 'role_id': 2, |
| 95 | + 'accepted_tos': datetime(2024, 1, 1), |
| 96 | + }, |
| 97 | + ], |
| 98 | + ) |
| 99 | + |
| 100 | + _run_upgrade(connection) |
| 101 | + _run_upgrade(connection) |
| 102 | + roles = dict( |
| 103 | + connection.execute(sa.select(user.c.id, user.c.role_id)).tuples().all() |
| 104 | + ) |
| 105 | + |
| 106 | + assert roles == {'admin': 1, 'older-non-admin': 2} |
| 107 | + |
| 108 | + |
| 109 | +def test_upgrade_noops_when_there_are_no_users(): |
| 110 | + engine, role, user = _database() |
| 111 | + |
| 112 | + with engine.begin() as connection: |
| 113 | + connection.execute(role.insert(), [{'id': 1, 'name': 'admin'}]) |
| 114 | + _run_upgrade(connection) |
| 115 | + users = connection.execute(sa.select(user)).all() |
| 116 | + |
| 117 | + assert users == [] |
0 commit comments