Mbonea Claude Sonnet 4.6 commited on
Commit
70a2f38
·
1 Parent(s): 1e8a248

Add migration runner that executes before server start

Browse files

Migrations are idempotent — errno 1060 (duplicate column) is silently
skipped so they are safe to run on every boot.
First migration adds ssid_name, ssid_password, omada_wlan_id, omada_ssid_id
to the devices table for existing installations.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Files changed (2) hide show
  1. src/server.js +11 -2
  2. src/utils/migrate.js +36 -0
src/server.js CHANGED
@@ -3,11 +3,20 @@ require('dotenv').config();
3
  const app = require('./app');
4
  const omada = require('./services/omada');
5
  const { initJobs } = require('./jobs');
 
6
 
7
  const PORT = process.env.PORT || 3000;
8
 
9
  async function start() {
10
- // 1. Boot Omada connection
 
 
 
 
 
 
 
 
11
  try {
12
  await omada.initOmada();
13
  } catch (err) {
@@ -15,7 +24,7 @@ async function start() {
15
  console.warn('[Boot] Continuing without Omada — check OMADA_URL / credentials');
16
  }
17
 
18
- // 2. Start background jobs
19
  initJobs();
20
 
21
  // 3. Listen
 
3
  const app = require('./app');
4
  const omada = require('./services/omada');
5
  const { initJobs } = require('./jobs');
6
+ const { runMigrations } = require('./utils/migrate');
7
 
8
  const PORT = process.env.PORT || 3000;
9
 
10
  async function start() {
11
+ // 1. Run DB migrations
12
+ try {
13
+ await runMigrations();
14
+ } catch (err) {
15
+ console.error('[Boot] Migration failed — aborting:', err.message);
16
+ process.exit(1);
17
+ }
18
+
19
+ // 2. Boot Omada connection
20
  try {
21
  await omada.initOmada();
22
  } catch (err) {
 
24
  console.warn('[Boot] Continuing without Omada — check OMADA_URL / credentials');
25
  }
26
 
27
+ // 3. Start background jobs
28
  initJobs();
29
 
30
  // 3. Listen
src/utils/migrate.js ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Safe schema migrations — run before server start.
3
+ * Each ALTER TABLE is caught on errno 1060 (duplicate column) so
4
+ * it is safe to run on every boot without wiping data.
5
+ */
6
+ const db = require('../config/db');
7
+
8
+ const migrations = [
9
+ // 001 — SSID / WLAN fields on devices
10
+ `ALTER TABLE devices ADD COLUMN ssid_name VARCHAR(64) DEFAULT NULL AFTER device_password`,
11
+ `ALTER TABLE devices ADD COLUMN ssid_password VARCHAR(64) DEFAULT NULL AFTER ssid_name`,
12
+ `ALTER TABLE devices ADD COLUMN omada_wlan_id VARCHAR(64) DEFAULT NULL AFTER omada_portal_id`,
13
+ `ALTER TABLE devices ADD COLUMN omada_ssid_id VARCHAR(64) DEFAULT NULL AFTER omada_wlan_id`,
14
+ ];
15
+
16
+ async function runMigrations() {
17
+ console.log('[migrate] Running schema migrations...');
18
+ for (const sql of migrations) {
19
+ try {
20
+ await db.query(sql);
21
+ // Extract column name from the SQL for a readable log line
22
+ const col = sql.match(/ADD COLUMN (\w+)/i)?.[1] ?? sql.slice(0, 60);
23
+ console.log(`[migrate] Applied: ${col}`);
24
+ } catch (err) {
25
+ if (err.errno === 1060) {
26
+ // Column already exists — nothing to do
27
+ } else {
28
+ console.error('[migrate] Migration failed:', err.message);
29
+ throw err;
30
+ }
31
+ }
32
+ }
33
+ console.log('[migrate] Done.');
34
+ }
35
+
36
+ module.exports = { runMigrations };