-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·146 lines (131 loc) · 3.71 KB
/
main.js
File metadata and controls
executable file
·146 lines (131 loc) · 3.71 KB
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
#!/usr/bin/env node
const CLI = require("./src/CLI").CLI;
const ConfigHandler = require("./src/ConfigHandler").ConfigHandler;
const PgDiffApi = require("pg-diff-api").PgDiff;
const chalk = require("chalk");
const { Progress } = require("clui");
const stdout = require("readline");
const pjson = require("./package.json");
const log = console.log;
//pgTypes.setTypeParser(1114, value => new Date(Date.parse(`${value}+0000`)));
CLI.PrintIntro(pjson);
Run()
.then(() => {
process.exitCode = 0;
process.exit();
})
.catch((err) => {
HandleError(err);
process.exitCode = -1;
process.exit();
});
/**
*
* @param {Error} e
*/
function HandleError(e) {
log();
log(chalk.red(e.stack));
switch (e.code) {
case "MODULE_NOT_FOUND":
log(chalk.red('Please create the configuration file "pg-diff-config1.json" in the same folder where you run pg-diff!'));
break;
}
}
/**
* Run the tool
*/
async function Run() {
var args = process.argv.slice(2);
if (args.length <= 0) {
log(chalk.red("Missing arguments!"));
CLI.PrintHelp();
process.exit();
}
switch (args[0]) {
case "-h":
case "--help": {
CLI.PrintHelp();
process.exit();
}
// falls through
case "-c":
case "--compare":
{
if (args.length != 3) {
log(chalk.red("Missing arguments!"));
CLI.PrintHelp();
process.exit();
}
let config = ConfigHandler.LoadConfig(args[1]);
ConfigHandler.ValidateCompareConfig(config);
CLI.PrintOptions(config);
let progressBar = new Progress(20);
let pgDiff = new PgDiffApi(config);
pgDiff.events.on("compare", function (message, percentage) {
stdout.clearLine(process.stdout);
stdout.cursorTo(process.stdout, 0);
process.stdout.write(progressBar.update(percentage / 100) + " - " + chalk.whiteBright(message));
});
let scriptFilePath = await pgDiff.compare(args[2]);
log();
if (scriptFilePath) log(chalk.whiteBright("SQL patch file has been created succesfully at: ") + chalk.green(scriptFilePath));
else log(chalk.yellow("No patch has been created because no differences have been found!"));
}
break;
case "-ms":
case "--migrate-to-source":
case "-mt":
case "--migrate-to-target":
{
if (args.length != 2) {
log(chalk.red("Missing arguments!"));
CLI.PrintHelp();
process.exit();
}
let toSourceClient = false;
if (args[0] == "-ms" || args[0] == "--migrate-to-source") toSourceClient = true;
let config = ConfigHandler.LoadConfig(args[1]);
ConfigHandler.ValidateMigrationConfig(config);
CLI.PrintOptions(config);
let progressBar = new Progress(20);
let pgDiff = new PgDiffApi(config);
pgDiff.events.on("migrate", function (message, percentage) {
stdout.clearLine(process.stdout);
stdout.cursorTo(process.stdout, 0);
process.stdout.write(progressBar.update(percentage / 100) + " - " + chalk.whiteBright(message));
});
let patches = await pgDiff.migrate(true, toSourceClient);
log();
if (patches.length <= 0) log(chalk.yellow("No new db patches have been found."));
else {
log(chalk.green("Following db patches have been applied:"));
for (let patch of patches) {
log(chalk.green(`- Patch "${patch.name}" version "${patch.version}"`));
}
}
}
break;
case "-s":
case "--save":
{
if (args.length != 3) {
log(chalk.red("Missing arguments!"));
CLI.PrintHelp();
process.exit();
}
let config = ConfigHandler.LoadConfig(args[1]);
CLI.PrintOptions(config);
let pgDiff = new PgDiffApi(config);
await pgDiff.save(args[2]);
log();
log(chalk.green(`Patch ${args[2]} has been saved!`));
}
break;
default: {
log(chalk.red("Missing arguments!"));
CLI.PrintHelp();
process.exit();
}
}
}