bifroest/app/migrations/versions/0016_chat_branches.py
2026-10-27 08:34:17 +01:00

80 lines
4.7 KiB
Python

"""Branch je Chat mit Commits; propose_change entfällt
Revision ID: 0016
Revises: 0015
Erstellt: 2026-10-22 12:53:01.361957
"""
import sqlalchemy as sa
from alembic import op
revision = '0016'
down_revision = '0015'
branch_labels = None
depends_on = None
def upgrade() -> None:
op.create_table('chat_branches',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('workspace_id', sa.Integer(), nullable=False),
sa.Column('chat_session_id', sa.Integer(), nullable=False),
sa.Column('source_id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=250), nullable=False),
sa.Column('target', sa.String(length=250), nullable=False),
sa.Column('base', sa.String(length=40), nullable=False),
sa.Column('head', sa.String(length=40), nullable=False),
sa.Column('pushed', sa.String(length=40), nullable=True),
sa.Column('pr_id', sa.String(length=40), nullable=True),
sa.Column('pr_url', sa.Text(), nullable=True),
sa.Column('pr_state', sa.String(length=20), nullable=False),
sa.Column('pr_draft', sa.Boolean(), nullable=False),
sa.Column('pr_info', sa.JSON(), nullable=False),
sa.Column('pr_checked', sa.DateTime(timezone=True), nullable=True),
sa.Column('closed', sa.DateTime(timezone=True), nullable=True),
sa.Column('created_by', sa.Integer(), nullable=True),
sa.Column('created', sa.DateTime(timezone=True), nullable=False),
sa.Column('updated', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['chat_session_id'], ['chat_sessions.id'], name=op.f('fk_chat_branches_chat_session_id_chat_sessions'), ondelete='CASCADE'),
sa.ForeignKeyConstraint(['created_by'], ['users.id'], name=op.f('fk_chat_branches_created_by_users'), ondelete='SET NULL'),
sa.ForeignKeyConstraint(['source_id'], ['data_sources.id'], name=op.f('fk_chat_branches_source_id_data_sources'), ondelete='CASCADE'),
sa.ForeignKeyConstraint(['workspace_id'], ['workspaces.id'], name=op.f('fk_chat_branches_workspace_id_workspaces'), ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id', name=op.f('pk_chat_branches'))
)
op.create_index(op.f('ix_chat_branches_chat_session_id'), 'chat_branches', ['chat_session_id'], unique=False)
op.create_index(op.f('ix_chat_branches_source_id'), 'chat_branches', ['source_id'], unique=False)
op.create_index(op.f('ix_chat_branches_workspace_id'), 'chat_branches', ['workspace_id'], unique=False)
op.create_index('uq_chat_branches_active', 'chat_branches', ['chat_session_id', 'source_id'], unique=True, postgresql_where=sa.text('closed IS NULL'))
op.create_table('branch_commits',
sa.Column('id', sa.BigInteger(), sa.Identity(always=False), nullable=False),
sa.Column('branch_id', sa.Integer(), nullable=False),
sa.Column('sha', sa.String(length=40), nullable=False),
sa.Column('parent', sa.String(length=40), nullable=False),
sa.Column('message', sa.Text(), nullable=False),
sa.Column('changes', sa.JSON(), nullable=False),
sa.Column('date', sa.String(length=40), nullable=False),
sa.Column('files', sa.JSON(), nullable=False),
sa.Column('run_id', sa.Uuid(), nullable=True),
sa.Column('created', sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['branch_id'], ['chat_branches.id'], name=op.f('fk_branch_commits_branch_id_chat_branches'), ondelete='CASCADE'),
sa.ForeignKeyConstraint(['run_id'], ['task_runs.id'], name=op.f('fk_branch_commits_run_id_task_runs'), ondelete='SET NULL'),
sa.PrimaryKeyConstraint('id', name=op.f('pk_branch_commits'))
)
op.create_index(op.f('ix_branch_commits_branch_id'), 'branch_commits', ['branch_id'], unique=False)
# Änderungen entstehen jetzt als Commits auf dem Chat-Branch (commit_change); das alte Tool samt Vorgaben entfällt.
# Offene Anträge dazu lassen sich nicht mehr ausführen und werden zurückgezogen.
op.execute("UPDATE approval_requests SET status = 'cancelled' WHERE tool = 'propose_change' AND status = 'pending'")
for table, column in (("source_tool_policies", "tool_name"), ("workspace_tool_policies", "tool_name"),
("tool_definitions", "name")):
op.execute(f"DELETE FROM {table} WHERE {column} = 'propose_change'")
def downgrade() -> None:
op.drop_index(op.f('ix_branch_commits_branch_id'), table_name='branch_commits')
op.drop_table('branch_commits')
op.drop_index('uq_chat_branches_active', table_name='chat_branches', postgresql_where=sa.text('closed IS NULL'))
op.drop_index(op.f('ix_chat_branches_workspace_id'), table_name='chat_branches')
op.drop_index(op.f('ix_chat_branches_source_id'), table_name='chat_branches')
op.drop_index(op.f('ix_chat_branches_chat_session_id'), table_name='chat_branches')
op.drop_table('chat_branches')