"""Add UserHomepageConfig, UserFavoriteAgent, UserDefaultAgent and Update Department Revision ID: f0f24c20e91a Revises: 60b4aee3ad2d Create Date: 2026-02-23 21:13:19.814958 """ from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. revision = 'f0f24c20e91a' down_revision = '60b4aee3ad2d' branch_labels = None depends_on = None def upgrade(): conn = op.get_bind() insp = sa.inspect(conn) tables = set(insp.get_table_names()) if 'users' in tables and 'agents' in tables: if 'user_default_agents' not in tables: op.create_table( 'user_default_agents', sa.Column('id', sa.Integer(), nullable=False), sa.Column('user_id', sa.Integer(), nullable=False), sa.Column('agent_id', sa.Integer(), nullable=False), sa.Column('sort_order', sa.Integer(), nullable=False), sa.Column('created_at', sa.DateTime(), nullable=False), sa.ForeignKeyConstraint(['agent_id'], ['agents.id']), sa.ForeignKeyConstraint(['user_id'], ['users.id']), sa.PrimaryKeyConstraint('id'), ) if 'user_favorite_agents' not in tables: op.create_table( 'user_favorite_agents', sa.Column('user_id', sa.Integer(), nullable=False), sa.Column('agent_id', sa.Integer(), nullable=False), sa.Column('created_at', sa.DateTime(), nullable=False), sa.ForeignKeyConstraint(['agent_id'], ['agents.id']), sa.ForeignKeyConstraint(['user_id'], ['users.id']), sa.PrimaryKeyConstraint('user_id', 'agent_id'), ) if 'user_homepage_configs' not in tables: op.create_table( 'user_homepage_configs', sa.Column('user_id', sa.Integer(), nullable=False), sa.Column('layout', sa.JSON(), nullable=False), sa.Column('updated_at', sa.DateTime(), nullable=False), sa.ForeignKeyConstraint(['user_id'], ['users.id']), sa.PrimaryKeyConstraint('user_id'), ) if 'departments' in tables: cols = {c['name'] for c in insp.get_columns('departments')} with op.batch_alter_table('departments', schema=None) as batch_op: if 'code' not in cols: batch_op.add_column(sa.Column('code', sa.String(length=50), nullable=True)) batch_op.create_unique_constraint(None, ['code']) if 'company' not in cols: batch_op.add_column(sa.Column('company', sa.String(length=100), nullable=True)) def downgrade(): conn = op.get_bind() insp = sa.inspect(conn) tables = set(insp.get_table_names()) if 'departments' in tables: cols = {c['name'] for c in insp.get_columns('departments')} with op.batch_alter_table('departments', schema=None) as batch_op: if 'code' in cols: try: batch_op.drop_constraint(None, type_='unique') except Exception: pass batch_op.drop_column('code') if 'company' in cols: batch_op.drop_column('company') for tbl in ('user_homepage_configs', 'user_favorite_agents', 'user_default_agents'): if tbl in tables: op.drop_table(tbl)