erp-core-database-v2/ddl
Adrian Flores Cortes bc15eec17d [TASK-028] security: Add RLS policies for 43 ERP tables
Creates Row Level Security policies for all ERP module tables:
- products: 2 policies
- inventory: 7 policies
- partners: 2 policies
- sales: 2 policies
- purchases: 2 policies
- billing: 2 policies
- financial: 17 policies
- projects: 6 policies

Total: 43 RLS policies for multi-tenant data isolation.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-24 22:23:28 -06:00
..
01-auth-profiles.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
02-auth-devices.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
03-core-branches.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
04-mobile.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
05-billing-usage.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
06-auth-extended.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
07-users-rbac.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
08-plans.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
09-notifications.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
10-audit.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
11-feature-flags.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
12-webhooks.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
13-storage.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
14-ai.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
15-whatsapp.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
16-partners.sql [SPRINT-2-COHERENCIA] feat: Add missing DDL tables and fix numbering 2026-01-24 08:54:32 -06:00
17-products.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
18-warehouses.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
19-product-attributes.sql [TASK-2026-01-24-GAPS] feat: Add product attributes DDL + fix numbering 2026-01-24 07:27:58 -06:00
20-core-catalogs.sql feat(catalogs): Add core catalogs DDL and seed data (MGN-005) 2026-01-18 08:57:41 -06:00
21-inventory.sql [SPRINT-2-COHERENCIA] feat: Add missing DDL tables and fix numbering 2026-01-24 08:54:32 -06:00
22-sales.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
23-purchases.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
24-invoices.sql refactor: Restructure DDL with numbered schema files 2026-01-16 00:40:32 -06:00
26-fiscal-catalogs.sql [SPRINT-2-COHERENCIA] feat: Add missing DDL tables and fix numbering 2026-01-24 08:54:32 -06:00
46-purchases-matching.sql [TASK-2026-01-20-003] feat: Add financial DDL and matching tables 2026-01-20 03:47:04 -06:00
50-financial-schema.sql [TASK-2026-01-20-003] feat: Add financial DDL and matching tables 2026-01-20 03:47:04 -06:00
51-financial-accounts.sql [TASK-2026-01-20-003] feat: Add financial DDL and matching tables 2026-01-20 03:47:04 -06:00
52-financial-journals.sql [TASK-2026-01-20-003] feat: Add financial DDL and matching tables 2026-01-20 03:47:04 -06:00
53-financial-entries.sql [TASK-2026-01-20-003] feat: Add financial DDL and matching tables 2026-01-20 03:47:04 -06:00
54-financial-invoices.sql [TASK-2026-01-20-003] feat: Add financial DDL and matching tables 2026-01-20 03:47:04 -06:00
55-financial-payments.sql [TASK-2026-01-20-003] feat: Add financial DDL and matching tables 2026-01-20 03:47:04 -06:00
56-financial-taxes.sql [TASK-2026-01-20-003] feat: Add financial DDL and matching tables 2026-01-20 03:47:04 -06:00
57-financial-bank-reconciliation.sql [TASK-2026-01-20-003] feat: Add financial DDL and matching tables 2026-01-20 03:47:04 -06:00
60-projects-timesheets.sql [PROJECTS] feat: Add timesheets DDL schema 2026-01-20 04:27:58 -06:00
99-rls-erp-modules.sql [TASK-028] security: Add RLS policies for 43 ERP tables 2026-01-24 22:23:28 -06:00
README.md [SPRINT-3-DOCS] docs: Add DDL README with numbering documentation 2026-01-24 11:56:54 -06:00

Database DDL Files - erp-core

Version: 1.0.0 Fecha: 2026-01-24 Total Archivos: 35 Total Schemas: 20


Convenciones de Numeracion

Los archivos DDL siguen un esquema de numeracion por rangos segun dominio funcional:

Rango Dominio Descripcion
01-09 Foundation Auth, core, mobile, billing base
10-15 System Audit, feature-flags, webhooks, storage, AI, WhatsApp
16-19 Partners & Products Partners, products, warehouses, attributes
20-24 Catalogs & Operations Core catalogs, inventory, sales, purchases, invoices
25-49 Reserved Expansiones futuras
50-59 Financial Contabilidad completa
60-69 Projects Proyectos y timesheets

Archivos DDL por Dominio

Foundation (01-09)

