"""Rename metadata to extra_metadata

Revision ID: 002
Revises: 001
Create Date: 2025-11-27

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

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


def upgrade() -> None:
    # Check if column exists before renaming
    op.execute("""
        DO $$
        BEGIN
            IF EXISTS (
                SELECT 1 FROM information_schema.columns
                WHERE table_name='audio_tracks' AND column_name='metadata'
            ) THEN
                ALTER TABLE audio_tracks RENAME COLUMN metadata TO extra_metadata;
            END IF;
        END $$;
    """)


def downgrade() -> None:
    # Rename back to metadata
    op.execute('ALTER TABLE audio_tracks RENAME COLUMN extra_metadata TO metadata')
