"""Enforce the Live owner chain without rewriting applied revision 0004.""" from alembic import op revision = '0005_live_owner_chain' down_revision = '0004_live_lifecycle' branch_labels = None depends_on = None def upgrade(**kwargs): op.execute(''' ALTER TABLE conversations ADD CONSTRAINT uq_conversation_owner UNIQUE (organization_id, owner_user_id, id); ALTER TABLE auth_sessions ADD CONSTRAINT uq_auth_owner UNIQUE (organization_id, user_id, id); ALTER TABLE live_sessions ADD CONSTRAINT fk_live_conversation_owner FOREIGN KEY (organization_id, owner_user_id, conversation_id) REFERENCES conversations (organization_id, owner_user_id, id) ON DELETE CASCADE; ALTER TABLE live_sessions ADD CONSTRAINT fk_live_auth_owner FOREIGN KEY (organization_id, owner_user_id, auth_session_id) REFERENCES auth_sessions (organization_id, user_id, id) ON DELETE CASCADE; ALTER TABLE tool_calls ADD COLUMN public_ref uuid; UPDATE tool_calls SET public_ref=id; ALTER TABLE tool_calls ALTER COLUMN public_ref SET NOT NULL; ALTER TABLE tool_calls ADD CONSTRAINT uq_tool_public_ref UNIQUE (public_ref); ''') def downgrade(**kwargs): op.execute(''' ALTER TABLE tool_calls DROP COLUMN public_ref; ALTER TABLE live_sessions DROP CONSTRAINT fk_live_auth_owner; ALTER TABLE live_sessions DROP CONSTRAINT fk_live_conversation_owner; ALTER TABLE auth_sessions DROP CONSTRAINT uq_auth_owner; ALTER TABLE conversations DROP CONSTRAINT uq_conversation_owner; ''')