solucion de eventos por pais
This commit is contained in:
parent
fc06566928
commit
68a5528f2f
7 changed files with 796 additions and 14 deletions
89
init-db/09-eventos.sql
Normal file
89
init-db/09-eventos.sql
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
BEGIN;
|
||||
|
||||
-- =============================================
|
||||
-- Sistema de eventos (clustering incremental)
|
||||
-- =============================================
|
||||
|
||||
-- ---------------------------------------------
|
||||
-- 1. TABLA DE EVENTOS (CLUSTERS)
|
||||
-- ---------------------------------------------
|
||||
CREATE TABLE IF NOT EXISTS eventos (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
creado_en TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
actualizado_en TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
|
||||
-- Datos "semánticos" del evento (para la web)
|
||||
titulo TEXT,
|
||||
fecha_inicio TIMESTAMPTZ,
|
||||
fecha_fin TIMESTAMPTZ,
|
||||
n_noticias INTEGER NOT NULL DEFAULT 0,
|
||||
|
||||
-- Datos de clustering
|
||||
centroid JSONB NOT NULL,
|
||||
total_traducciones INTEGER NOT NULL DEFAULT 1
|
||||
);
|
||||
|
||||
-- ---------------------------------------------
|
||||
-- 2. COLUMNA evento_id EN TRADUCCIONES
|
||||
-- ---------------------------------------------
|
||||
ALTER TABLE traducciones
|
||||
ADD COLUMN IF NOT EXISTS evento_id BIGINT REFERENCES eventos(id);
|
||||
|
||||
-- ---------------------------------------------
|
||||
-- 3. TABLA RELACIÓN EVENTO <-> NOTICIA <-> TRADUCCIÓN
|
||||
-- ---------------------------------------------
|
||||
CREATE TABLE IF NOT EXISTS eventos_noticias (
|
||||
evento_id BIGINT NOT NULL REFERENCES eventos(id) ON DELETE CASCADE,
|
||||
noticia_id CHAR(32) NOT NULL REFERENCES noticias(id) ON DELETE CASCADE,
|
||||
traduccion_id BIGINT NOT NULL REFERENCES traducciones(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (evento_id, traduccion_id)
|
||||
);
|
||||
|
||||
-- ---------------------------------------------
|
||||
-- 4. ÍNDICES ÚTILES
|
||||
-- ---------------------------------------------
|
||||
|
||||
-- Consultar traducciones por evento
|
||||
CREATE INDEX IF NOT EXISTS idx_traducciones_evento
|
||||
ON traducciones(evento_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_traducciones_evento_fecha
|
||||
ON traducciones(evento_id, noticia_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_trad_id
|
||||
ON traducciones(id);
|
||||
|
||||
-- Ordenar eventos por fecha de inicio
|
||||
CREATE INDEX IF NOT EXISTS idx_eventos_fecha_inicio
|
||||
ON eventos (fecha_inicio DESC NULLS LAST);
|
||||
|
||||
-- Relación evento <-> noticia / traducción
|
||||
CREATE INDEX IF NOT EXISTS idx_eventos_noticias_evento
|
||||
ON eventos_noticias (evento_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_eventos_noticias_noticia
|
||||
ON eventos_noticias (noticia_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_eventos_noticias_traduccion
|
||||
ON eventos_noticias (traduccion_id);
|
||||
|
||||
-- ---------------------------------------------
|
||||
-- 5. TRIGGER PARA actualizar "actualizado_en"
|
||||
-- ---------------------------------------------
|
||||
CREATE OR REPLACE FUNCTION actualizar_evento_modificado()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.actualizado_en = NOW();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
DROP TRIGGER IF EXISTS trg_evento_modificado ON eventos;
|
||||
|
||||
CREATE TRIGGER trg_evento_modificado
|
||||
BEFORE UPDATE ON eventos
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION actualizar_evento_modificado();
|
||||
|
||||
COMMIT;
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue