From 7c0ba41514245a7e11affb8c6ee231b53f864bf9 Mon Sep 17 00:00:00 2001 From: jramos Date: Thu, 2 Apr 2026 15:23:38 -0600 Subject: [PATCH] fix(migrations): add created_at/updated_at to archer_tickets if missing Production instances where the table was created before these columns were added to the schema will see 500 errors on all /api/archer-tickets endpoints. This migration safely checks PRAGMA table_info before each ALTER TABLE so it is idempotent and safe to run multiple times. Co-Authored-By: Claude Sonnet 4.6 --- .../add_archer_tickets_timestamps.js | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 backend/migrations/add_archer_tickets_timestamps.js diff --git a/backend/migrations/add_archer_tickets_timestamps.js b/backend/migrations/add_archer_tickets_timestamps.js new file mode 100644 index 0000000..f248e18 --- /dev/null +++ b/backend/migrations/add_archer_tickets_timestamps.js @@ -0,0 +1,56 @@ +// Migration: Add created_at / updated_at columns to archer_tickets +// +// SQLite does not support ALTER TABLE ADD COLUMN IF NOT EXISTS, so we check +// PRAGMA table_info first and only add the column when it is absent. +// +// Run on any instance where archer_tickets was created before these columns +// were added to the schema (symptoms: every /api/archer-tickets call → 500). +// +// Usage: node backend/migrations/add_archer_tickets_timestamps.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 archer_tickets timestamp migration...'); + +db.all('PRAGMA table_info(archer_tickets)', [], (err, columns) => { + if (err) { + console.error('Error reading table info:', err); + return db.close(); + } + + const names = columns.map(c => c.name); + + db.serialize(() => { + if (!names.includes('created_at')) { + db.run( + `ALTER TABLE archer_tickets ADD COLUMN created_at DATETIME DEFAULT CURRENT_TIMESTAMP`, + (err) => { + if (err) console.error('Error adding created_at:', err); + else console.log('✓ created_at column added'); + } + ); + } else { + console.log('✓ created_at already exists — skipping'); + } + + if (!names.includes('updated_at')) { + db.run( + `ALTER TABLE archer_tickets ADD COLUMN updated_at DATETIME DEFAULT CURRENT_TIMESTAMP`, + (err) => { + if (err) console.error('Error adding updated_at:', err); + else console.log('✓ updated_at column added'); + } + ); + } else { + console.log('✓ updated_at already exists — skipping'); + } + }); + + db.close(() => { + console.log('Migration complete. Restart the backend server.'); + }); +});