# ruff: noqa
"""remove branch/tag FK constraint

Revision ID: af4c96f50d5a
Revises: 4b1a3b2c8fa5
Create Date: 2025-11-13 13:36:16.244545

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'af4c96f50d5a'
down_revision: Union[str, None] = '4b1a3b2c8fa5'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    # Use batch mode for SQLite compatibility
    # PostgreSQL autogenerates constraint names as <table>_<column>_fkey
    # SQLite needs naming convention to identify unnamed constraints

    bind = op.get_bind()
    is_sqlite = bind.dialect.name == 'sqlite'

    naming_convention = {
        "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
    }

    with op.batch_alter_table(
        'git_branches',
        schema=None,
        naming_convention=naming_convention if is_sqlite else None
    ) as batch_op:
        batch_op.create_index(batch_op.f('ix_git_branches_head_commit_sha'), ['head_commit_sha'], unique=False)
        if is_sqlite:
            batch_op.drop_constraint('fk_git_branches_head_commit_sha_git_commits', type_='foreignkey')
        else:
            batch_op.drop_constraint('git_branches_head_commit_sha_fkey', type_='foreignkey')

    with op.batch_alter_table('git_commits', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_git_commits_parent_commit_sha'), ['parent_commit_sha'], unique=False)

    with op.batch_alter_table(
        'git_tags',
        schema=None,
        naming_convention=naming_convention if is_sqlite else None
    ) as batch_op:
        if is_sqlite:
            batch_op.drop_constraint('fk_git_tags_target_commit_sha_git_commits', type_='foreignkey')
        else:
            batch_op.drop_constraint('git_tags_target_commit_sha_fkey', type_='foreignkey')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    bind = op.get_bind()
    is_sqlite = bind.dialect.name == 'sqlite'

    naming_convention = {
        "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
    }

    with op.batch_alter_table(
        'git_tags',
        schema=None,
        naming_convention=naming_convention if is_sqlite else None
    ) as batch_op:
        # PostgreSQL will autogenerate the constraint name
        constraint_name = 'fk_git_tags_target_commit_sha_git_commits' if is_sqlite else None
        batch_op.create_foreign_key(constraint_name, 'git_commits', ['target_commit_sha'], ['commit_sha'])

    with op.batch_alter_table('git_commits', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_git_commits_parent_commit_sha'))

    with op.batch_alter_table(
        'git_branches',
        schema=None,
        naming_convention=naming_convention if is_sqlite else None
    ) as batch_op:
        # PostgreSQL will autogenerate the constraint name
        constraint_name = 'fk_git_branches_head_commit_sha_git_commits' if is_sqlite else None
        batch_op.create_foreign_key(constraint_name, 'git_commits', ['head_commit_sha'], ['commit_sha'])
        batch_op.drop_index(batch_op.f('ix_git_branches_head_commit_sha'))
    # ### end Alembic commands ###
