diff --git a/migrations/001_create_audit_logs.sql b/migrations/001_create_audit_logs.sql new file mode 100644 index 0000000..ee85c3e --- /dev/null +++ b/migrations/001_create_audit_logs.sql @@ -0,0 +1,23 @@ +-- Migration: Create audit_logs table +-- Description: Audit log untuk tracking semua perubahan data (create/update/delete) +-- Date: 2024-12-28 + +CREATE TABLE IF NOT EXISTS audit_logs ( + id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + actor_user_id INT UNSIGNED NOT NULL, + actor_username VARCHAR(100) NOT NULL, + actor_role VARCHAR(50) NOT NULL, + action VARCHAR(20) NOT NULL COMMENT 'create|update|delete', + entity VARCHAR(50) NOT NULL COMMENT 'locations|gates|tariffs', + entity_key VARCHAR(255) NOT NULL COMMENT 'Primary key atau composite key (e.g., "kerkof_01" or "kerkof_01:gate01")', + before_json JSON NULL COMMENT 'Data sebelum perubahan (untuk update/delete)', + after_json JSON NULL COMMENT 'Data sesudah perubahan (untuk create/update)', + ip_address VARCHAR(45) NOT NULL COMMENT 'IPv4 atau IPv6', + user_agent VARCHAR(500) NULL, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + INDEX idx_actor_user_id (actor_user_id), + INDEX idx_entity (entity, entity_key), + INDEX idx_action (action), + INDEX idx_created_at (created_at) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + diff --git a/migrations/002_create_hourly_summary.sql b/migrations/002_create_hourly_summary.sql new file mode 100644 index 0000000..4f573db --- /dev/null +++ b/migrations/002_create_hourly_summary.sql @@ -0,0 +1,19 @@ +-- Migration: Create hourly_summary table +-- Description: Rekap per jam untuk kebutuhan grafik dashboard +-- Date: 2024-12-28 + +CREATE TABLE IF NOT EXISTS hourly_summary ( + summary_date DATE NOT NULL, + summary_hour TINYINT UNSIGNED NOT NULL COMMENT '0-23', + location_code VARCHAR(64) NOT NULL, + gate_code VARCHAR(64) NOT NULL, + category VARCHAR(64) NOT NULL, + total_count INT UNSIGNED NOT NULL DEFAULT 0, + total_amount BIGINT UNSIGNED NOT NULL DEFAULT 0, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (summary_date, summary_hour, location_code, gate_code, category), + INDEX idx_summary_date_location (summary_date, location_code), + INDEX idx_summary_date_hour (summary_date, summary_hour) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + diff --git a/migrations/003_create_realtime_events.sql b/migrations/003_create_realtime_events.sql new file mode 100644 index 0000000..1126035 --- /dev/null +++ b/migrations/003_create_realtime_events.sql @@ -0,0 +1,19 @@ +-- Migration: Create realtime_events table +-- Description: Ring buffer untuk realtime stream & SSE events +-- Date: 2024-12-28 + +CREATE TABLE IF NOT EXISTS realtime_events ( + id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + location_code VARCHAR(64) NOT NULL, + gate_code VARCHAR(64) NOT NULL, + category VARCHAR(64) NOT NULL, + event_time INT UNSIGNED NOT NULL COMMENT 'Unix timestamp', + total_count_delta INT NOT NULL DEFAULT 1 COMMENT 'Delta count untuk event ini (biasanya 1)', + INDEX idx_created_at (created_at), + INDEX idx_location_gate (location_code, gate_code) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Optional: Cleanup old events (older than 7 days) via cron or manual +-- DELETE FROM realtime_events WHERE created_at < DATE_SUB(NOW(), INTERVAL 7 DAY); +