forked from m3talsmith/pg-migrator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrate.js
87 lines (67 loc) · 2.54 KB
/
migrate.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
"use strict";
var fs = require("fs");
var path = require("path");
var colors = require("colors/safe");
var Pgb = require("pg-bluebird");
var messages = require("./lib/infrastructure/messages");
colors.setTheme({
verbose: 'cyan',
info: 'green',
warn: 'yellow',
error: 'red'
});
function migrate (options) {
var connectionString = options.connectionString;
var targetVersion = options.targetVersion || 0;
var currentPath = options.path || '.';
var connection;
var currentPersister;
var currentVersion;
var persisterProvider = new Pgb();
// Connecting to PostgreSQL
return persisterProvider.connect(connectionString)
.then(function (persister) {
connection = persister;
currentPersister = persister.client;
// Starting transaction
return currentPersister.query("BEGIN TRANSACTION");
})
.then(function () {
return getMigrationService(currentPersister).migrate(currentPath, targetVersion);
})
.then(function (curVer) {
currentVersion = curVer;
// Migration has been completed successfully, committing the transaction
return currentPersister.query("COMMIT");
})
.then(function () {
connection.done();
console.log(colors.info("--------------------------------------------------"));
console.log(colors.info(messages.MIGRATION_COMPLETED + currentVersion));
// process.exit(0);
})
.catch(function (error) {
// Migration failed
if (error) {
console.error(colors.error(messages.MIGRATION_ERROR + error));
}
if (connection) {
connection.done();
}
// Rethrow error
throw error;
});
}
var getMigrationService = function (persister) {
var MigratiorService = require("./lib/application/service/migrator-service");
var ScriptService = require("./lib/domain/service/script-service");
var VersionService = require("./lib/domain/service/version-service");
var ScriptRepository = require("./lib/domain/repository/script-repository");
var VersionRepository = require("./lib/domain/repository/version-repository");
// Service definition with dependency injection
return new MigratiorService(
new ScriptService(new ScriptRepository(fs, persister), path),
new VersionService(new VersionRepository(persister), messages),
messages);
};
module.exports = migrate;