Saltar a contenido

0006 - cat_tipos_canal_comercial

"""cat_tipos_canal_comercial

Revision ID: 0006
Revises: 0005
Create Date: 2026-09-20
"""
from typing import Sequence, Union

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "0006"
down_revision: Union[str, None] = "0005"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.execute(
        """
        CREATE TABLE clientes.cat_tipos_canal_comercial (
            id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
            empresa_id BIGINT NOT NULL,
            canal_id UUID NOT NULL,
            codigo VARCHAR(30) NOT NULL,
            nombre VARCHAR(100) NOT NULL,
            descripcion TEXT,
            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_tipos_canal_empresa_id UNIQUE (empresa_id, id),
            CONSTRAINT uq_tipos_canal_empresa_canal_codigo
                UNIQUE (empresa_id, canal_id, codigo),
            CONSTRAINT fk_tipos_canal_canal
                FOREIGN KEY (empresa_id, canal_id)
                REFERENCES clientes.cat_canales_comerciales (empresa_id, id)
        );

        ALTER TABLE clientes.cat_tipos_canal_comercial ENABLE ROW LEVEL SECURITY;
        CREATE POLICY rls_tipos_canal_empresa
            ON clientes.cat_tipos_canal_comercial
            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.cat_tipos_canal_comercial;")