44 lines
2.3 KiB
Python
44 lines
2.3 KiB
Python
|
|
"""Additive V2 schema, frozen independently of runtime application models."""
|
||
|
|
from alembic import op
|
||
|
|
import sqlalchemy as sa
|
||
|
|
import importlib.util,sys
|
||
|
|
from pathlib import Path
|
||
|
|
revision="002"
|
||
|
|
down_revision="001"
|
||
|
|
branch_labels=None
|
||
|
|
depends_on=None
|
||
|
|
|
||
|
|
def metadata():
|
||
|
|
for name,file in [("frozen_v2_models","v2_schema_models.py"),("frozen_v2_schema","v2_schema.py")]:
|
||
|
|
spec=importlib.util.spec_from_file_location(name,Path(__file__).parents[1]/file)
|
||
|
|
module=importlib.util.module_from_spec(spec);sys.modules[name]=module;spec.loader.exec_module(module)
|
||
|
|
return sys.modules["frozen_v2_models"].Base.metadata
|
||
|
|
|
||
|
|
def upgrade():
|
||
|
|
bind=op.get_bind();meta=metadata()
|
||
|
|
# All new fields have explicit server backfill defaults, including historical jobs.
|
||
|
|
defaults={"must_change_password":sa.false(),"execution_mode":"interactive","policy_version":"night-v1","explicit_attempt":"1","desired_state":"run","hold_reasons":"[]"}
|
||
|
|
inspector=sa.inspect(bind)
|
||
|
|
for name in ["visual_users","visual_jobs"]:
|
||
|
|
existing={col["name"] for col in inspector.get_columns(name)}
|
||
|
|
for column in meta.tables[name].columns:
|
||
|
|
if column.name not in existing:
|
||
|
|
copied=column._copy()
|
||
|
|
if column.name in defaults:
|
||
|
|
value=sa.text("('[]')") if column.name=="hold_reasons" and bind.dialect.name=="mysql" else defaults[column.name]
|
||
|
|
copied.server_default=sa.DefaultClause(value)
|
||
|
|
op.add_column(name,copied)
|
||
|
|
existing=set(inspector.get_table_names())
|
||
|
|
meta.create_all(bind,tables=[table for table in meta.sorted_tables if table.name not in existing],checkfirst=False)
|
||
|
|
# Preserve searchable historical JSON tags while switching pagination to SQL.
|
||
|
|
import uuid
|
||
|
|
assets=meta.tables["visual_assets"];tags=meta.tables["visual_asset_tags"]
|
||
|
|
present={(row.asset_id,row.tag) for row in bind.execute(sa.select(tags.c.asset_id,tags.c.tag))}
|
||
|
|
for row in bind.execute(sa.select(assets.c.id,assets.c.owner_id,assets.c.tags)):
|
||
|
|
for tag in row.tags or []:
|
||
|
|
if isinstance(tag,str) and (row.id,tag) not in present:
|
||
|
|
bind.execute(tags.insert().values(id=uuid.uuid4().hex,owner_id=row.owner_id,asset_id=row.id,tag=tag))
|
||
|
|
present.add((row.id,tag))
|
||
|
|
|
||
|
|
def downgrade():raise RuntimeError("Destructive downgrade is unsupported; restore a verified checkpoint")
|