-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate.ts
48 lines (41 loc) · 1.47 KB
/
migrate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env ts-node-script
import { Sequelize } from 'sequelize';
import type { MigrationMeta } from 'umzug';
import { Umzug, SequelizeStorage } from 'umzug';
import { sequelizeConfig } from './src/db';
const sequelize = new Sequelize({ ...sequelizeConfig, logging: undefined });
const storageTableName = {
migration: { name: 'SequelizeMeta', path: './src/migrations/*.ts' },
seeder: { name: 'SequelizeData', path: './src/seeders/*.ts' },
} as const;
const getUmzug = (type: keyof typeof storageTableName) => {
return new Umzug({
logger: console,
migrations: {
glob: storageTableName[type].path,
},
context: { queryInterface: sequelize.getQueryInterface(), Sequelize },
storage: new SequelizeStorage({
sequelize,
modelName: storageTableName[type].name,
}),
});
};
const execute = async (fn: () => Promise<MigrationMeta[]>, msg: string) => {
fn()
.then((result) => {
console.log(
msg,
result.map((r) => r?.path ?? r)
);
process.exit();
})
.catch((err) => {
console.error(err);
process.exit(1);
});
};
export const seedUp = () => execute(() => getUmzug('seeder').up(), 'Executed seeds:');
export const seedDown = () => execute(() => getUmzug('seeder').down(), 'Reverted seeds:');
export const migrateUp = () => execute(() => getUmzug('migration').up(), 'Executed migrations:');
export const migrateDown = () => execute(() => getUmzug('migration').down(), 'Reverted migration:');