From 3dcb91a1fc13e7fb37a89e465c35e32873809150 Mon Sep 17 00:00:00 2001 From: jramos Date: Thu, 2 Apr 2026 10:17:33 -0600 Subject: [PATCH] chore(migrations): add migration script for ivanti_counts_history table MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Standalone migration script for consistency with other files in backend/migrations/. The table is also created automatically at server startup via CREATE TABLE IF NOT EXISTS in initTables() so no manual step is required on the dev server — just restart the backend after pulling. Co-Authored-By: Claude Sonnet 4.6 --- .../add_ivanti_counts_history_table.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 backend/migrations/add_ivanti_counts_history_table.js diff --git a/backend/migrations/add_ivanti_counts_history_table.js b/backend/migrations/add_ivanti_counts_history_table.js new file mode 100644 index 0000000..f474525 --- /dev/null +++ b/backend/migrations/add_ivanti_counts_history_table.js @@ -0,0 +1,41 @@ +// Migration: Add ivanti_counts_history table +// +// Stores a snapshot of open/closed Ivanti finding counts on every sync. +// Unlike ivanti_counts_cache (single-row, always overwritten), this table +// accumulates all snapshots so time-series charts can be built from it. +// +// The GET /api/ivanti/findings/counts/history endpoint aggregates these rows +// to the last snapshot per calendar day using a ROW_NUMBER window function. +// +// NOTE: This table is also created automatically at server startup via +// CREATE TABLE IF NOT EXISTS in initTables() (ivantiFindings.js). +// This script is provided for manual setup on fresh installs and for +// documentation consistency with other migration files. +// +// Usage: node backend/migrations/add_ivanti_counts_history_table.js + +const sqlite3 = require('sqlite3').verbose(); +const path = require('path'); + +const dbPath = path.join(__dirname, '..', 'cve_database.db'); +const db = new sqlite3.Database(dbPath); + +console.log('Starting ivanti_counts_history migration...'); + +db.serialize(() => { + db.run(` + CREATE TABLE IF NOT EXISTS ivanti_counts_history ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + open_count INTEGER NOT NULL, + closed_count INTEGER NOT NULL, + recorded_at DATETIME DEFAULT CURRENT_TIMESTAMP + ) + `, (err) => { + if (err) console.error('Error creating ivanti_counts_history table:', err); + else console.log('✓ ivanti_counts_history table created (or already exists)'); + }); +}); + +db.close(() => { + console.log('Migration complete.'); +});