- P2606: Plan v1.1 EN EJECUCIÓN. Fases 1-3 completadas. - Ficha nodo srvv-nginx-rm.md. P2606 en adn/07_proyectos.md. - Incluye cambios acumulados de sesiones anteriores.
55 lines
2.1 KiB
SQL
55 lines
2.1 KiB
SQL
-- 001_create_ambitos.sql
|
|
-- Creación de tabla ambitos y modificación de nodos y entradas
|
|
|
|
DO $$
|
|
BEGIN
|
|
-- Crear tabla ambitos
|
|
CREATE TABLE IF NOT EXISTS bitacoras.ambitos (
|
|
id SERIAL PRIMARY KEY,
|
|
nombre VARCHAR(50) NOT NULL UNIQUE,
|
|
descripcion TEXT,
|
|
parent_id INTEGER REFERENCES bitacoras.ambitos(id) ON DELETE SET NULL,
|
|
activo BOOLEAN DEFAULT true,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
-- Índices para ambitos
|
|
CREATE INDEX IF NOT EXISTS idx_ambitos_nombre ON bitacoras.ambitos(nombre);
|
|
CREATE INDEX IF NOT EXISTS idx_ambitos_parent ON bitacoras.ambitos(parent_id);
|
|
|
|
-- Modificar tabla nodos: añadir ambito_id
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM information_schema.columns
|
|
WHERE table_schema = 'bitacoras' AND table_name = 'nodos' AND column_name = 'ambito_id'
|
|
) THEN
|
|
ALTER TABLE bitacoras.nodos
|
|
ADD COLUMN ambito_id INTEGER REFERENCES bitacoras.ambitos(id) ON DELETE SET NULL;
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_nodos_ambito ON bitacoras.nodos(ambito_id);
|
|
END IF;
|
|
|
|
-- Modificar tabla entradas: añadir ambito_id
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM information_schema.columns
|
|
WHERE table_schema = 'bitacoras' AND table_name = 'entradas' AND column_name = 'ambito_id'
|
|
) THEN
|
|
ALTER TABLE bitacoras.entradas
|
|
ADD COLUMN ambito_id INTEGER REFERENCES bitacoras.ambitos(id) ON DELETE SET NULL;
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_entradas_ambito ON bitacoras.entradas(ambito_id);
|
|
END IF;
|
|
|
|
-- Crear vista materializada o tabla resumen_ambitos
|
|
CREATE TABLE IF NOT EXISTS bitacoras.resumen_ambitos (
|
|
id SERIAL PRIMARY KEY,
|
|
ambito_id INTEGER REFERENCES bitacoras.ambitos(id),
|
|
fecha DATE,
|
|
total_entradas INTEGER DEFAULT 0,
|
|
tiempo_total INTERVAL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
UNIQUE(ambito_id, fecha)
|
|
);
|
|
|
|
END $$;
|