diff --git a/schemas/02-hr-schema-ddl.sql b/schemas/02-hr-schema-ddl.sql index 2ce3137..a787aa8 100644 --- a/schemas/02-hr-schema-ddl.sql +++ b/schemas/02-hr-schema-ddl.sql @@ -59,9 +59,12 @@ CREATE TABLE IF NOT EXISTS hr.employees ( created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), created_by UUID REFERENCES auth.users(id), + -- Mapeo a usuario del sistema (opcional - empleado puede no tener acceso) + user_id UUID, CONSTRAINT uq_employees_codigo UNIQUE (tenant_id, codigo), - CONSTRAINT uq_employees_curp UNIQUE (tenant_id, curp) + CONSTRAINT uq_employees_curp UNIQUE (tenant_id, curp), + CONSTRAINT uq_employees_user_id UNIQUE (user_id) ); -- Tabla: Puestos @@ -85,6 +88,20 @@ ALTER TABLE hr.employees ADD CONSTRAINT fk_employees_puesto FOREIGN KEY (puesto_id) REFERENCES hr.puestos(id); +-- FK condicional: employees.user_id → auth.users (si tabla existe) +DO $$ +BEGIN + IF EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'auth' AND tablename = 'users') THEN + IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'fk_employees_user') THEN + ALTER TABLE hr.employees + ADD CONSTRAINT fk_employees_user + FOREIGN KEY (user_id) REFERENCES auth.users(id) + ON DELETE SET NULL; + RAISE NOTICE 'FK creada: hr.employees.user_id → auth.users'; + END IF; + END IF; +END $$; + -- Tabla: Asignacion de empleados a obras CREATE TABLE IF NOT EXISTS hr.employee_fraccionamientos ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), @@ -107,6 +124,7 @@ CREATE TABLE IF NOT EXISTS hr.employee_fraccionamientos ( CREATE INDEX IF NOT EXISTS idx_employees_tenant ON hr.employees(tenant_id); CREATE INDEX IF NOT EXISTS idx_employees_estado ON hr.employees(estado); CREATE INDEX IF NOT EXISTS idx_employees_puesto ON hr.employees(puesto_id); +CREATE INDEX IF NOT EXISTS idx_employees_user_id ON hr.employees(user_id); CREATE INDEX IF NOT EXISTS idx_puestos_tenant ON hr.puestos(tenant_id); CREATE INDEX IF NOT EXISTS idx_employee_fraccionamientos_employee ON hr.employee_fraccionamientos(employee_id); CREATE INDEX IF NOT EXISTS idx_employee_fraccionamientos_fraccionamiento ON hr.employee_fraccionamientos(fraccionamiento_id); diff --git a/schemas/06-inventory-ext-schema-ddl.sql b/schemas/06-inventory-ext-schema-ddl.sql index 0ebeb4b..c152bea 100644 --- a/schemas/06-inventory-ext-schema-ddl.sql +++ b/schemas/06-inventory-ext-schema-ddl.sql @@ -55,7 +55,7 @@ CREATE TABLE IF NOT EXISTS inventory.almacenes_proyecto ( tenant_id UUID NOT NULL REFERENCES auth.tenants(id) ON DELETE CASCADE, warehouse_id UUID NOT NULL, -- FK a inventory.warehouses (ERP Core) fraccionamiento_id UUID NOT NULL REFERENCES construction.fraccionamientos(id), - warehouse_type inventory.warehouse_type_construction NOT NULL DEFAULT 'obra', + construction_warehouse_type inventory.warehouse_type_construction NOT NULL DEFAULT 'obra', location_description TEXT, location GEOMETRY(POINT, 4326), responsible_id UUID REFERENCES auth.users(id),