"""Add private/shared publication scope and owner deletion tombstones.""" from alembic import op import sqlalchemy as sa from sqlalchemy.dialects.mysql import DATETIME revision='004' down_revision='003' branch_labels=None depends_on=None def referenced_id_type(bind, table_name): reflected=next(column['type'] for column in sa.inspect(bind).get_columns(table_name) if column['name']=='id').copy() if bind.dialect.name=='mysql': # SHOW CREATE reflection omits charset/collation inherited from table # defaults. INFORMATION_SCHEMA exposes the effective column settings. charset,collation=bind.execute(sa.text( 'SELECT CHARACTER_SET_NAME,COLLATION_NAME FROM INFORMATION_SCHEMA.COLUMNS ' "WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=:table_name AND COLUMN_NAME='id'" ),{'table_name':table_name}).one() if charset is not None: reflected.charset=charset reflected.collation=collation return reflected def upgrade(): bind=op.get_bind() timestamp=sa.DateTime().with_variant(DATETIME(fsp=6),'mysql') for name in ['visual_jobs','visual_drafts','visual_workflow_runs','visual_batches']: if 'deleted_at' not in {c['name'] for c in sa.inspect(bind).get_columns(name)}: op.add_column(name,sa.Column('deleted_at',timestamp,nullable=True)) if 'library_scope' not in {c['name'] for c in sa.inspect(bind).get_columns('visual_assets')}: op.add_column('visual_assets',sa.Column('library_scope',sa.String(16),nullable=False,server_default='private')) if 'ix_visual_assets_library_scope' not in {i['name'] for i in sa.inspect(bind).get_indexes('visual_assets')}: op.create_index('ix_visual_assets_library_scope','visual_assets',['library_scope']) # This revision is frozen and does not import evolving application models. metadata=sa.MetaData() user_id_type=referenced_id_type(bind,'visual_users') asset_id_type=referenced_id_type(bind,'visual_assets') sa.Table('visual_users',metadata,sa.Column('id',user_id_type.copy(),primary_key=True)) sa.Table('visual_assets',metadata,sa.Column('id',asset_id_type.copy(),primary_key=True)) table=sa.Table('visual_shared_assets',metadata, sa.Column('id',sa.String(32),primary_key=True), sa.Column('publisher_id',user_id_type.copy(),sa.ForeignKey('visual_users.id'),nullable=False,index=True), sa.Column('asset_id',asset_id_type.copy(),sa.ForeignKey('visual_assets.id'),nullable=False,unique=True), sa.Column('source_asset_id',asset_id_type.copy(),sa.ForeignKey('visual_assets.id'),nullable=True,index=True), sa.Column('version',sa.Integer(),nullable=False,server_default='1'), sa.Column('created_at',timestamp,nullable=False,index=True), sa.Column('withdrawn_at',timestamp,nullable=True)) 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) def downgrade(): raise RuntimeError('Destructive downgrade unsupported; restore a verified joint checkpoint')