"""add_campaigns_tables Revision ID: b013campaigns Revises: b012detectionassoc Create Date: 2026-02-09 18:00:00.000000 """ from typing import Sequence, Union from alembic import op import sqlalchemy as sa from sqlalchemy.dialects.postgresql import UUID, JSONB # revision identifiers, used by Alembic. revision: str = 'b013campaigns' down_revision: Union[str, Sequence[str], None] = 'b012detectionassoc' branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: """Create campaigns and campaign_tests tables.""" # campaigns op.create_table( 'campaigns', sa.Column('id', UUID(as_uuid=True), primary_key=True), sa.Column('name', sa.String(), nullable=False), sa.Column('description', sa.Text(), nullable=True), sa.Column('type', sa.String(), nullable=False, server_default='custom'), sa.Column('threat_actor_id', UUID(as_uuid=True), sa.ForeignKey('threat_actors.id', ondelete='SET NULL'), nullable=True), sa.Column('status', sa.String(), nullable=False, server_default='draft'), sa.Column('created_by', UUID(as_uuid=True), sa.ForeignKey('users.id', ondelete='SET NULL'), nullable=True), sa.Column('scheduled_at', sa.DateTime(), nullable=True), sa.Column('completed_at', sa.DateTime(), nullable=True), sa.Column('target_platform', sa.String(), nullable=True), sa.Column('tags', JSONB(), nullable=True), sa.Column('created_at', sa.DateTime(), server_default=sa.func.now()), ) op.create_index('ix_campaigns_status', 'campaigns', ['status']) op.create_index('ix_campaigns_type', 'campaigns', ['type']) op.create_index('ix_campaigns_threat_actor', 'campaigns', ['threat_actor_id']) op.create_index('ix_campaigns_created_by', 'campaigns', ['created_by']) # campaign_tests op.create_table( 'campaign_tests', sa.Column('id', UUID(as_uuid=True), primary_key=True), sa.Column('campaign_id', UUID(as_uuid=True), sa.ForeignKey('campaigns.id', ondelete='CASCADE'), nullable=False), sa.Column('test_id', UUID(as_uuid=True), sa.ForeignKey('tests.id', ondelete='CASCADE'), nullable=False), sa.Column('order_index', sa.Integer(), nullable=False, server_default='0'), sa.Column('depends_on', UUID(as_uuid=True), sa.ForeignKey('campaign_tests.id', ondelete='SET NULL'), nullable=True), sa.Column('phase', sa.String(), nullable=True), ) op.create_index('ix_campaign_tests_campaign', 'campaign_tests', ['campaign_id']) op.create_index('ix_campaign_tests_test', 'campaign_tests', ['test_id']) def downgrade() -> None: """Drop campaign_tests and campaigns tables.""" op.drop_index('ix_campaign_tests_test', table_name='campaign_tests') op.drop_index('ix_campaign_tests_campaign', table_name='campaign_tests') op.drop_table('campaign_tests') op.drop_index('ix_campaigns_created_by', table_name='campaigns') op.drop_index('ix_campaigns_threat_actor', table_name='campaigns') op.drop_index('ix_campaigns_type', table_name='campaigns') op.drop_index('ix_campaigns_status', table_name='campaigns') op.drop_table('campaigns')