[ST-P0-003] docs: Add DDL execution order and update README
- Create DDL-EXECUTION-ORDER.md with schema dependencies - Document execution order for erp-core + erp-construccion - Add WSL commands for database recreation - Update README.md with current schema counts (10 schemas, 143 tables, 115 ENUMs) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
f76afed513
commit
6b02d8ab08
278
DDL-EXECUTION-ORDER.md
Normal file
278
DDL-EXECUTION-ORDER.md
Normal file
@ -0,0 +1,278 @@
|
|||||||
|
# DDL Execution Order - ERP Construccion
|
||||||
|
|
||||||
|
**Version:** 1.0.0
|
||||||
|
**Fecha:** 2026-02-03
|
||||||
|
**Proyecto:** erp-construccion
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Descripcion
|
||||||
|
|
||||||
|
Este documento define el orden correcto de ejecucion de archivos DDL para crear una base de datos limpia de ERP Construccion. Es fundamental seguir este orden debido a las dependencias entre schemas y tablas.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Dependencias de Schema
|
||||||
|
|
||||||
|
### Diagrama de Dependencias
|
||||||
|
|
||||||
|
```
|
||||||
|
erp-core (auth, core, etc.)
|
||||||
|
|
|
||||||
|
+-- auth.tenants (REQUERIDO)
|
||||||
|
+-- auth.users (REQUERIDO)
|
||||||
|
|
|
||||||
|
v
|
||||||
|
erp-construccion
|
||||||
|
|
|
||||||
|
+-- init-scripts/01-init-database.sql
|
||||||
|
| |
|
||||||
|
| +-- core_shared (funciones)
|
||||||
|
| +-- core.tenants (fallback si erp-core no instalado)
|
||||||
|
| +-- core.users (fallback si erp-core no instalado)
|
||||||
|
| +-- Schemas: construction, estimates, infonavit, hse, hr, inventory, purchase
|
||||||
|
|
|
||||||
|
+-- schemas/01-construction-schema-ddl.sql
|
||||||
|
| |
|
||||||
|
| +-- VERIFICA: auth.tenants, auth.users
|
||||||
|
| +-- CREA: Schema construction (24 tablas)
|
||||||
|
|
|
||||||
|
+-- schemas/02-hr-schema-ddl.sql
|
||||||
|
+-- schemas/03-hse-schema-ddl.sql
|
||||||
|
+-- schemas/04-estimates-schema-ddl.sql
|
||||||
|
+-- schemas/05-infonavit-schema-ddl.sql
|
||||||
|
+-- schemas/06-inventory-ext-schema-ddl.sql
|
||||||
|
+-- schemas/07-purchase-ext-schema-ddl.sql
|
||||||
|
+-- schemas/08-finance-schema-ddl.sql
|
||||||
|
+-- schemas/09-assets-schema-ddl.sql
|
||||||
|
+-- schemas/10-documents-schema-ddl.sql
|
||||||
|
+-- schemas/99-rls-policies.sql
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Orden de Ejecucion
|
||||||
|
|
||||||
|
### Escenario A: Base de Datos Nueva (sin erp-core)
|
||||||
|
|
||||||
|
Cuando se crea una BD limpia SIN erp-core instalado previamente:
|
||||||
|
|
||||||
|
| Paso | Archivo | Descripcion |
|
||||||
|
|------|---------|-------------|
|
||||||
|
| 1 | `init-scripts/01-init-database.sql` | Extensiones, core_shared, schemas base, tablas fallback |
|
||||||
|
| 2 | `schemas/01-construction-schema-ddl.sql` | Schema construction (24 tablas, 7 ENUMs) |
|
||||||
|
| 3 | `schemas/02-hr-schema-ddl.sql` | Schema hr extension |
|
||||||
|
| 4 | `schemas/03-hse-schema-ddl.sql` | Schema hse (58 tablas, 67 ENUMs) |
|
||||||
|
| 5 | `schemas/04-estimates-schema-ddl.sql` | Schema estimates (8 tablas, 4 ENUMs) |
|
||||||
|
| 6 | `schemas/05-infonavit-schema-ddl.sql` | Schema infonavit (8 tablas) |
|
||||||
|
| 7 | `schemas/06-inventory-ext-schema-ddl.sql` | Extension inventory |
|
||||||
|
| 8 | `schemas/07-purchase-ext-schema-ddl.sql` | Extension purchase |
|
||||||
|
| 9 | `schemas/08-finance-schema-ddl.sql` | Schema finance (11 tablas, 20 ENUMs) |
|
||||||
|
| 10 | `schemas/09-assets-schema-ddl.sql` | Schema assets (11 tablas, 9 ENUMs) |
|
||||||
|
| 11 | `schemas/10-documents-schema-ddl.sql` | Schema documents (11 tablas, 8 ENUMs) |
|
||||||
|
| 12 | `schemas/99-rls-policies.sql` | Politicas RLS para todos los schemas |
|
||||||
|
| 13 | `init-scripts/02-payment-terminals.sql` | Terminales de pago (opcional) |
|
||||||
|
|
||||||
|
### Escenario B: Base de Datos con erp-core (Recomendado)
|
||||||
|
|
||||||
|
Cuando erp-core YA esta instalado:
|
||||||
|
|
||||||
|
| Paso | Ubicacion | Archivo | Descripcion |
|
||||||
|
|------|-----------|---------|-------------|
|
||||||
|
| 1-N | erp-core | `01-auth-profiles.sql` ... `99-rls-erp-modules.sql` | Todos los DDL de erp-core |
|
||||||
|
| N+1 | erp-construccion | `init-scripts/01-init-database.sql` | Extensiones PostGIS, funciones |
|
||||||
|
| N+2 | erp-construccion | `schemas/01-construction-schema-ddl.sql` | Schema construction |
|
||||||
|
| ... | ... | ... | Resto de schemas construccion |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Verificaciones Pre-Ejecucion
|
||||||
|
|
||||||
|
El archivo `01-construction-schema-ddl.sql` incluye verificaciones automaticas:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
-- Verificar que ERP-Core esta instalado
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (SELECT 1 FROM pg_namespace WHERE nspname = 'auth') THEN
|
||||||
|
RAISE EXCEPTION 'Schema auth no existe. Ejecutar primero ERP-Core DDL';
|
||||||
|
END IF;
|
||||||
|
IF NOT EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'auth' AND tablename = 'tenants') THEN
|
||||||
|
RAISE EXCEPTION 'Tabla auth.tenants no existe. ERP-Core debe estar instalado';
|
||||||
|
END IF;
|
||||||
|
IF NOT EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'auth' AND tablename = 'users') THEN
|
||||||
|
RAISE EXCEPTION 'Tabla auth.users no existe. ERP-Core debe estar instalado';
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Naming de Schemas
|
||||||
|
|
||||||
|
### erp-core usa
|
||||||
|
|
||||||
|
| Schema | Descripcion |
|
||||||
|
|--------|-------------|
|
||||||
|
| `auth` | Autenticacion, tenants, users, roles |
|
||||||
|
| `core` | Sucursales, catalogos compartidos |
|
||||||
|
| `billing` | Facturacion SaaS |
|
||||||
|
| `inventory` | Inventario base |
|
||||||
|
| `partners` | Clientes/proveedores |
|
||||||
|
| `products` | Productos |
|
||||||
|
| `purchases` | Compras |
|
||||||
|
| `sales` | Ventas |
|
||||||
|
| `financial` | Contabilidad |
|
||||||
|
|
||||||
|
### erp-construccion usa
|
||||||
|
|
||||||
|
| Schema | Tipo | Descripcion |
|
||||||
|
|--------|------|-------------|
|
||||||
|
| `construction` | Propio | Proyectos, frentes, avances (24 tablas) |
|
||||||
|
| `estimates` | Propio | Estimaciones, generadores (8 tablas) |
|
||||||
|
| `infonavit` | Propio | Procesos INFONAVIT (8 tablas) |
|
||||||
|
| `hse` | Propio | Seguridad, salud, ambiente (58 tablas) |
|
||||||
|
| `hr` | Extension | RRHH extendido para construccion |
|
||||||
|
| `inventory` | Extension | Almacen de obra |
|
||||||
|
| `purchase` | Extension | Compras por proyecto |
|
||||||
|
| `finance` | Propio | Contabilidad construccion (11 tablas) |
|
||||||
|
| `assets` | Propio | Equipos, maquinaria (11 tablas) |
|
||||||
|
| `documents` | Propio | Gestion documental (11 tablas) |
|
||||||
|
|
||||||
|
### Schemas Legacy (Deprecados)
|
||||||
|
|
||||||
|
Los siguientes schemas se crean en `01-init-database.sql` para compatibilidad pero estan DEPRECADOS:
|
||||||
|
|
||||||
|
- `auth_management` -> Usar `auth`
|
||||||
|
- `project_management` -> Usar `construction`
|
||||||
|
- `financial_management` -> Usar `estimates`
|
||||||
|
- `purchasing_management` -> Usar `purchase`
|
||||||
|
- `inventory_management` -> Usar `inventory`
|
||||||
|
- `construction_management` -> Usar `construction`
|
||||||
|
- `quality_management` -> Usar `hse` o `construction`
|
||||||
|
- `safety_management` -> Usar `hse`
|
||||||
|
- `infonavit_management` -> Usar `infonavit`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Comandos de Ejecucion WSL
|
||||||
|
|
||||||
|
### Recrear BD Completa (Recomendado)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Desde Windows PowerShell
|
||||||
|
wsl -d Ubuntu-24.04 -u developer -- bash '/mnt/c/Empresas/ISEM/workspace-v2/projects/erp-construccion/database/drop-and-recreate-database.sh'
|
||||||
|
```
|
||||||
|
|
||||||
|
### Ejecutar Archivos Individuales
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Variables de conexion
|
||||||
|
export PGHOST=localhost
|
||||||
|
export PGPORT=5432
|
||||||
|
export PGDATABASE=erp_construccion_db
|
||||||
|
export PGUSER=erp_admin
|
||||||
|
export PGPASSWORD=erp_dev_2026
|
||||||
|
|
||||||
|
# Ruta base Windows desde WSL
|
||||||
|
BASE_PATH="/mnt/c/Empresas/ISEM/workspace-v2/projects/erp-construccion/database"
|
||||||
|
|
||||||
|
# 1. Inicializacion
|
||||||
|
psql -f "$BASE_PATH/init-scripts/01-init-database.sql"
|
||||||
|
|
||||||
|
# 2. Schemas en orden
|
||||||
|
for i in $(seq -w 1 10); do
|
||||||
|
f="$BASE_PATH/schemas/${i}-*.sql"
|
||||||
|
if ls $f 1>/dev/null 2>&1; then
|
||||||
|
psql -f $f
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# 3. RLS
|
||||||
|
psql -f "$BASE_PATH/schemas/99-rls-policies.sql"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Ejecutar Solo ERP-Core (si separado)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Ruta erp-core
|
||||||
|
CORE_PATH="/mnt/c/Empresas/ISEM/workspace-v2/projects/erp-core/database/ddl"
|
||||||
|
|
||||||
|
# Ejecutar todos los DDL de core en orden
|
||||||
|
for f in $(ls "$CORE_PATH"/*.sql | sort -V); do
|
||||||
|
psql -f "$f"
|
||||||
|
done
|
||||||
|
```
|
||||||
|
|
||||||
|
### Verificar Instalacion
|
||||||
|
|
||||||
|
```sql
|
||||||
|
-- Verificar schemas creados
|
||||||
|
SELECT schema_name
|
||||||
|
FROM information_schema.schemata
|
||||||
|
WHERE schema_name IN (
|
||||||
|
'auth', 'core', 'construction', 'estimates',
|
||||||
|
'infonavit', 'hse', 'hr', 'inventory',
|
||||||
|
'purchase', 'finance', 'assets', 'documents'
|
||||||
|
)
|
||||||
|
ORDER BY schema_name;
|
||||||
|
|
||||||
|
-- Contar tablas por schema
|
||||||
|
SELECT table_schema, COUNT(*) as table_count
|
||||||
|
FROM information_schema.tables
|
||||||
|
WHERE table_schema IN (
|
||||||
|
'auth', 'core', 'construction', 'estimates',
|
||||||
|
'infonavit', 'hse', 'hr', 'inventory',
|
||||||
|
'purchase', 'finance', 'assets', 'documents'
|
||||||
|
)
|
||||||
|
GROUP BY table_schema
|
||||||
|
ORDER BY table_schema;
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Error: "Schema auth no existe"
|
||||||
|
|
||||||
|
**Causa:** Se ejecuto `01-construction-schema-ddl.sql` sin ejecutar `01-init-database.sql` primero, o sin instalar erp-core.
|
||||||
|
|
||||||
|
**Solucion:**
|
||||||
|
```bash
|
||||||
|
# Opcion A: Ejecutar init primero
|
||||||
|
psql -f init-scripts/01-init-database.sql
|
||||||
|
|
||||||
|
# Opcion B: Instalar erp-core completo primero
|
||||||
|
```
|
||||||
|
|
||||||
|
### Error: "Tabla auth.tenants no existe"
|
||||||
|
|
||||||
|
**Causa:** El init-database.sql usa fallback a `core.tenants` pero el construction DDL espera `auth.tenants`.
|
||||||
|
|
||||||
|
**Solucion:** El `01-init-database.sql` crea tablas en `core.*` como fallback. Para produccion, se recomienda instalar erp-core completo.
|
||||||
|
|
||||||
|
### Error: Extension postgis no disponible
|
||||||
|
|
||||||
|
**Causa:** PostGIS no esta instalado en el servidor PostgreSQL.
|
||||||
|
|
||||||
|
**Solucion:**
|
||||||
|
```bash
|
||||||
|
# En Ubuntu/Debian
|
||||||
|
sudo apt install postgresql-15-postgis-3
|
||||||
|
|
||||||
|
# Luego en psql
|
||||||
|
CREATE EXTENSION postgis;
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Referencias
|
||||||
|
|
||||||
|
- `HERENCIA-ERP-CORE.md` - Relacion con erp-core
|
||||||
|
- `_MAP.md` - Mapa completo de la BD
|
||||||
|
- `VALIDACION-DDL-INVENTARIOS.md` - Validaciones de inventario
|
||||||
|
- `../CLAUDE.md` - Instrucciones del proyecto
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
*Documentacion creada: 2026-02-03 - ST-P0-003*
|
||||||
258
README.md
258
README.md
@ -1,185 +1,221 @@
|
|||||||
# Database - MVP Sistema Administración de Obra
|
# Database - ERP Construccion
|
||||||
|
|
||||||
**Stack:** PostgreSQL 15+ con PostGIS
|
**Stack:** PostgreSQL 15+ con PostGIS
|
||||||
**Versión:** 1.0.0
|
**Version:** 2.0.0
|
||||||
**Fecha:** 2025-11-20
|
**Fecha:** 2026-02-03
|
||||||
|
**Dependencia:** erp-core (auth, core schemas)
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📋 DESCRIPCIÓN
|
## Descripcion
|
||||||
|
|
||||||
Base de datos del sistema de administración de obra e INFONAVIT.
|
Base de datos del ERP de construccion. Extiende erp-core con schemas especializados para gestion de obras, estimaciones, INFONAVIT, HSE y mas.
|
||||||
|
|
||||||
**Schemas principales:**
|
**Schemas principales (actuales):**
|
||||||
- `auth_management` - Autenticación y usuarios
|
- `construction` - Proyectos, fraccionamientos, avances (24 tablas)
|
||||||
- `project_management` - Proyectos y estructura de obra
|
- `estimates` - Presupuestos, estimaciones, generadores (8 tablas)
|
||||||
- `financial_management` - Presupuestos y control financiero
|
- `infonavit` - Integracion INFONAVIT (8 tablas)
|
||||||
- `purchasing_management` - Compras e inventarios
|
- `hse` - Seguridad, salud y ambiente (58 tablas)
|
||||||
- `construction_management` - Control de obra y avances
|
- `hr` - Extension RRHH para construccion
|
||||||
- `quality_management` - Calidad y postventa
|
- `inventory` - Extension almacen de obra
|
||||||
- `infonavit_management` - Integración INFONAVIT
|
- `purchase` - Extension compras por proyecto
|
||||||
|
- `finance` - Contabilidad construccion (11 tablas)
|
||||||
|
- `assets` - Equipos y maquinaria (11 tablas)
|
||||||
|
- `documents` - Gestion documental (11 tablas)
|
||||||
|
|
||||||
|
**Total:** 10 schemas, 143 tablas, 115 ENUMs
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🏗️ ESTRUCTURA
|
## Estructura
|
||||||
|
|
||||||
```
|
```
|
||||||
ddl/
|
database/
|
||||||
├── 00-init.sql # Inicialización + extensiones
|
├── DDL-EXECUTION-ORDER.md # Orden de ejecucion (LEER PRIMERO)
|
||||||
└── schemas/ # Schemas por contexto
|
├── README.md # Este archivo
|
||||||
├── auth_management/
|
├── HERENCIA-ERP-CORE.md # Relacion con erp-core
|
||||||
│ ├── tables/ # Tablas (01-users.sql, 02-roles.sql, ...)
|
├── _MAP.md # Mapa completo de la BD
|
||||||
│ ├── functions/ # Funciones SQL
|
├── VALIDACION-DDL-INVENTARIOS.md # Validaciones
|
||||||
│ ├── triggers/ # Triggers
|
├── drop-and-recreate-database.sh # Script recrear BD
|
||||||
│ └── views/ # Vistas
|
├── validate-clean-load-policy.sh # Validar politica carga limpia
|
||||||
├── project_management/
|
├── init-scripts/
|
||||||
│ └── ...
|
│ ├── 01-init-database.sql # Extensiones, funciones, schemas base
|
||||||
└── [otros schemas]/
|
│ └── 02-payment-terminals.sql # Terminales de pago
|
||||||
|
└── schemas/
|
||||||
seeds/
|
├── 01-construction-schema-ddl.sql # construction (24 tablas)
|
||||||
├── dev/ # Datos de desarrollo
|
├── 02-hr-schema-ddl.sql # hr extension
|
||||||
└── prod/ # Datos de producción inicial
|
├── 03-hse-schema-ddl.sql # hse (58 tablas, 67 ENUMs)
|
||||||
|
├── 04-estimates-schema-ddl.sql # estimates (8 tablas)
|
||||||
migrations/ # Migraciones versionadas
|
├── 05-infonavit-schema-ddl.sql # infonavit (8 tablas)
|
||||||
scripts/ # Scripts de utilidad
|
├── 06-inventory-ext-schema-ddl.sql # inventory extension
|
||||||
|
├── 07-purchase-ext-schema-ddl.sql # purchase extension
|
||||||
|
├── 08-finance-schema-ddl.sql # finance (11 tablas)
|
||||||
|
├── 09-assets-schema-ddl.sql # assets (11 tablas)
|
||||||
|
├── 10-documents-schema-ddl.sql # documents (11 tablas)
|
||||||
|
└── 99-rls-policies.sql # Politicas RLS
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🚀 SETUP INICIAL
|
## Dependencias
|
||||||
|
|
||||||
### 1. Crear Base de Datos
|
**IMPORTANTE:** Este proyecto depende de erp-core. Antes de ejecutar los DDL de construccion:
|
||||||
|
|
||||||
|
1. El schema `auth` debe existir con las tablas `tenants` y `users`
|
||||||
|
2. Ver `DDL-EXECUTION-ORDER.md` para orden de ejecucion completo
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Setup Inicial
|
||||||
|
|
||||||
|
### Opcion A: Recrear BD Completa (Recomendado)
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Ejecutar script de creación
|
# Desde Windows PowerShell - ejecuta en WSL
|
||||||
cd scripts
|
wsl -d Ubuntu-24.04 -u developer -- bash '/mnt/c/Empresas/ISEM/workspace-v2/projects/erp-construccion/database/drop-and-recreate-database.sh'
|
||||||
./create-database.sh
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### 2. Ejecutar DDL
|
### Opcion B: Ejecutar DDL Manualmente
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Ejecutar inicialización
|
# Variables de conexion
|
||||||
psql $DATABASE_URL -f ddl/00-init.sql
|
export PGHOST=localhost
|
||||||
|
export PGPORT=5432
|
||||||
|
export PGDATABASE=erp_construccion_db
|
||||||
|
export PGUSER=erp_admin
|
||||||
|
export PGPASSWORD=erp_dev_2026
|
||||||
|
|
||||||
# Ejecutar schemas (en orden)
|
# Ruta base desde WSL
|
||||||
psql $DATABASE_URL -f ddl/schemas/auth_management/tables/01-users.sql
|
BASE="/mnt/c/Empresas/ISEM/workspace-v2/projects/erp-construccion/database"
|
||||||
# ... etc
|
|
||||||
```
|
|
||||||
|
|
||||||
### 3. Cargar Seeds (desarrollo)
|
# 1. Inicializacion
|
||||||
|
psql -f "$BASE/init-scripts/01-init-database.sql"
|
||||||
|
|
||||||
```bash
|
# 2. Schemas en orden
|
||||||
psql $DATABASE_URL -f seeds/dev/01-users.sql
|
psql -f "$BASE/schemas/01-construction-schema-ddl.sql"
|
||||||
psql $DATABASE_URL -f seeds/dev/02-projects.sql
|
psql -f "$BASE/schemas/02-hr-schema-ddl.sql"
|
||||||
|
psql -f "$BASE/schemas/03-hse-schema-ddl.sql"
|
||||||
|
psql -f "$BASE/schemas/04-estimates-schema-ddl.sql"
|
||||||
|
psql -f "$BASE/schemas/05-infonavit-schema-ddl.sql"
|
||||||
|
psql -f "$BASE/schemas/06-inventory-ext-schema-ddl.sql"
|
||||||
|
psql -f "$BASE/schemas/07-purchase-ext-schema-ddl.sql"
|
||||||
|
psql -f "$BASE/schemas/08-finance-schema-ddl.sql"
|
||||||
|
psql -f "$BASE/schemas/09-assets-schema-ddl.sql"
|
||||||
|
psql -f "$BASE/schemas/10-documents-schema-ddl.sql"
|
||||||
|
psql -f "$BASE/schemas/99-rls-policies.sql"
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🔧 SCRIPTS DISPONIBLES
|
## Scripts Disponibles
|
||||||
|
|
||||||
| Script | Descripción |
|
| Script | Descripcion |
|
||||||
|--------|-------------|
|
|--------|-------------|
|
||||||
| `create-database.sh` | Crea la base de datos desde cero |
|
| `drop-and-recreate-database.sh` | Elimina y recrea la BD completa |
|
||||||
| `reset-database.sh` | Elimina y recrea la base de datos |
|
| `validate-clean-load-policy.sh` | Valida politica de carga limpia |
|
||||||
| `run-migrations.sh` | Ejecuta migraciones pendientes |
|
|
||||||
| `backup-database.sh` | Crea backup de la base de datos |
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📊 CONVENCIONES
|
## Convenciones
|
||||||
|
|
||||||
### Nomenclatura
|
### Nomenclatura Actual
|
||||||
|
|
||||||
Seguir **ESTANDARES-NOMENCLATURA.md**:
|
- Schemas: `snake_case` (sin sufijo `_management`)
|
||||||
- Schemas: `snake_case` + sufijo `_management`
|
|
||||||
- Tablas: `snake_case` plural
|
- Tablas: `snake_case` plural
|
||||||
- Columnas: `snake_case` singular
|
- Columnas: `snake_case` singular
|
||||||
- Índices: `idx_{tabla}_{columnas}`
|
- Indices: `idx_{tabla}_{columnas}`
|
||||||
- Foreign Keys: `fk_{origen}_to_{destino}`
|
- Constraints: `chk_{tabla}_{condicion}`, `uq_{tabla}_{columnas}`
|
||||||
- Constraints: `chk_{tabla}_{columna}`
|
|
||||||
|
|
||||||
### Estructura de Archivo DDL
|
### Schemas Legacy (Deprecados)
|
||||||
|
|
||||||
```sql
|
Los siguientes schemas existen para compatibilidad pero NO deben usarse:
|
||||||
-- ============================================================================
|
- `auth_management` -> Usar `auth`
|
||||||
-- Tabla: nombre_tabla
|
- `project_management` -> Usar `construction`
|
||||||
-- Schema: nombre_schema
|
- `financial_management` -> Usar `estimates`
|
||||||
-- Descripción: [descripción]
|
- Ver `DDL-EXECUTION-ORDER.md` para lista completa
|
||||||
-- Autor: Database-Agent
|
|
||||||
-- Fecha: YYYY-MM-DD
|
|
||||||
-- ============================================================================
|
|
||||||
|
|
||||||
DROP TABLE IF EXISTS schema.tabla CASCADE;
|
|
||||||
|
|
||||||
CREATE TABLE schema.tabla (
|
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
||||||
-- columnas...
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
||||||
);
|
|
||||||
|
|
||||||
COMMENT ON TABLE schema.tabla IS '[descripción]';
|
|
||||||
COMMENT ON COLUMN schema.tabla.columna IS '[descripción]';
|
|
||||||
|
|
||||||
CREATE INDEX idx_tabla_columna ON schema.tabla(columna);
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 🔍 VALIDACIÓN
|
## Validacion
|
||||||
|
|
||||||
### Verificar Schemas
|
### Verificar Schemas
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
SELECT schema_name
|
SELECT schema_name
|
||||||
FROM information_schema.schemata
|
FROM information_schema.schemata
|
||||||
WHERE schema_name LIKE '%_management';
|
WHERE schema_name IN (
|
||||||
|
'auth', 'core', 'construction', 'estimates',
|
||||||
|
'infonavit', 'hse', 'hr', 'inventory',
|
||||||
|
'purchase', 'finance', 'assets', 'documents'
|
||||||
|
)
|
||||||
|
ORDER BY schema_name;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Verificar Tablas
|
### Contar Tablas por Schema
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
SELECT table_schema, table_name,
|
SELECT table_schema, COUNT(*) as table_count
|
||||||
(SELECT COUNT(*) FROM information_schema.columns
|
FROM information_schema.tables
|
||||||
WHERE table_schema = t.table_schema
|
WHERE table_schema IN (
|
||||||
AND table_name = t.table_name) as column_count
|
'construction', 'estimates', 'infonavit', 'hse',
|
||||||
FROM information_schema.tables t
|
'hr', 'inventory', 'purchase', 'finance', 'assets', 'documents'
|
||||||
WHERE table_schema LIKE '%_management'
|
)
|
||||||
ORDER BY table_schema, table_name;
|
GROUP BY table_schema
|
||||||
|
ORDER BY table_count DESC;
|
||||||
```
|
```
|
||||||
|
|
||||||
### Verificar Índices
|
### Verificar Tablas Core Requeridas
|
||||||
|
|
||||||
```sql
|
```sql
|
||||||
SELECT tablename, indexname
|
-- Estas tablas deben existir antes de ejecutar DDL de construccion
|
||||||
FROM pg_indexes
|
SELECT EXISTS (
|
||||||
WHERE schemaname LIKE '%_management'
|
SELECT 1 FROM pg_tables
|
||||||
ORDER BY tablename;
|
WHERE schemaname = 'auth' AND tablename = 'tenants'
|
||||||
|
) AS tenants_exists,
|
||||||
|
EXISTS (
|
||||||
|
SELECT 1 FROM pg_tables
|
||||||
|
WHERE schemaname = 'auth' AND tablename = 'users'
|
||||||
|
) AS users_exists;
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📚 REFERENCIAS
|
## Referencias
|
||||||
|
|
||||||
- [DIRECTIVA-DISENO-BASE-DATOS.md](../../orchestration/directivas/DIRECTIVA-DISENO-BASE-DATOS.md)
|
- `DDL-EXECUTION-ORDER.md` - Orden de ejecucion y dependencias
|
||||||
- [ESTANDARES-NOMENCLATURA.md](../../orchestration/directivas/ESTANDARES-NOMENCLATURA.md)
|
- `HERENCIA-ERP-CORE.md` - Relacion con erp-core
|
||||||
- [MVP-APP.md](../../docs/00-overview/MVP-APP.md)
|
- `_MAP.md` - Mapa completo de la BD
|
||||||
|
- `VALIDACION-DDL-INVENTARIOS.md` - Validaciones de inventario
|
||||||
|
- `../../CLAUDE.md` - Instrucciones del proyecto
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📝 VARIABLES DE ENTORNO
|
## Variables de Entorno
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
DATABASE_URL=postgresql://usuario:password@localhost:5432/nombre_db
|
# Desarrollo local (WSL)
|
||||||
DB_HOST=localhost
|
DB_HOST=localhost
|
||||||
DB_PORT=5432
|
DB_PORT=5432
|
||||||
DB_NAME=erp_construccion
|
DB_NAME=erp_construccion_db
|
||||||
DB_USER=postgres
|
DB_USER=erp_admin
|
||||||
DB_PASSWORD=password
|
DB_PASSWORD=erp_dev_2026
|
||||||
|
|
||||||
|
# Connection string
|
||||||
|
DATABASE_URL=postgresql://erp_admin:erp_dev_2026@localhost:5432/erp_construccion_db
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
**Mantenido por:** Database-Agent
|
## Credenciales de Desarrollo
|
||||||
**Última actualización:** 2025-11-20
|
|
||||||
|
| Variable | Valor |
|
||||||
|
|----------|-------|
|
||||||
|
| Database | erp_construccion_db |
|
||||||
|
| User | erp_admin |
|
||||||
|
| Password | erp_dev_2026 |
|
||||||
|
| Host | localhost |
|
||||||
|
| Port | 5432 |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
*Documentacion actualizada: 2026-02-03*
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user