workspace-v1/projects/gamilit/database/schemas/01-core-ddl.sql
Adrian Flores Cortes 967ab360bb Initial commit: Workspace v1 with 3-layer architecture
Structure:
- control-plane/: Registries, SIMCO directives, CI/CD templates
- projects/: Gamilit, ERP-Suite, Trading-Platform, Betting-Analytics
- shared/: Libs catalog, knowledge-base

Key features:
- Centralized port, domain, database, and service registries
- 23 SIMCO directives + 6 fundamental principles
- NEXUS agent profiles with delegation rules
- Validation scripts for workspace integrity
- Dockerfiles for all services
- Path aliases for quick reference

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-23 00:35:19 -06:00

71 lines
2.7 KiB
PL/PgSQL

-- ==============================================================================
-- GAMILIT - SCHEMA CORE
-- ==============================================================================
-- Tablas fundamentales del sistema
-- Mantenido por: Database-Agent
-- Actualizado: 2025-12-18
-- ==============================================================================
-- ------------------------------------------------------------------------------
-- FUNCION PARA UPDATED_AT
-- ------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ language 'plpgsql';
-- ------------------------------------------------------------------------------
-- TABLA: TENANTS (Multi-tenancy)
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tenants (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(255) NOT NULL,
slug VARCHAR(100) UNIQUE NOT NULL,
settings JSONB DEFAULT '{}',
status VARCHAR(50) DEFAULT 'active',
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_tenants_slug ON tenants(slug);
CREATE INDEX idx_tenants_status ON tenants(status);
CREATE TRIGGER update_tenants_updated_at
BEFORE UPDATE ON tenants
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- ------------------------------------------------------------------------------
-- TABLA: ROLES
-- ------------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS roles (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(100) NOT NULL,
description TEXT,
permissions JSONB DEFAULT '[]',
tenant_id UUID REFERENCES tenants(id) ON DELETE CASCADE,
is_system BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
UNIQUE(name, tenant_id)
);
CREATE INDEX idx_roles_tenant ON roles(tenant_id);
CREATE TRIGGER update_roles_updated_at
BEFORE UPDATE ON roles
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- ------------------------------------------------------------------------------
-- INSERTAR ROLES DEL SISTEMA
-- ------------------------------------------------------------------------------
INSERT INTO roles (name, description, permissions, is_system) VALUES
('admin', 'Administrador del sistema', '["*"]', true),
('instructor', 'Instructor de cursos', '["courses:*", "students:read"]', true),
('student', 'Estudiante', '["courses:read", "progress:*", "achievements:read"]', true)
ON CONFLICT DO NOTHING;