"""mae_clientes
Revision ID: 0011
Revises: 0010
Create Date: 2026-09-20
"""
from typing import Sequence, Union
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "0011"
down_revision: Union[str, None] = "0010"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# No incluye todavía fk_mae_clientes_contacto_principal: esa FK se
# agrega en la migración 0014, después de crear rel_clientes_contactos,
# porque ambas tablas se referencian mutuamente.
op.execute(
"""
CREATE TABLE clientes.mae_clientes (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
empresa_id BIGINT NOT NULL,
codigo VARCHAR(30) NOT NULL,
razon_social VARCHAR(200) NOT NULL,
nombre_comercial VARCHAR(200),
tipo_identificacion_id UUID NOT NULL,
numero_documento VARCHAR(30) NOT NULL,
tipo_cliente_id UUID NOT NULL,
tipo_canal_id UUID NOT NULL,
clase_cliente_id UUID,
modelo_comercial_id UUID,
grupo_economico_id UUID,
unidad_comercial_id UUID,
termino_pago_id UUID,
contacto_principal_id UUID,
fecha_registro DATE NOT NULL,
is_activo BOOLEAN NOT NULL DEFAULT true,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
deleted_at TIMESTAMPTZ,
created_by UUID,
updated_by UUID,
CONSTRAINT uq_mae_clientes_empresa_id UNIQUE (empresa_id, id),
CONSTRAINT uq_mae_clientes_empresa_codigo UNIQUE (empresa_id, codigo),
CONSTRAINT uq_mae_clientes_empresa_documento
UNIQUE (empresa_id, tipo_identificacion_id, numero_documento),
CONSTRAINT fk_mae_clientes_tipo_cliente
FOREIGN KEY (empresa_id, tipo_cliente_id)
REFERENCES clientes.cat_tipos_clientes (empresa_id, id),
CONSTRAINT fk_mae_clientes_tipo_canal
FOREIGN KEY (empresa_id, tipo_canal_id)
REFERENCES clientes.cat_tipos_canal_comercial (empresa_id, id),
CONSTRAINT fk_mae_clientes_clase
FOREIGN KEY (empresa_id, clase_cliente_id)
REFERENCES clientes.cat_clases_cliente (empresa_id, id),
CONSTRAINT fk_mae_clientes_modelo
FOREIGN KEY (empresa_id, modelo_comercial_id)
REFERENCES clientes.cat_modelos_comerciales (empresa_id, id),
CONSTRAINT fk_mae_clientes_grupo_economico
FOREIGN KEY (empresa_id, grupo_economico_id)
REFERENCES clientes.mae_grupos_economicos (empresa_id, id),
CONSTRAINT fk_mae_clientes_unidad_comercial
FOREIGN KEY (empresa_id, unidad_comercial_id)
REFERENCES clientes.cat_unidades_comerciales (empresa_id, id),
CONSTRAINT fk_mae_clientes_termino_pago
FOREIGN KEY (empresa_id, termino_pago_id)
REFERENCES clientes.cat_terminos_pago (empresa_id, id)
);
CREATE INDEX ix_mae_clientes_empresa_tipo_canal
ON clientes.mae_clientes (empresa_id, tipo_canal_id);
CREATE INDEX ix_mae_clientes_empresa_clase
ON clientes.mae_clientes (empresa_id, clase_cliente_id);
CREATE INDEX ix_mae_clientes_empresa_grupo_economico
ON clientes.mae_clientes (empresa_id, grupo_economico_id);
CREATE INDEX ix_mae_clientes_empresa_unidad_comercial
ON clientes.mae_clientes (empresa_id, unidad_comercial_id);
CREATE INDEX ix_mae_clientes_empresa_termino_pago
ON clientes.mae_clientes (empresa_id, termino_pago_id);
ALTER TABLE clientes.mae_clientes ENABLE ROW LEVEL SECURITY;
CREATE POLICY rls_mae_clientes_empresa
ON clientes.mae_clientes
USING (empresa_id = current_setting('app.current_empresa_id', true)::BIGINT)
WITH CHECK (empresa_id = current_setting('app.current_empresa_id', true)::BIGINT);
"""
)
def downgrade() -> None:
op.execute("DROP TABLE IF EXISTS clientes.mae_clientes;")