"""Alter Appellation Table

Revision ID: 0f3a4026c398
Revises: 913ee7a57f67
Create Date: 2025-08-22 14:02:16.747673

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = '0f3a4026c398'
down_revision: Union[str, None] = '913ee7a57f67'
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! ###
    op.alter_column('appellations', 'sub_region_id',
               existing_type=sa.INTEGER(),
               nullable=True)
    op.alter_column('appellations', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.alter_column('appellations', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.alter_column('countries', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.alter_column('countries', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.alter_column('regions', 'region_code',
               existing_type=sa.VARCHAR(length=255),
               nullable=False)
    op.alter_column('regions', 'country_id',
               existing_type=sa.INTEGER(),
               nullable=True)
    op.alter_column('regions', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.alter_column('regions', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.alter_column('sub_appellations', 'appellation_id',
               existing_type=sa.INTEGER(),
               nullable=True)
    op.alter_column('sub_appellations', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.alter_column('sub_appellations', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.alter_column('sub_regions', 'region_id',
               existing_type=sa.INTEGER(),
               nullable=True)
    op.alter_column('sub_regions', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.alter_column('sub_regions', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.add_column('wine_appellations', sa.Column('import_tag', sa.String(length=255), nullable=True))
    op.alter_column('wine_appellations', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.alter_column('wine_appellations', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
    op.drop_index('ix_wine_appellations_title', table_name='wine_appellations')
    op.drop_constraint('uq_wa_title_appellation', 'wine_appellations', type_='unique')
    op.drop_constraint('uq_wa_title_country', 'wine_appellations', type_='unique')
    op.drop_constraint('uq_wa_title_region', 'wine_appellations', type_='unique')
    op.drop_constraint('uq_wa_title_sub_appellation', 'wine_appellations', type_='unique')
    op.drop_constraint('uq_wa_title_sub_region', 'wine_appellations', type_='unique')
    op.drop_column('wine_appellations', 'title')
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('wine_appellations', sa.Column('title', sa.VARCHAR(length=255), autoincrement=False, nullable=False))
    op.create_unique_constraint('uq_wa_title_sub_region', 'wine_appellations', ['title', 'sub_region_id'])
    op.create_unique_constraint('uq_wa_title_sub_appellation', 'wine_appellations', ['title', 'sub_appellation_id'])
    op.create_unique_constraint('uq_wa_title_region', 'wine_appellations', ['title', 'region_id'])
    op.create_unique_constraint('uq_wa_title_country', 'wine_appellations', ['title', 'country_id'])
    op.create_unique_constraint('uq_wa_title_appellation', 'wine_appellations', ['title', 'appellation_id'])
    op.create_index('ix_wine_appellations_title', 'wine_appellations', ['title'], unique=False)
    op.alter_column('wine_appellations', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    op.alter_column('wine_appellations', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    op.drop_column('wine_appellations', 'import_tag')
    op.alter_column('sub_regions', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    op.alter_column('sub_regions', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    op.alter_column('sub_regions', 'region_id',
               existing_type=sa.INTEGER(),
               nullable=False)
    op.alter_column('sub_appellations', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    op.alter_column('sub_appellations', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    op.alter_column('sub_appellations', 'appellation_id',
               existing_type=sa.INTEGER(),
               nullable=False)
    op.alter_column('regions', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    op.alter_column('regions', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    op.alter_column('regions', 'country_id',
               existing_type=sa.INTEGER(),
               nullable=False)
    op.alter_column('regions', 'region_code',
               existing_type=sa.VARCHAR(length=255),
               nullable=True)
    op.alter_column('countries', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    op.alter_column('countries', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    op.alter_column('appellations', 'updated_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    op.alter_column('appellations', 'created_at',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
    op.alter_column('appellations', 'sub_region_id',
               existing_type=sa.INTEGER(),
               nullable=False)
    # ### end Alembic commands ###
