"""Add git_fingerprint to managed_repos

Stores the root commit SHA-1 of the git repository — a machine-independent
identifier that is identical across every clone regardless of remote URL,
checkout path, or protocol.  Used by the consistency checker and other tools
to match a locally checked-out repo to its state-hub record without relying
on local_path or remote_url (both of which vary per machine).

Revision ID: n1i2j3k4l5m6
Revises: m0h1i2j3k4l5
Create Date: 2026-03-28
"""
from alembic import op
import sqlalchemy as sa

revision = 'n1i2j3k4l5m6'
down_revision = 'm0h1i2j3k4l5'
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.add_column(
        'managed_repos',
        sa.Column('git_fingerprint', sa.String(40), nullable=True),
    )
    # Non-unique index: repos forked from the same ancestor share a root commit
    # SHA-1.  The index speeds up lookup; disambiguate by remote_url when needed.
    op.create_index(
        'ix_managed_repos_git_fingerprint',
        'managed_repos',
        ['git_fingerprint'],
        unique=False,
    )


def downgrade() -> None:
    op.drop_index('ix_managed_repos_git_fingerprint', table_name='managed_repos')
    op.drop_column('managed_repos', 'git_fingerprint')
