from pathlib import Path from alembic import command from alembic.config import Config from sqlalchemy import create_engine, text, inspect def test_v5_is_additive_and_matches_runtime_metadata(tmp_path): config = Config(str(Path(__file__).parents[1] / "alembic.ini")) path = tmp_path / "story-migration.sqlite" config.attributes["url"] = "sqlite+aiosqlite:///" + str(path) command.upgrade(config, "004") engine = create_engine("sqlite:///" + str(path)) with engine.begin() as connection: before = { t: inspect(connection).get_columns(t) for t in inspect(connection).get_table_names() if t != "alembic_version" } command.upgrade(config, "005") command.upgrade(config, "005") with engine.begin() as connection: assert connection.scalar(text("SELECT version_num FROM alembic_version")) == "005" assert set(inspect(connection).get_table_names()) == set(before) | { "alembic_version", "visual_shot_candidates", "visual_shot_selections", } for name, columns in before.items(): assert [(c["name"], str(c["type"]), c["nullable"]) for c in columns] == [ (c["name"], str(c["type"]), c["nullable"]) for c in inspect(connection).get_columns(name) ] from app.core.schema import check_schema_revision # 历史升级断言仍验证 005;运行时 metadata 则在新增 006 后校验。 command.upgrade(config, "head") with engine.begin() as connection: check_schema_revision(connection, {"008"})