chore(server): sort data migrations (#11979)

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## 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.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
forehalo
2025-04-25 03:51:18 +00:00
parent f0e2361e81
commit 0abe65653b

View File

@@ -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<void>;
down: (db: PrismaClient, injector: ModuleRef) => Promise<void>;
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({