"""Add config_id to alert_rules table Revision ID: 010 Revises: 009 Create Date: 2025-11-19 This migration adds config_id foreign key to alert_rules table to replace the config_file column, completing the migration from file-based to database-based configurations. """ from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic revision = '010' down_revision = '009' branch_labels = None depends_on = None def upgrade(): """ Add config_id to alert_rules table and remove config_file. """ with op.batch_alter_table('alert_rules', schema=None) as batch_op: # Add config_id column with foreign key batch_op.add_column(sa.Column('config_id', sa.Integer(), nullable=True, comment='FK to scan_configs table')) batch_op.create_index('ix_alert_rules_config_id', ['config_id'], unique=False) batch_op.create_foreign_key('fk_alert_rules_config_id', 'scan_configs', ['config_id'], ['id']) # Remove the old config_file column batch_op.drop_column('config_file') print("✓ Migration complete: AlertRule now uses config_id") print(" - Added config_id foreign key to alert_rules table") print(" - Removed deprecated config_file column") def downgrade(): """Remove config_id and restore config_file on alert_rules.""" with op.batch_alter_table('alert_rules', schema=None) as batch_op: # Remove foreign key and config_id column batch_op.drop_constraint('fk_alert_rules_config_id', type_='foreignkey') batch_op.drop_index('ix_alert_rules_config_id') batch_op.drop_column('config_id') # Restore config_file column batch_op.add_column(sa.Column('config_file', sa.String(255), nullable=True, comment='Optional: specific config file this rule applies to')) print("✓ Downgrade complete: AlertRule config_id removed, config_file restored")