"""sync_model_drift

Revision ID: a7aa29cf8a20
Revises: a1b2c3d4e5f9
Create Date: 2026-05-22 01:08:44.078222

"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'a7aa29cf8a20'
down_revision = 'a1b2c3d4e5f9'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(op.f('fk_activity_events_board_id_boards'), 'activity_events', type_='foreignkey')
    op.create_foreign_key(None, 'activity_events', 'boards', ['board_id'], ['id'])
    op.alter_column('agents', 'gateway_id',
               existing_type=sa.UUID(),
               nullable=False)
    op.drop_index(op.f('ix_agents_openclaw_session_id'), table_name='agents', postgresql_where='(openclaw_session_id IS NOT NULL)')
    op.create_index(op.f('ix_agents_openclaw_session_id'), 'agents', ['openclaw_session_id'], unique=False)
    op.alter_column('approvals', 'confidence',
               existing_type=sa.INTEGER(),
               type_=sa.Float(),
               existing_nullable=False)
    op.drop_index(op.f('ix_board_repository_links_org_id'), table_name='board_repository_links')
    op.create_index(op.f('ix_board_repository_links_organization_id'), 'board_repository_links', ['organization_id'], unique=False)
    op.drop_constraint(op.f('board_task_custom_fields_organization_id_fkey'), 'board_task_custom_fields', type_='foreignkey')
    op.drop_column('board_task_custom_fields', 'organization_id')
    op.alter_column('boards', 'description',
               existing_type=sa.VARCHAR(),
               nullable=False)
    op.drop_index(op.f('ix_forgejo_connections_org_id'), table_name='forgejo_connections')
    op.create_index(op.f('ix_forgejo_connections_organization_id'), 'forgejo_connections', ['organization_id'], unique=False)
    op.drop_index(op.f('ix_forgejo_issues_org_id'), table_name='forgejo_issues')
    op.drop_index(op.f('ix_forgejo_issues_repo_id'), table_name='forgejo_issues')
    op.create_index(op.f('ix_forgejo_issues_forgejo_issue_number'), 'forgejo_issues', ['forgejo_issue_number'], unique=False)
    op.create_index(op.f('ix_forgejo_issues_organization_id'), 'forgejo_issues', ['organization_id'], unique=False)
    op.create_index(op.f('ix_forgejo_issues_repository_id'), 'forgejo_issues', ['repository_id'], unique=False)
    op.drop_index(op.f('ix_forgejo_repos_conn_id'), table_name='forgejo_repositories')
    op.drop_index(op.f('ix_forgejo_repos_org_id'), table_name='forgejo_repositories')
    op.create_index(op.f('ix_forgejo_repositories_connection_id'), 'forgejo_repositories', ['connection_id'], unique=False)
    op.create_index(op.f('ix_forgejo_repositories_organization_id'), 'forgejo_repositories', ['organization_id'], unique=False)
    op.drop_constraint(op.f('uq_organizations_name'), 'organizations', type_='unique')
    op.drop_index(op.f('ix_provider_credentials_org_id'), table_name='provider_credentials')
    op.drop_constraint(op.f('uq_provider_credentials_org_provider_key'), 'provider_credentials', type_='unique')
    op.create_index(op.f('ix_provider_credentials_account_key'), 'provider_credentials', ['account_key'], unique=False)
    op.create_index(op.f('ix_provider_credentials_organization_id'), 'provider_credentials', ['organization_id'], unique=False)
    op.drop_constraint(op.f('task_custom_field_values_organization_id_fkey'), 'task_custom_field_values', type_='foreignkey')
    op.drop_column('task_custom_field_values', 'organization_id')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('task_custom_field_values', sa.Column('organization_id', sa.UUID(), autoincrement=False, nullable=False))
    op.create_foreign_key(op.f('task_custom_field_values_organization_id_fkey'), 'task_custom_field_values', 'organizations', ['organization_id'], ['id'])
    op.drop_index(op.f('ix_provider_credentials_organization_id'), table_name='provider_credentials')
    op.drop_index(op.f('ix_provider_credentials_account_key'), table_name='provider_credentials')
    op.create_unique_constraint(op.f('uq_provider_credentials_org_provider_key'), 'provider_credentials', ['organization_id', 'provider', 'account_key'], postgresql_nulls_not_distinct=False)
    op.create_index(op.f('ix_provider_credentials_org_id'), 'provider_credentials', ['organization_id'], unique=False)
    op.create_unique_constraint(op.f('uq_organizations_name'), 'organizations', ['name'], postgresql_nulls_not_distinct=False)
    op.drop_index(op.f('ix_forgejo_repositories_organization_id'), table_name='forgejo_repositories')
    op.drop_index(op.f('ix_forgejo_repositories_connection_id'), table_name='forgejo_repositories')
    op.create_index(op.f('ix_forgejo_repos_org_id'), 'forgejo_repositories', ['organization_id'], unique=False)
    op.create_index(op.f('ix_forgejo_repos_conn_id'), 'forgejo_repositories', ['connection_id'], unique=False)
    op.drop_index(op.f('ix_forgejo_issues_repository_id'), table_name='forgejo_issues')
    op.drop_index(op.f('ix_forgejo_issues_organization_id'), table_name='forgejo_issues')
    op.drop_index(op.f('ix_forgejo_issues_forgejo_issue_number'), table_name='forgejo_issues')
    op.create_index(op.f('ix_forgejo_issues_repo_id'), 'forgejo_issues', ['repository_id'], unique=False)
    op.create_index(op.f('ix_forgejo_issues_org_id'), 'forgejo_issues', ['organization_id'], unique=False)
    op.drop_index(op.f('ix_forgejo_connections_organization_id'), table_name='forgejo_connections')
    op.create_index(op.f('ix_forgejo_connections_org_id'), 'forgejo_connections', ['organization_id'], unique=False)
    op.alter_column('boards', 'description',
               existing_type=sa.VARCHAR(),
               nullable=True)
    op.add_column('board_task_custom_fields', sa.Column('organization_id', sa.UUID(), autoincrement=False, nullable=False))
    op.create_foreign_key(op.f('board_task_custom_fields_organization_id_fkey'), 'board_task_custom_fields', 'organizations', ['organization_id'], ['id'])
    op.drop_index(op.f('ix_board_repository_links_organization_id'), table_name='board_repository_links')
    op.create_index(op.f('ix_board_repository_links_org_id'), 'board_repository_links', ['organization_id'], unique=False)
    op.alter_column('approvals', 'confidence',
               existing_type=sa.Float(),
               type_=sa.INTEGER(),
               existing_nullable=False)
    op.drop_index(op.f('ix_agents_openclaw_session_id'), table_name='agents')
    op.create_index(op.f('ix_agents_openclaw_session_id'), 'agents', ['openclaw_session_id'], unique=True, postgresql_where='(openclaw_session_id IS NOT NULL)')
    op.alter_column('agents', 'gateway_id',
               existing_type=sa.UUID(),
               nullable=True)
    op.drop_constraint(None, 'activity_events', type_='foreignkey')
    op.create_foreign_key(op.f('fk_activity_events_board_id_boards'), 'activity_events', 'boards', ['board_id'], ['id'], ondelete='CASCADE')
    # ### end Alembic commands ###
