-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgmigrations.js
347 lines (290 loc) · 11.4 KB
/
pgmigrations.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const mainConfig = require("./config.js");
const { error, warning, info, sections } = require("./log.js");
const { command: commandRunner, schema, psql } = require("./runner.js");
const {migrate, history} = require("./migration.js");
const tests = require("./tests.js");
var defaultconfigFile = "./db.js";
var defaultconfigFile2 = "./pgmigrations.js";
var args = process.argv.slice(2);
var cmd = args[0];
var userConfigs = [defaultconfigFile, defaultconfigFile2];
const envRegex = /\$\{([^}]+)\}/g;
function buildConfig(opt) {
for (var i = 0; i < userConfigs.length; i++) {
var configFile = path.join(process.cwd(), userConfigs[i]);
if (fs.existsSync(configFile) && fs.lstatSync(configFile).isFile()) {
if (opt.verbose) {
console.log("Using default config file: " + configFile);
}
var config = require(configFile);
for (var key in config) {
if (mainConfig[key] === undefined) {
error("Unknown config key: " + key +". Please provide a valid config key.");
return {};
}
mainConfig[key] = config[key];
}
} else if (userConfigs[i] !== defaultconfigFile && userConfigs[i] !== defaultconfigFile2) {
warning("Config file not found: " + configFile + ". Please provide a valid config file.");
}
}
if (mainConfig.env) {
if (typeof mainConfig.env === "string") {
require('dotenv').config({
path: path.resolve(__dirname, mainConfig.env)
});
} else {
require('dotenv').config();
}
}
for (var key of Object.keys(mainConfig)) {
var value = mainConfig[key];
if (typeof value === "string") {
value = value.replace(envRegex, function(match, regkey) {
return process.env[regkey] !== undefined ? process.env[regkey] : match;
});
mainConfig[key] = value;
}
}
return mainConfig || {};
}
if (!cmd) {
error("Command is required. Please provide a valid command.");
return;
}
cmd = cmd.toLowerCase();
if (!cmd || cmd === 'help' || cmd === '-h' || cmd === '--help') {
console.log('Usage:');
info('pgmigrations [command] [switches]');
console.log('\nCommands:');
sections([
{key: "up", value: "Run migrations migrations in order: before, before repeatable, up, repeatable, after. Optional switches: --list, --dry, --full, --dump."},
{key: "down", value: "Run only down migrations. Optional switches: --list, --dry, --full, --dump."},
{key: "history", value: "console.log the current migration schema history."},
{key: "run | exec", value: "Run a command or a script file or script directory with psql. Command text or a script file is required as the second argument. Any additional arguments will be passed to a psql command."},
{key: "dump | schema", value: "Run pg_dump command with --schema-only --encoding=UTF8 swtiches on (plus schemaDumpAdditionalArgs from the config). Any additional arguments will be passed to pg_dump command."},
{key: "psql", value: "Run arbitrary psql command or open psql shell. Any additional arguments will be passed to a psql."},
{key: "test", value: "Run database tests."},
{key: "config", value: "console.log the current configuration."},
], 16);
console.log('\nSwitches:');
sections([
{key: "-h, --help", value: "Show help"},
{key: "--list", value: "List available migrations in this direction (up or down) or list available database tests."},
{key: "--dry", value: "Run in the migrations dry run mode on database in this direction (up or down). No changes will be made to the database (rollbacks changes)."},
{key: "--full", value: "Executes all migrations in this direction (up or down). Schema history will be ignored."},
{key: "--dump", value: "Dump the SQL for the migration to the console instead of executing it."},
{key: "--verbose", value: "Run in verbose mode. This switch applies to all commands. Default is false."}
], 16);
console.log('\nConfigurations files:');
sections([
{key: '', value: './db.js or ./pgmigrations.js from the current dir is the default configuration file. It will be ignored if not found.'},
{key: '--config=[file]', value: 'Set the custom config file or multiple files (multiple --config switches). Config files are merged in the order they are provided.'}
], 16);
console.log();
return;
}
const options = args.slice(1);
let verbose = false;
if (cmd == "up" || cmd == "down") {
let list = false;
let dry = false;
let full = false;
let dump = false;
for (let i = 0; i < options.length; i++) {
let opt = options[i];
if (opt.startsWith("-")) {
if (opt == "--list") {
list = true;
} else if (opt == "--dry") {
dry = true;
} else if (opt == "--full") {
full = true;
} else if (opt == "--dump") {
dump = true;
} else if (opt == "--verbose") {
verbose = true;
} else if (opt.startsWith("--config")) {
let parts = opt.split("=");
if (parts.length <= 1) {
error("Config file is required. Please provide a valid config file.");
return;
}
userConfigs.push(parts[1]);
} else {
error("Unknown option: " + opt + ". Please provide a valid option");
return;
}
}
}
if (list && (dry || dump)) {
error("List option is not allowed with dry or dump. Please provide a valid option.");
return;
}
const opt = {list, dry, dump, full, verbose};
const config = buildConfig(opt);
opt.verbose = opt.verbose || config.verbose;
migrate(cmd, opt, config);
} else if (cmd == "run" || cmd == "exec") {
let command;
let additionalArgs = [];
for (let i = 0; i < options.length; i++) {
let opt = options[i];
if (opt.startsWith("-")) {
if (opt == "--verbose") {
verbose = true;
} else if (opt.startsWith("--config")) {
let parts = opt.split("=");
if (parts.length <= 1) {
error("Config file is required. Please provide a valid config file.");
return;
}
userConfigs.push(parts[1]);
} else {
additionalArgs.push(opt);
}
}
else {
if (!command) {
command = opt;
} else {
additionalArgs.push(opt);
}
}
}
if (!command && additionalArgs.indexOf("--help") === -1) {
error("Command is required. Please provide a command to run.");
return;
}
const opt = {verbose};
const config = buildConfig(opt);
opt.verbose = opt.verbose || config.verbose;
commandRunner(command, opt, additionalArgs, config);
} else if (cmd == "dump" || cmd == "schema") {
let additionalArgs = [];
for (let i = 0; i < options.length; i++) {
let opt = options[i];
if (opt.startsWith("-")) {
if (opt == "--verbose") {
verbose = true;
} else if (opt.startsWith("--config")) {
let parts = opt.split("=");
if (parts.length <= 1) {
error("Config file is required. Please provide a valid config file.");
return;
}
userConfigs.push(parts[1]);
} else {
additionalArgs.push(opt);
}
}
else {
additionalArgs.push(opt);
}
}
const opt = {verbose};
const config = buildConfig(opt);
opt.verbose = opt.verbose || config.verbose;
schema(opt, additionalArgs, config);
} else if (cmd == "psql") {
let additionalArgs = [];
for (let i = 0; i < options.length; i++) {
let opt = options[i];
if (opt.startsWith("-")) {
if (opt == "--verbose") {
verbose = true;
} else if (opt.startsWith("--config")) {
let parts = opt.split("=");
if (parts.length <= 1) {
error("Config file is required. Please provide a valid config file.");
return;
}
userConfigs.push(parts[1]);
} else {
additionalArgs.push(opt);
}
}
else {
additionalArgs.push(opt);
}
}
const opt = {verbose};
const config = buildConfig(opt);
opt.verbose = opt.verbose || config.verbose;
psql(opt, additionalArgs, config);
} else if (cmd == "test") {
let list = false;
for (let i = 0; i < options.length; i++) {
let opt = options[i];
if (opt.startsWith("-")) {
if (opt == "--list") {
list = true;
} else if (opt == "--verbose") {
verbose = true;
} else if (opt.startsWith("--config")) {
let parts = opt.split("=");
if (parts.length <= 1) {
error("Config file is required. Please provide a valid config file.");
return;
}
userConfigs.push(parts[1]);
} else {
error("Unknown option: " + opt + ". Please provide a valid option");
return;
}
}
}
const opt = {list, verbose};
const config = buildConfig(opt);
opt.verbose = opt.verbose || config.verbose;
tests(opt, config);
} else if (cmd == "config") {
for (let i = 0; i < options.length; i++) {
let opt = options[i];
if (opt.startsWith("-")) {
if (opt == "--verbose") {
verbose = true;
} else if (opt.startsWith("--config")) {
let parts = opt.split("=");
if (parts.length <= 1) {
error("Config file is required. Please provide a valid config file.");
return;
}
userConfigs.push(parts[1]);
} else {
error("Unknown option: " + opt + ". Please provide a valid option");
return;
}
}
}
const config = buildConfig({verbose});
console.log(config);
} else if (cmd == "history") {
for (let i = 0; i < options.length; i++) {
let opt = options[i];
if (opt.startsWith("-")) {
if (opt == "--verbose") {
verbose = true;
} else if (opt.startsWith("--config")) {
let parts = opt.split("=");
if (parts.length <= 1) {
error("Config file is required. Please provide a valid config file.");
return;
}
userConfigs.push(parts[1]);
} else {
error("Unknown option: " + opt + ". Please provide a valid option");
return;
}
}
}
const config = buildConfig({verbose});
history({verbose}, config);
} else {
error("Unknown command: " + cmd + ". Please provide a valid command.");
return;
}