ids.alfacom.it/database-schema/cleanup_old_logs.sql
marco370 661e945f57 Implement automatic database cleanup and schema updates
Adds scripts for automatic database log cleanup, schema migration application, and cron job setup. Modifies the update script to apply SQL migrations before pushing Drizzle schema.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: 9a659f15-d68a-4b7d-99f8-3eccc59afebe
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/4LjHWWz
2025-11-21 16:49:13 +00:00

40 lines
1.3 KiB
SQL

-- =============================================================================
-- IDS - Pulizia Automatica Log Vecchi
-- =============================================================================
-- Mantiene solo gli ultimi 7 giorni di network_logs
-- Esegui giornalmente via cron: psql $DATABASE_URL < cleanup_old_logs.sql
-- =============================================================================
-- Conta log prima della pulizia
DO $$
DECLARE
total_count bigint;
old_count bigint;
BEGIN
SELECT COUNT(*) INTO total_count FROM network_logs;
SELECT COUNT(*) INTO old_count FROM network_logs WHERE timestamp < NOW() - INTERVAL '7 days';
RAISE NOTICE 'Log totali: %', total_count;
RAISE NOTICE 'Log da eliminare (>7 giorni): %', old_count;
END $$;
-- Elimina log più vecchi di 7 giorni
DELETE FROM network_logs
WHERE timestamp < NOW() - INTERVAL '7 days';
-- Vacuum per liberare spazio fisico
VACUUM ANALYZE network_logs;
-- Conta log dopo pulizia
DO $$
DECLARE
remaining_count bigint;
db_size text;
BEGIN
SELECT COUNT(*) INTO remaining_count FROM network_logs;
SELECT pg_size_pretty(pg_database_size(current_database())) INTO db_size;
RAISE NOTICE 'Log rimanenti: %', remaining_count;
RAISE NOTICE 'Dimensione database: %', db_size;
END $$;