Use umzug to autorun migrations (#5281)

This commit is contained in:
Apoorv Mishra
2023-05-31 05:42:38 +05:30
committed by GitHub
parent 5e76d72ae6
commit 7f8a177b01
4 changed files with 101 additions and 44 deletions

View File

@@ -1,4 +1,6 @@
import path from "path";
import { Sequelize } from "sequelize-typescript";
import { Umzug, SequelizeStorage, MigrationError } from "umzug";
import env from "@server/env";
import Logger from "../logging/Logger";
import * as models from "../models";
@@ -32,3 +34,39 @@ export const sequelize = new Sequelize(url, {
idle: 10000,
},
});
export const migrations = new Umzug({
migrations: {
glob: ["migrations/*.js", { cwd: path.resolve("server") }],
resolve: ({ name, path, context }) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const migration = require(path as string);
return {
name,
up: async () => migration.up(context, Sequelize),
down: async () => migration.down(context, Sequelize),
};
},
},
context: sequelize.getQueryInterface(),
storage: new SequelizeStorage({ sequelize }),
logger: {
warn: (params) => Logger.warn("database", params),
error: (params: Record<string, unknown> & MigrationError) =>
Logger.error(params.message, params),
info: (params) =>
Logger.info(
"database",
params.event === "migrating"
? `Migrating ${params.name}`
: `Migrated ${params.name} in ${params.durationSeconds}s`
),
debug: (params) =>
Logger.debug(
"database",
params.event === "migrating"
? `Migrating ${params.name}`
: `Migrated ${params.name} in ${params.durationSeconds}s`
),
},
});