erp-transportistas-database-v2/ddl/README.md
Adrian Flores Cortes c93e2b1e0e feat: Initial database structure for ERP Transportistas
- PostgreSQL 15 with PostGIS extension
- 8 schemas for transport domain
- ENUMs for viaje states, unit types, events
- DDL documentation and conventions

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 09:51:31 -06:00

2.0 KiB

DDL - ERP Transportistas

Este directorio contiene los scripts DDL (Data Definition Language) para PostgreSQL.

Estructura de Archivos

ddl/
├── 01-transport-schema-ddl.sql      # OT, Embarques, Viajes, Paradas, POD
├── 02-fleet-schema-ddl.sql          # Unidades, Remolques, Operadores
├── 03-tracking-schema-ddl.sql       # Eventos GPS, Geocercas, Alertas
├── 04-fuel-schema-ddl.sql           # Combustible, Peajes, Gastos
├── 05-maintenance-schema-ddl.sql    # Mantenimiento, Ordenes de trabajo
├── 06-carriers-schema-ddl.sql       # Terceros, Documentos, Scorecard
├── 07-billing-transport-ddl.sql     # Tarifas, Facturacion, Recargos
├── 08-compliance-schema-ddl.sql     # Carta Porte, HOS, Inspecciones
└── 99-rls-transport-modules.sql     # Row Level Security

Orden de Ejecucion

  1. Ejecutar DDL de erp-core primero (schemas base)
  2. Ejecutar archivos de este directorio en orden numerico

Convencion de Nombres

  • Tablas: snake_case, plural (ej: ordenes_transporte)
  • Indices: idx_{tabla}_{columnas}
  • Foreign Keys: fk_{tabla}_{referencia}
  • Constraints: chk_{tabla}_{regla}

Multi-Tenancy

Todas las tablas incluyen:

  • tenant_id UUID NOT NULL
  • Indice compuesto con tenant_id
  • RLS policy para aislamiento

Ejemplo de Tabla

CREATE TABLE transport.ordenes_transporte (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  tenant_id UUID NOT NULL REFERENCES public.tenants(id),
  codigo VARCHAR(50) NOT NULL,
  -- ... mas columnas
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  created_by_id UUID NOT NULL,
  CONSTRAINT uq_ot_tenant_codigo UNIQUE (tenant_id, codigo)
);

CREATE INDEX idx_ot_tenant ON transport.ordenes_transporte(tenant_id);
CREATE INDEX idx_ot_estado ON transport.ordenes_transporte(tenant_id, estado);

ALTER TABLE transport.ordenes_transporte ENABLE ROW LEVEL SECURITY;

CREATE POLICY tenant_isolation ON transport.ordenes_transporte
  USING (tenant_id = current_setting('app.tenant_id')::uuid);