68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
from importlib.util import module_from_spec, spec_from_file_location
|
|
from pathlib import Path
|
|
|
|
|
|
MIGRATION_PATH = Path(__file__).parents[1] / "alembic" / "versions" / "0009_remove_product_domain.py"
|
|
SPEC = spec_from_file_location("remove_product_domain_migration", MIGRATION_PATH)
|
|
assert SPEC and SPEC.loader
|
|
MIGRATION = module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(MIGRATION)
|
|
|
|
|
|
class LegacySchemaInspector:
|
|
def get_table_names(self) -> list[str]:
|
|
return ["Product", "MiniProgramFavorite", "MiniProgramHistory"]
|
|
|
|
def get_foreign_keys(self, table_name: str) -> list[dict[str, object]]:
|
|
foreign_keys = {
|
|
"MiniProgramFavorite": [
|
|
{
|
|
"name": "MiniProgramFavorite_productId_fkey",
|
|
"constrained_columns": ["productId"],
|
|
"referred_table": "Product",
|
|
"referred_columns": ["id"],
|
|
}
|
|
],
|
|
"MiniProgramHistory": [
|
|
{
|
|
"name": "MiniProgramHistory_productId_fkey",
|
|
"constrained_columns": ["productId"],
|
|
"referred_table": "Product",
|
|
"referred_columns": ["id"],
|
|
}
|
|
],
|
|
}
|
|
return foreign_keys.get(table_name, [])
|
|
|
|
|
|
def test_upgrade_drops_legacy_product_foreign_keys_before_product_table(monkeypatch):
|
|
events: list[tuple[str, ...]] = []
|
|
inspector = LegacySchemaInspector()
|
|
|
|
monkeypatch.setattr(MIGRATION, "inspect", lambda _bind: inspector)
|
|
monkeypatch.setattr(MIGRATION.op, "get_bind", lambda: object())
|
|
monkeypatch.setattr(
|
|
MIGRATION.op,
|
|
"drop_constraint",
|
|
lambda name, table_name, type_: events.append(("drop_constraint", name, table_name, type_)),
|
|
)
|
|
monkeypatch.setattr(MIGRATION.op, "drop_table", lambda table_name: events.append(("drop_table", table_name)))
|
|
|
|
MIGRATION.upgrade()
|
|
|
|
assert events == [
|
|
(
|
|
"drop_constraint",
|
|
"MiniProgramFavorite_productId_fkey",
|
|
"MiniProgramFavorite",
|
|
"foreignkey",
|
|
),
|
|
(
|
|
"drop_constraint",
|
|
"MiniProgramHistory_productId_fkey",
|
|
"MiniProgramHistory",
|
|
"foreignkey",
|
|
),
|
|
("drop_table", "Product"),
|
|
]
|