Skip to content

Commit d3ade70

Browse files
committed
fix(db): push-migration picks newest SQL, not oldest
The default-case resolver used sort()[0], which returned the first migration (0000_*) instead of the latest. Use sort().at(-1) and tighten the glob to the NNNN_ prefix so only real migrations are considered.
1 parent 497924b commit d3ade70

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

shared/db/scripts/push-migration.mjs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ if (!url) {
1717
process.exit(1);
1818
}
1919

20-
// Resolve the migration file — default to the newest 0000_*.sql in migrations/.
20+
// Resolve the migration file — default to the newest NNNN_*.sql in migrations/.
2121
const migrationsDir = 'shared/db/migrations';
2222
const explicit = process.argv[2];
23-
const migrationPath =
24-
explicit ??
25-
join(
26-
migrationsDir,
27-
readdirSync(migrationsDir)
28-
.filter((f) => f.endsWith('.sql'))
29-
.sort()[0],
30-
);
23+
const latest = readdirSync(migrationsDir)
24+
.filter((f) => /^\d{4}_.*\.sql$/.test(f))
25+
.sort()
26+
.at(-1);
27+
if (!explicit && !latest) {
28+
console.error(`no migration files found in ${migrationsDir}`);
29+
process.exit(1);
30+
}
31+
const migrationPath = explicit ?? join(migrationsDir, latest);
3132

3233
console.log(`→ applying ${migrationPath}`);
3334
const raw = readFileSync(migrationPath, 'utf-8');

0 commit comments

Comments
 (0)