"""fix mtgjson foreign key

Revision ID: 2025_04_14_fix_mtgjson_foreign_key
Revises: 4ad81b486caf
Create Date: 2025-04-14 00:00:00.000000

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '2025_04_14_fix_mtgjson_foreign_key'
down_revision = '4ad81b486caf'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Drop the foreign key constraint and make card_id nullable
    op.execute('ALTER TABLE mtgjson_skus DROP CONSTRAINT IF EXISTS mtgjson_skus_card_id_fkey')
    op.alter_column('mtgjson_skus', 'card_id',
                    existing_type=sa.String(),
                    nullable=True)


def downgrade() -> None:
    # Make card_id not nullable and recreate foreign key
    op.alter_column('mtgjson_skus', 'card_id',
                    existing_type=sa.String(),
                    nullable=False)
    op.create_foreign_key('mtgjson_skus_card_id_fkey',
                         'mtgjson_skus', 'mtgjson_cards',
                         ['card_id'], ['card_id']) 