Saltar a contenido

Especificación Técnica: cfg_variables_sistemas

Esquema: core.identidad
Base de Datos: core
Servicio: svc-identidad
Propósito: Variables de configuración del sistema por empresa, con tipo de dato tipado para su interpretación.


1. Justificación y Mejoras de Arquitectura

  • Configuración clave-valor por empresa: cada tenant define sus parámetros operativos.
  • tipo_dato permite interpretar el valor (string, int, bool, json, etc.) sin acoplar el esquema al tipo.
  • Unique (empresa_id, clave) evita claves duplicadas por contexto.
  • Índice en empresa y clave acelera la resolución de parámetros.
  • RLS por empresa actual aísla la configuración.

2. Definiciones de Implementación

Table core.identidad.cfg_variables_sistemas {
    id                         uuid          [pk, default: `gen_random_uuid()`]
    empresa_id                 bigint        [not null]
    clave                      varchar(100)  [not null]
    tipo_dato                  varchar(30)   [not null, default: `'string'`]
    valor                      text          [not null]

    // Auditoría General
    is_activo               boolean       [not null, default: `true`, note: 'Estado lógico del registro']
    created_at              timestamptz   [not null, default: `now()`, note: 'Fecha de creación']
    updated_at              timestamptz   [not null, default: `now()`, note: 'Fecha de última actualización']
    deleted_at              timestamptz   [note: 'Fecha de eliminación lógica']
    created_by              uuid          [note: 'Usuario creador']
    updated_by              uuid          [note: 'Usuario modificador']

    Indexes {
        (empresa_id, clave) [unique, name: 'uq_cfg_variables_sistemas_empresa_clave']
        clave [name: 'ix_cfg_variables_sistemas_clave']
    }

    // RLS por empresa actual
}
CREATE TABLE IF NOT EXISTS core.identidad.cfg_variables_sistemas (
    id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
    empresa_id BIGINT NOT NULL,
    clave VARCHAR(100) NOT NULL,
    tipo_dato VARCHAR(30) NOT NULL DEFAULT 'string',
    valor TEXT NOT NULL,

    -- Auditoría General
    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_cfg_variables_sistemas_empresa_clave UNIQUE (empresa_id, clave)
);
CREATE INDEX IF NOT EXISTS ix_cfg_variables_sistemas_clave ON core.identidad.cfg_variables_sistemas (clave);

-- Row-Level Security
ALTER TABLE core.identidad.cfg_variables_sistemas ENABLE ROW LEVEL SECURITY;
CREATE POLICY aislamiento_empresa ON core.identidad.cfg_variables_sistemas
    FOR ALL
    USING (empresa_id = NULLIF(current_setting('app.current_empresa_id', true), '')::BIGINT);

-- Comentarios
COMMENT ON COLUMN core.identidad.cfg_variables_sistemas.empresa_id IS 'Empresa configurada';
COMMENT ON COLUMN core.identidad.cfg_variables_sistemas.clave IS 'Identificador de la variable (ej. timezone, moneda_base)';
COMMENT ON COLUMN core.identidad.cfg_variables_sistemas.tipo_dato IS 'Tipo de dato del valor (string, int, bool, json, date)';
COMMENT ON COLUMN core.identidad.cfg_variables_sistemas.valor IS 'Valor de la variable en texto';