from pathlib import Path from alembic import command from alembic.config import Config from sqlalchemy import create_engine, text from app.core.schema import check_schema_revision def test_007_preserves_legacy_run_and_adds_nullable_editor_fields(tmp_path): config = Config(str(Path(__file__).parents[1] / "alembic.ini")) path = tmp_path / "editor.sqlite" config.attributes["url"] = "sqlite+aiosqlite:///" + str(path) command.upgrade(config, "006") engine = create_engine("sqlite:///" + str(path)) try: with engine.begin() as c: c.execute( text( "INSERT INTO visual_users(id,username,display_name,password_hash,role,disabled,must_change_password,created_at) VALUES('owner','owner','Owner','unchanged','user',0,0,CURRENT_TIMESTAMP)" ) ) c.execute( text( "INSERT INTO visual_drafts(id,owner_id,name,kind,spec,revision,version,created_at,updated_at) VALUES('draft','owner','Legacy','three_shot','{}',1,1,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)" ) ) c.execute( text( "INSERT INTO visual_workflow_runs(id,owner_id,draft_id,draft_revision,spec_snapshot,spec_hash,execution_mode,state,desired_state,hold_reasons,version,created_at) VALUES('run','owner','draft',1,'{}','hash','interactive','succeeded','run','[]',1,CURRENT_TIMESTAMP)" ) ) command.upgrade(config, "007") command.upgrade(config, "head") with engine.begin() as c: check_schema_revision(c, {"009"}) assert c.execute( text("SELECT run_kind,output_goal,result_manifest FROM visual_workflow_runs") ).one() == ("legacy", "film", None) assert c.scalar(text("SELECT editor_manifest FROM visual_drafts")) is None assert c.scalar(text("SELECT password_hash FROM visual_users")) == "unchanged" finally: engine.dispose()