# Archivo Schema Descripcion
01 01-auth-profiles.sql auth Usuarios, sesiones, perfiles
02 02-auth-devices.sql auth Dispositivos, biometria
03 03-core-branches.sql core Sucursales, horarios
04 04-mobile.sql mobile Sesiones movil, push, sync
05 05-billing-usage.sql billing Suscripciones, planes, uso
06 06-auth-extended.sql auth OAuth, MFA extendido
07 07-users-rbac.sql auth Roles, permisos, grupos
08 08-plans.sql billing Planes detallados, features
09 09-notifications.sql notifications Notificaciones multicanal

System (10-15)

# Archivo Schema Descripcion
10 10-audit.sql audit Logs de auditoria completos
11 11-feature-flags.sql feature_flags Feature toggles por tenant
12 12-webhooks.sql webhooks Suscripciones y entregas
13 13-storage.sql storage Archivos, buckets, uploads
14 14-ai.sql ai Modelos, prompts, completions
15 15-whatsapp.sql whatsapp Conversaciones, mensajes

Partners & Products (16-19)

# Archivo Schema Descripcion
16 16-partners.sql partners Clientes/proveedores, tax_info, segments
17 17-products.sql products Productos comerciales
18 18-warehouses.sql inventory Almacenes, zonas, ubicaciones
19 19-product-attributes.sql products Atributos, variantes

Catalogs & Operations (20-26)

# Archivo Schema Descripcion
20 20-core-catalogs.sql core Paises, monedas, UoM, secuencias
21 21-inventory.sql inventory Stock, lots, pickings, transfers
22 22-sales.sql sales Ordenes de venta, cotizaciones
23 23-purchases.sql purchases Ordenes de compra
24 24-invoices.sql billing Facturas operacionales, pagos
26 26-fiscal-catalogs.sql fiscal Catalogos SAT (CFDI, regimenes)

Financial (50-57)

# Archivo Schema Descripcion
50 50-financial-schema.sql financial Schema y extensiones
51 51-financial-accounts.sql financial Plan de cuentas, tipos
52 52-financial-journals.sql financial Diarios contables
53 53-financial-entries.sql financial Asientos contables
54 54-financial-invoices.sql financial Facturas contables
55 55-financial-payments.sql financial Pagos contables
56 56-financial-taxes.sql financial Impuestos
57 57-financial-bank-reconciliation.sql financial Conciliacion bancaria

Projects (60+)

# Archivo Schema Descripcion
60 60-projects-timesheets.sql projects Hojas de tiempo

Matching (46)

# Archivo Schema Descripcion
46 46-purchases-matching.sql purchases 3-way matching PO/Receipt/Invoice

Schemas PostgreSQL

Schema Proposito DDL Files
auth Autenticacion y usuarios 01, 02, 06, 07
audit Logs de auditoria 10
billing Facturacion SaaS 05, 08, 24
core Catalogos compartidos 03, 20
feature_flags Feature toggles 11
financial Contabilidad 50-57
fiscal Catalogos SAT 26
inventory Inventario y stock 18, 21
mobile App movil 04
notifications Notificaciones 09
partners Clientes/proveedores 16
products Productos 17, 19
projects Proyectos 60
purchases Compras 23, 46
sales Ventas 22
storage Archivos 13
webhooks Webhooks 12
whatsapp WhatsApp 15
ai Inteligencia artificial 14

Orden de Ejecucion

Para una instalacion limpia, ejecutar en orden numerico:

for f in $(ls *.sql | sort -n); do
  psql -d erp_db -f "$f"
done

Dependencias importantes:

  1. 01-auth-profiles.sql debe ejecutarse primero (define auth.users, auth.tenants)
  2. 50-financial-schema.sql antes del resto de financial
  3. 20-core-catalogs.sql antes de inventory/sales/purchases

Notas de Sprint 2 (2026-01-24)

Cambios realizados:

  1. 21-inventory.sql: Agregadas tablas lots y pickings con ENUMs
  2. 16-partners.sql: Agregadas tablas partner_tax_info y partner_segments
  3. 26-fiscal-catalogs.sql: Renumerado de 21 a 26 para evitar conflicto

Renumeracion historica:

  • 21-fiscal-catalogs.sql -> 26-fiscal-catalogs.sql (Sprint 2, 2026-01-24)
  • 25-fiscal-catalogs.sql eliminado (duplicado)

Referencias

  • Entities: docs/_definitions/ENTITIES-CATALOG.md
  • Services: docs/_definitions/SERVICES-CATALOG.md
  • Invoices/Payments: docs/_definitions/INVOICES-PAYMENTS-REFERENCE.md

Documentacion creada: 2026-01-24 - Sprint 3 Documentacion