cmvr-visual/backend/alembic/versions/006_logical_folders.py
zhanghao 88cf14d794 feat: consolidate story studio and editable automatic production
Restructure backend and frontend modules; add story, library, and editor schema migrations.

Support editable automatic production, manual confirmation modes, account and user lifecycle management, compact container scrolling, consistent forms, and per-task result previews.

Update inference and TTS scheduling, deployment guidance, and regression coverage; remove obsolete standalone pages and temporary acceptance artifacts.
2026-11-14 14:07:26 +08:00

68 lines
3.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Database-only logical folders; existing business rows and paths stay intact."""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects.mysql import DATETIME
revision = '006'
down_revision = '005'
branch_labels = None
depends_on = None
def referenced_type(bind, name):
column = next(c['type'].copy() for c in sa.inspect(bind).get_columns(name) if c['name'] == 'id')
if bind.dialect.name == 'mysql':
charset, collation = bind.execute(sa.text("SELECT CHARACTER_SET_NAME,COLLATION_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=:name AND COLUMN_NAME='id'"), {'name': name}).one()
column.charset, column.collation = charset, collation
return column
def upgrade():
bind = op.get_bind()
uid = referenced_type(bind, 'visual_users')
aid = referenced_type(bind, 'visual_assets')
# 新目录 ID 继承用户 ID 的字符集/排序规则;所有引用使用反射类型。
fid = uid.copy()
timestamp = sa.DateTime().with_variant(DATETIME(fsp=6), 'mysql')
metadata = sa.MetaData()
sa.Table('visual_users', metadata, sa.Column('id', uid.copy(), primary_key=True))
sa.Table('visual_assets', metadata, sa.Column('id', aid.copy(), primary_key=True))
folders = sa.Table('visual_folders', metadata,
sa.Column('id', fid.copy(), primary_key=True),
sa.Column('owner_id', uid.copy(), sa.ForeignKey('visual_users.id'), nullable=False, index=True),
sa.Column('name', sa.String(255), nullable=False),
sa.Column('parent_id', fid.copy(), sa.ForeignKey('visual_folders.id', ondelete='CASCADE'), nullable=True, index=True),
sa.Column('parent_key', sa.String(32), nullable=False, server_default=''),
sa.Column('created_at', timestamp, nullable=False),
sa.Column('updated_at', timestamp, nullable=False),
sa.UniqueConstraint('owner_id', 'parent_key', 'name', name='uq_visual_folders_sibling'))
links = sa.Table('visual_asset_folders', metadata,
sa.Column('asset_id', aid.copy(), sa.ForeignKey('visual_assets.id', ondelete='CASCADE'), primary_key=True),
sa.Column('folder_id', fid.copy(), sa.ForeignKey('visual_folders.id', ondelete='CASCADE'), primary_key=True, index=True))
for table in (folders, links):
table.create(bind, checkfirst=True)
existing = {i['name'] for i in sa.inspect(bind).get_indexes(table.name)}
for index in table.indexes:
if index.name not in existing:
index.create(bind, checkfirst=True)
# SQLite 用 batch 新增外键;MySQL 保留原表与历史数据,使用原生 ALTER。
for table_name in ('visual_jobs', 'visual_drafts', 'visual_workflow_runs'):
fields = ['folder_id'] + ([] if table_name == 'visual_drafts' else ['output_folder_id'])
inspector = sa.inspect(bind)
columns = {c['name'] for c in inspector.get_columns(table_name)}
fks = {tuple(f['constrained_columns']) for f in inspector.get_foreign_keys(table_name)}
indexes = {i['name'] for i in inspector.get_indexes(table_name)}
with op.batch_alter_table(table_name) as batch:
for field in fields:
if field not in columns:
batch.add_column(sa.Column(field, fid.copy(), nullable=True))
if (field,) not in fks:
batch.create_foreign_key(f'fk_{table_name}_{field}', 'visual_folders', [field], ['id'], ondelete='SET NULL')
index = f'ix_{table_name}_{field}'
if index not in indexes:
batch.create_index(index, [field])
def downgrade():
raise RuntimeError('Destructive downgrade unsupported; restore a verified joint checkpoint')