This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
migrator
executable file
·123 lines (106 loc) · 2.96 KB
/
migrator
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env node
/**
* Run me as if I were a bash program eg: `./bin/migrator up [name]` or `./bin/migrator down <name>}`
* I run the **compiled** migrations (eg: dist). You may want to `npm run watch` for changes as you run me.
*/
const path = require("path");
const fs = require("fs");
const { Migrations } = require("ah-sequelize-plugin/dist/modules/migrations");
const { Sequelize } = require("sequelize");
const args = process.argv.slice(2);
function validatePath() {
if (!process.env.INIT_CWD) process.env.INIT_CWD = process.env.PWD;
const relativeCorePath = path.join(
process.env.INIT_CWD,
"node_modules",
"@grouparoo",
"core"
);
if (fs.existsSync(relativeCorePath)) {
console.info(`running migrator in ${process.env.INIT_CWD}\r\n`);
process.chdir(relativeCorePath);
} else {
console.info(`running migrator in core\r\n`);
}
}
async function main() {
let failureReason;
if (args.length === 0 || args[0] === "help") {
console.log(`@grouparoo/core migrator.
usage:
\`./node_modules/@grouparoo/core/bin/migrator <mode> [migrationName]\`
Use me from within your Grouparoo application directory
modes:
* up
* upAll
* down
* downAll`);
process.exit(1);
}
try {
validatePath();
const env = process.env.NODE_ENV || "development";
const configFile = path.resolve(
__dirname,
"..",
"dist",
"config",
"sequelize.js"
);
const CONFIG = Object.assign({}, require(configFile)[env], {
autoMigrate: false,
logging: logSequelizeQuery,
retry: undefined,
});
const sequelizeInstance = new Sequelize(CONFIG);
const umzugs = await Migrations.importMigrationsFromDirectory(
CONFIG,
sequelizeInstance,
logger,
"info"
);
await sequelizeInstance.transaction(async () => {
switch (args[0]) {
case "up": {
await Migrations.upOne(umzugs);
break;
}
case "upAll": {
await Migrations.upAll(umzugs);
break;
}
case "down": {
if (!args[1]) throw new Error(`migration name is required`);
await Migrations.downOne(umzugs, args[1]);
break;
}
case "downAll": {
await Migrations.downAll(umzugs);
break;
}
default: {
throw new Error(`I cannot preform a ${args[0]} migration`);
}
}
});
} catch (error) {
failureReason = error.stack;
}
completeProcess(failureReason);
}
function logger(message, level) {
console.log(message);
}
function logSequelizeQuery(string) {
if (string.includes("SequelizeMeta")) return; // skip all the boilerplate storage logs
console.log(string);
}
function completeProcess(failureReason) {
let exitCode = 0;
if (failureReason) exitCode = 1;
!failureReason
? console.log("✅ Done")
: console.error(`❌ ${failureReason || "Something went wrong"}`);
process.nextTick(() => process.exit(exitCode));
}
main();