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:
@@ -4,25 +4,35 @@ import { PrismaClient } from '@prisma/client';
|
|||||||
import { once } from 'lodash-es';
|
import { once } from 'lodash-es';
|
||||||
import { Command, CommandRunner } from 'nest-commander';
|
import { Command, CommandRunner } from 'nest-commander';
|
||||||
|
|
||||||
import * as migrations from '../migrations';
|
import * as migrationImports from '../migrations';
|
||||||
|
|
||||||
interface Migration {
|
interface Migration {
|
||||||
name: string;
|
name: string;
|
||||||
always?: boolean;
|
always?: boolean;
|
||||||
up: (db: PrismaClient, injector: ModuleRef) => Promise<void>;
|
up: (db: PrismaClient, injector: ModuleRef) => Promise<void>;
|
||||||
down: (db: PrismaClient, injector: ModuleRef) => Promise<void>;
|
down: (db: PrismaClient, injector: ModuleRef) => Promise<void>;
|
||||||
|
order: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const collectMigrations = once(() => {
|
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 {
|
return {
|
||||||
name: migration.name,
|
name: migration.name,
|
||||||
// @ts-expect-error optional
|
// @ts-expect-error optional
|
||||||
always: migration.always,
|
always: migration.always,
|
||||||
up: migration.up,
|
up: migration.up,
|
||||||
down: migration.down,
|
down: migration.down,
|
||||||
|
order,
|
||||||
};
|
};
|
||||||
}) as Migration[];
|
}) as Migration[];
|
||||||
|
|
||||||
|
return migrations.sort((a, b) => a.order - b.order);
|
||||||
});
|
});
|
||||||
|
|
||||||
@Command({
|
@Command({
|
||||||
|
|||||||
Reference in New Issue
Block a user