"""totvs_carga: fonte 'produtos' (cadastro de produtos do TOTVS)

Revision ID: c6d1a9e3b275
Revises: b5e2f8a1c746
Create Date: 2026-10-30 09:00:00.000000

Só roda nos schemas de Planejamento (ver f1a9c3d7e284).
"""
from typing import Sequence, Union

from alembic import op


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


def _no_schema_comum() -> bool:
    contexto = op.get_context()
    return contexto.dialect.name == 'postgresql' and contexto.version_table_schema is None


def upgrade() -> None:
    if _no_schema_comum():
        return
    op.drop_constraint('ck_totvs_carga_fonte', 'totvs_carga', type_='check')
    op.create_check_constraint('ck_totvs_carga_fonte', 'totvs_carga', "fonte in ('estrutura', 'ndc', 'produtos')")


def downgrade() -> None:
    if _no_schema_comum():
        return
    op.execute("delete from totvs_carga where fonte = 'produtos'")
    op.drop_constraint('ck_totvs_carga_fonte', 'totvs_carga', type_='check')
    op.create_check_constraint('ck_totvs_carga_fonte', 'totvs_carga', "fonte in ('estrutura', 'ndc')")
