[EPIC-002] feat: Add user_id FK to employees + rename warehouse_type
T2.1: Renamed warehouse_type -> construction_warehouse_type
in almacenes_proyecto to avoid naming conflict with ERP-Core
T2.3: Added user_id column to hr.employees
- Optional FK to auth.users (employee may not have system access)
- Unique constraint for 1:1 mapping
- Conditional FK (only created if auth.users exists)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
842ca897d5
commit
094cbe3ffb
@ -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);
|
||||
|
||||
@ -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),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user