Cambios: - ejecutor.rb: Corregir lectura de output con Open3 - bitacora_db.rb: Agregar métodos crear_evento/actualizar_evento (public) - dron_db.rb: Corregir conversión de PG::Result a Hash (.first.to_h) - vigilante.rb: Corregir formato de fecha en dashboard - migrations/004: Crear tabla bitacoras.events para Bitácora Web Pruebas: - dron lanzar --evento AUTO --nota "Test" -- echo "hola" ✅ - dron flota ✅ - dron salud ✅ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
66 lines
3.0 KiB
Ruby
66 lines
3.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Migración 004: Crear tabla events para Bitácora Web
|
|
# ====================================================
|
|
# Esta tabla almacena eventos visibles en la Bitácora Web
|
|
# Es el medio de comunicación donde los drones dejan registro de su actividad
|
|
|
|
require_relative '../core/bitacora_db'
|
|
|
|
module BitacorasDB
|
|
module Migrations
|
|
class CreateEventsTable
|
|
def self.up
|
|
BitacorasDB::BitacoraDB.with_connection do |db|
|
|
# Crear tabla
|
|
db.instance_variable_get(:@connection).exec(<<~SQL)
|
|
CREATE TABLE IF NOT EXISTS bitacoras.events (
|
|
id SERIAL PRIMARY KEY,
|
|
nodo_id INTEGER NOT NULL REFERENCES bitacoras.nodos(id),
|
|
descripcion TEXT NOT NULL,
|
|
estado VARCHAR(10) NOT NULL DEFAULT '⏳',
|
|
inicio VARCHAR(10) NOT NULL,
|
|
fin VARCHAR(10),
|
|
modo VARCHAR(10) DEFAULT 'A',
|
|
metadata JSONB DEFAULT '{}'::jsonb,
|
|
created_at TIMESTAMP DEFAULT NOW(),
|
|
updated_at TIMESTAMP DEFAULT NOW()
|
|
)
|
|
SQL
|
|
|
|
# Crear índices
|
|
db.instance_variable_get(:@connection).exec("CREATE INDEX IF NOT EXISTS idx_events_nodo ON bitacoras.events(nodo_id)")
|
|
db.instance_variable_get(:@connection).exec("CREATE INDEX IF NOT EXISTS idx_events_estado ON bitacoras.events(estado)")
|
|
db.instance_variable_get(:@connection).exec("CREATE INDEX IF NOT EXISTS idx_events_inicio ON bitacoras.events(inicio)")
|
|
db.instance_variable_get(:@connection).exec("CREATE INDEX IF NOT EXISTS idx_events_metadata ON bitacoras.events USING GIN(metadata)")
|
|
|
|
# Enable RLS
|
|
db.instance_variable_get(:@connection).exec("ALTER TABLE bitacoras.events ENABLE ROW LEVEL SECURITY")
|
|
|
|
# Policies
|
|
db.instance_variable_get(:@connection).exec("DROP POLICY IF EXISTS events_insert ON bitacoras.events")
|
|
db.instance_variable_get(:@connection).exec("CREATE POLICY events_insert ON bitacoras.events FOR INSERT WITH CHECK (true)")
|
|
|
|
db.instance_variable_get(:@connection).exec("DROP POLICY IF EXISTS events_select ON bitacoras.events")
|
|
db.instance_variable_get(:@connection).exec("CREATE POLICY events_select ON bitacoras.events FOR SELECT USING (true)")
|
|
|
|
db.instance_variable_get(:@connection).exec("DROP POLICY IF EXISTS events_update ON bitacoras.events")
|
|
db.instance_variable_get(:@connection).exec("CREATE POLICY events_update ON bitacoras.events FOR UPDATE USING (true)")
|
|
end
|
|
puts "✅ Tabla bitacoras.events creada"
|
|
end
|
|
|
|
def self.down
|
|
BitacorasDB::BitacoraDB.with_connection do |db|
|
|
db.instance_variable_get(:@connection).exec("DROP TABLE IF EXISTS bitacoras.events CASCADE")
|
|
end
|
|
puts "✅ Tabla bitacoras.events eliminada"
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
if __FILE__ == $PROGRAM_NAME
|
|
BitacorasDB::Migrations::CreateEventsTable.up
|
|
end
|