cmvr-visual/backend/tests/test_schema_v8.py

73 lines
2.8 KiB
Python
Raw Permalink Normal View History

"""008在独立SQLite迁移;生产校验分支不得接纳缺列的007数据库。"""
import asyncio
from pathlib import Path
import pytest
from alembic import command
from alembic.config import Config
from app.core.config import Settings
from app.core.database import Database
async def test_runtime_accepts_migrated_009_and_refuses_older_schemas(tmp_path):
path = tmp_path / "v8.sqlite3"
config = Config(str(Path(__file__).parents[1] / "alembic.ini"))
config.attributes["url"] = "sqlite+aiosqlite:///" + str(path)
await asyncio.to_thread(command.upgrade, config, "007")
settings = Settings(
mode="test",
database_url=config.attributes["url"],
workspace_root=tmp_path,
run_coordinator=False,
)
database = Database(settings)
database.mode = "production"
try:
with pytest.raises(RuntimeError, match="[Ss]chema"):
await database.initialize()
await asyncio.to_thread(command.upgrade, config, "008")
with pytest.raises(RuntimeError, match="[Ss]chema"):
await database.initialize()
await asyncio.to_thread(command.upgrade, config, "009")
await database.initialize()
finally:
await database.close()
def test_008_roundtrip_keeps_legacy_rows_and_rebuilds_metadata(tmp_path):
from sqlalchemy import create_engine, inspect, text
from sqlalchemy.pool import NullPool
from app.core.schema import schema_preflight
from app.models import Base
path = tmp_path / "roundtrip.sqlite3"
config = Config(str(Path(__file__).parents[1] / "alembic.ini"))
config.attributes["url"] = "sqlite+aiosqlite:///" + str(path)
command.upgrade(config, "007")
engine = create_engine("sqlite:///" + str(path), poolclass=NullPool)
try:
with engine.begin() as connection:
connection.execute(
text(
"INSERT INTO visual_users(id,username,display_name,password_hash,role,disabled,must_change_password,created_at) VALUES('owner','owner','Legacy','unchanged','user',0,0,CURRENT_TIMESTAMP)"
)
)
command.upgrade(config, "008")
command.downgrade(config, "007")
with engine.begin() as connection:
assert "visual_workflow_variants" not in inspect(connection).get_table_names()
assert "phase" not in {
column["name"] for column in inspect(connection).get_columns("visual_workflow_runs")
}
assert (
connection.scalar(text("SELECT password_hash FROM visual_users WHERE id='owner'"))
== "unchanged"
)
command.upgrade(config, "head")
with engine.begin() as connection:
assert schema_preflight(connection, Base.metadata).matches
finally:
engine.dispose()