"""add intent_step_graphs (Спринт 7.7 — версионирование графа шагов) Revision ID: j6d8c4b56g23 Revises: i5c8b3a45f12 Create Date: 2026-04-28 16:00:00.000000 Версионирование графа шагов state machine. Один intent может иметь несколько графов; ровно один is_active=True. Существующие intent_steps остаются с graph_id=NULL после этой миграции; data migration делается в lifespan через intent_step_graph_service.ensure_seed_graphs (так чище — Alembic не лезет в бизнес-логику сидинга). """ from typing import Sequence, Union from alembic import op import sqlalchemy as sa revision: str = 'j6d8c4b56g23' down_revision: Union[str, None] = 'i5c8b3a45f12' branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None def upgrade() -> None: op.create_table( 'intent_step_graphs', sa.Column('id', sa.Integer(), nullable=False), sa.Column('intent_id', sa.Integer(), nullable=False), sa.Column('version', sa.Integer(), nullable=False), sa.Column('name', sa.String(length=200), nullable=False), sa.Column('is_active', sa.Boolean(), nullable=False), sa.Column('created_at', sa.DateTime(timezone=True), nullable=False), sa.ForeignKeyConstraint(['intent_id'], ['intents.id'], ondelete='CASCADE'), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('intent_id', 'version', name='uq_intent_step_graph_version'), ) op.create_index('ix_intent_step_graphs_intent_id', 'intent_step_graphs', ['intent_id']) # В intent_steps: добавляем graph_id (FK), снимаем старый UNIQUE (intent_id, code), # ставим новый UNIQUE (graph_id, code). У существующих записей graph_id=NULL — миграция # данных идёт в lifespan, см. intent_step_graph_service.ensure_seed_graphs. with op.batch_alter_table('intent_steps', recreate='always') as batch: batch.add_column(sa.Column('graph_id', sa.Integer(), nullable=True)) batch.create_foreign_key( 'fk_intent_steps_graph_id', 'intent_step_graphs', ['graph_id'], ['id'], ondelete='CASCADE', ) batch.drop_constraint('uq_intent_step_code', type_='unique') batch.create_unique_constraint('uq_intent_step_graph_code', ['graph_id', 'code']) def downgrade() -> None: with op.batch_alter_table('intent_steps', recreate='always') as batch: batch.drop_constraint('uq_intent_step_graph_code', type_='unique') batch.create_unique_constraint('uq_intent_step_code', ['intent_id', 'code']) batch.drop_constraint('fk_intent_steps_graph_id', type_='foreignkey') batch.drop_column('graph_id') op.drop_index('ix_intent_step_graphs_intent_id', table_name='intent_step_graphs') op.drop_table('intent_step_graphs')