Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1import { DataTypes, type ModelOptions, type Sequelize } from 'sequelize';
2import { RunnableMigration, SequelizeStorage } from 'umzug';
3
4export function makeSequelizeUmzugStorage(
5 sequelize: Sequelize,
6 opts: ModelOptions,
7) {
8 return new SequelizeStorage({
9 sequelize,
10 model: sequelize.define(
11 'SequelizeMeta',
12 {
13 name: {
14 type: DataTypes.STRING,
15 allowNull: false,
16 unique: true,
17 primaryKey: true,
18 autoIncrement: false,
19 },
20 },
21 { timestamps: true, ...opts },
22 ),
23 });
24}
25
26export function wrapMigration<T>(
27 hooks: {
28 runBefore?: () => void | Promise<void>;
29 runAfter?: () => void | Promise<void>;
30 },
31 migration: RunnableMigration<T>,
32) {
33 return {
34 ...migration,
35 async up(params) {
36 await hooks.runBefore?.();
37 await migration.up(params);
38 await hooks.runAfter?.();
39 },
40 ...(migration.down
41 ? {
42 async down(params) {
43 await hooks.runBefore?.();
44 await migration.down!(params);
45 await hooks.runAfter?.();
46 },
47 }
48 : {}),
49 } satisfies RunnableMigration<T>;
50}