From 4e052966d34b9b1cbf62283e564b5784627167de Mon Sep 17 00:00:00 2001 From: paul Date: Fri, 25 Jul 2025 12:51:54 +0200 Subject: [PATCH] Fix migration sorting to use numeric comparison - Changed from string sort to numeric sort for migration files - String sort was causing '029' to run before '001' - Now properly extracts and compares numeric prefixes - Applied fix to both run-migrations.js and run-migrations-safe.js This ensures 001_init.js runs first and creates all necessary tables before other migrations try to use them. --- backend/migrations/run-migrations-safe.js | 8 +++++++- backend/migrations/run-migrations.js | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/backend/migrations/run-migrations-safe.js b/backend/migrations/run-migrations-safe.js index 50bfba2..51acda3 100644 --- a/backend/migrations/run-migrations-safe.js +++ b/backend/migrations/run-migrations-safe.js @@ -134,7 +134,13 @@ async function runMigrations() { migrationFiles = coreFiles .filter(f => f.match(/^\d{3}_.*\.js$/)) .map(f => path.join('core', f)) - .sort(); + .sort((a, b) => { + const baseA = path.basename(a); + const baseB = path.basename(b); + const numA = parseInt(baseA.split('_')[0]); + const numB = parseInt(baseB.split('_')[0]); + return numA - numB; + }); } else { // For existing deployments, run all migrations (legacy + core) console.log('Existing deployment detected - checking all migrations'); diff --git a/backend/migrations/run-migrations.js b/backend/migrations/run-migrations.js index 9f23b85..3ab7f7b 100644 --- a/backend/migrations/run-migrations.js +++ b/backend/migrations/run-migrations.js @@ -68,7 +68,13 @@ async function runMigrations() { migrationFiles = coreFiles .filter(f => f.match(/^\d{3}_.*\.js$/)) .map(f => path.join('core', f)) - .sort(); + .sort((a, b) => { + const baseA = path.basename(a); + const baseB = path.basename(b); + const numA = parseInt(baseA.split('_')[0]); + const numB = parseInt(baseB.split('_')[0]); + return numA - numB; + }); } else { // For existing deployments, run all migrations (legacy + core) console.log('Existing deployment detected - checking all migrations');