From 0abe65653b446d16c00b1140ec5bf6088e6f273c Mon Sep 17 00:00:00 2001 From: forehalo Date: Fri, 25 Apr 2025 03:51:18 +0000 Subject: [PATCH] chore(server): sort data migrations (#11979) ## Summary by CodeRabbit - **Bug Fixes** - Improved reliability of migration processing by ensuring migrations are now executed in a defined sequence based on numeric order extracted from their names. Errors will be shown if migration names do not include a valid numeric order suffix. --- packages/backend/server/src/data/commands/run.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/backend/server/src/data/commands/run.ts b/packages/backend/server/src/data/commands/run.ts index 5f318d40ff..80b00801b2 100644 --- a/packages/backend/server/src/data/commands/run.ts +++ b/packages/backend/server/src/data/commands/run.ts @@ -4,25 +4,35 @@ import { PrismaClient } from '@prisma/client'; import { once } from 'lodash-es'; import { Command, CommandRunner } from 'nest-commander'; -import * as migrations from '../migrations'; +import * as migrationImports from '../migrations'; interface Migration { name: string; always?: boolean; up: (db: PrismaClient, injector: ModuleRef) => Promise; down: (db: PrismaClient, injector: ModuleRef) => Promise; + order: number; } export const collectMigrations = once(() => { - return Object.values(migrations).map(migration => { + const migrations = Object.values(migrationImports).map(migration => { + const order = Number(migration.name.match(/([\d]+)$/)?.[1]); + + if (Number.isNaN(order)) { + throw new Error(`Invalid migration name: ${migration.name}`); + } + return { name: migration.name, // @ts-expect-error optional always: migration.always, up: migration.up, down: migration.down, + order, }; }) as Migration[]; + + return migrations.sort((a, b) => a.order - b.order); }); @Command({