|
| 1 | +import pytest |
| 2 | + |
| 3 | +from alembic_utils.pg_extension import PGExtension |
| 4 | +from alembic_utils.replaceable_entity import register_entities |
| 5 | +from alembic_utils.testbase import TEST_VERSIONS_ROOT, run_alembic_command |
| 6 | + |
| 7 | +TEST_EXT = PGExtension(schema="public", signature="uuid-ossp") |
| 8 | + |
| 9 | + |
| 10 | +def test_create_revision(engine) -> None: |
| 11 | + register_entities([TEST_EXT], entity_types=[PGExtension]) |
| 12 | + |
| 13 | + output = run_alembic_command( |
| 14 | + engine=engine, |
| 15 | + command="revision", |
| 16 | + command_kwargs={"autogenerate": True, "rev_id": "1", "message": "create"}, |
| 17 | + ) |
| 18 | + |
| 19 | + migration_create_path = TEST_VERSIONS_ROOT / "1_create.py" |
| 20 | + |
| 21 | + with migration_create_path.open() as migration_file: |
| 22 | + migration_contents = migration_file.read() |
| 23 | + |
| 24 | + assert "op.create_entity" in migration_contents |
| 25 | + assert "op.drop_entity" in migration_contents |
| 26 | + assert "op.replace_entity" not in migration_contents |
| 27 | + assert "from alembic_utils.pg_extension import PGExtension" in migration_contents |
| 28 | + |
| 29 | + # Execute upgrade |
| 30 | + run_alembic_command(engine=engine, command="upgrade", command_kwargs={"revision": "head"}) |
| 31 | + # Execute Downgrade |
| 32 | + run_alembic_command(engine=engine, command="downgrade", command_kwargs={"revision": "base"}) |
| 33 | + |
| 34 | + |
| 35 | +def test_create_or_replace_raises(): |
| 36 | + with pytest.raises(NotImplementedError): |
| 37 | + TEST_EXT.to_sql_statement_create_or_replace() |
| 38 | + |
| 39 | + |
| 40 | +def test_update_is_unreachable(engine) -> None: |
| 41 | + # Updates are not possible. The only parameter that may change is |
| 42 | + # schema, and that will result in a drop and create due to schema |
| 43 | + # scoping assumptions made for all other entities |
| 44 | + |
| 45 | + # Create the view outside of a revision |
| 46 | + engine.execute(TEST_EXT.to_sql_statement_create()) |
| 47 | + |
| 48 | + UPDATED_TEST_EXT = PGExtension("DEV", TEST_EXT.signature) |
| 49 | + |
| 50 | + register_entities([UPDATED_TEST_EXT], schemas=["public", "DEV"], entity_types=[PGExtension]) |
| 51 | + |
| 52 | + # Autogenerate a new migration |
| 53 | + # It should detect the change we made and produce a "replace_function" statement |
| 54 | + output = run_alembic_command( |
| 55 | + engine=engine, |
| 56 | + command="revision", |
| 57 | + command_kwargs={"autogenerate": True, "rev_id": "2", "message": "replace"}, |
| 58 | + ) |
| 59 | + |
| 60 | + migration_replace_path = TEST_VERSIONS_ROOT / "2_replace.py" |
| 61 | + |
| 62 | + with migration_replace_path.open() as migration_file: |
| 63 | + migration_contents = migration_file.read() |
| 64 | + |
| 65 | + assert "op.replace_entity" not in migration_contents |
| 66 | + assert "from alembic_utils.pg_extension import PGExtension" in migration_contents |
| 67 | + |
| 68 | + |
| 69 | +def test_noop_revision(engine) -> None: |
| 70 | + # Create the view outside of a revision |
| 71 | + engine.execute(TEST_EXT.to_sql_statement_create()) |
| 72 | + |
| 73 | + register_entities([TEST_EXT], entity_types=[PGExtension]) |
| 74 | + |
| 75 | + # Create a third migration without making changes. |
| 76 | + # This should result in no create, drop or replace statements |
| 77 | + run_alembic_command(engine=engine, command="upgrade", command_kwargs={"revision": "head"}) |
| 78 | + |
| 79 | + output = run_alembic_command( |
| 80 | + engine=engine, |
| 81 | + command="revision", |
| 82 | + command_kwargs={"autogenerate": True, "rev_id": "3", "message": "do_nothing"}, |
| 83 | + ) |
| 84 | + migration_do_nothing_path = TEST_VERSIONS_ROOT / "3_do_nothing.py" |
| 85 | + |
| 86 | + with migration_do_nothing_path.open() as migration_file: |
| 87 | + migration_contents = migration_file.read() |
| 88 | + |
| 89 | + assert "op.create_entity" not in migration_contents |
| 90 | + assert "op.drop_entity" not in migration_contents |
| 91 | + assert "op.replace_entity" not in migration_contents |
| 92 | + assert "from alembic_utils" not in migration_contents |
| 93 | + |
| 94 | + # Execute upgrade |
| 95 | + run_alembic_command(engine=engine, command="upgrade", command_kwargs={"revision": "head"}) |
| 96 | + # Execute Downgrade |
| 97 | + run_alembic_command(engine=engine, command="downgrade", command_kwargs={"revision": "base"}) |
| 98 | + |
| 99 | + |
| 100 | +def test_drop_revision(engine) -> None: |
| 101 | + # Register no functions locally |
| 102 | + register_entities([], entity_types=[PGExtension]) |
| 103 | + |
| 104 | + # Manually create a SQL function |
| 105 | + engine.execute(TEST_EXT.to_sql_statement_create()) |
| 106 | + |
| 107 | + output = run_alembic_command( |
| 108 | + engine=engine, |
| 109 | + command="revision", |
| 110 | + command_kwargs={"autogenerate": True, "rev_id": "1", "message": "drop"}, |
| 111 | + ) |
| 112 | + |
| 113 | + migration_create_path = TEST_VERSIONS_ROOT / "1_drop.py" |
| 114 | + |
| 115 | + with migration_create_path.open() as migration_file: |
| 116 | + migration_contents = migration_file.read() |
| 117 | + |
| 118 | + assert "op.drop_entity" in migration_contents |
| 119 | + assert "op.create_entity" in migration_contents |
| 120 | + assert "from alembic_utils" in migration_contents |
| 121 | + assert migration_contents.index("op.drop_entity") < migration_contents.index("op.create_entity") |
| 122 | + |
| 123 | + # Execute upgrade |
| 124 | + run_alembic_command(engine=engine, command="upgrade", command_kwargs={"revision": "head"}) |
| 125 | + # Execute Downgrade |
| 126 | + run_alembic_command(engine=engine, command="downgrade", command_kwargs={"revision": "base"}) |
0 commit comments