-
Notifications
You must be signed in to change notification settings - Fork 1
/
migration.js
115 lines (115 loc) · 4.19 KB
/
migration.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
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const pg = require("pg");
const Sequelize = require("sequelize");
const Umzug = require("umzug");
const _ = pg;
class Migration {
constructor(url) {
this.url = url;
this.init();
}
init() {
let opts = {
pool: {
max: 1,
min: 0,
idle: 5000
}
};
this._sequelize = new Sequelize(this.url, opts);
this._umzug = new Umzug({
storage: 'sequelize',
storageOptions: {
sequelize: this.sequelize
},
migrations: {
params: [
this._sequelize.getQueryInterface(),
this._sequelize.constructor
]
},
logging: function () {
console.log.apply(null, arguments);
}
});
}
get sequelize() {
return this._sequelize;
}
get umzug() {
return this._umzug;
}
up() {
let results = this.umzug.up();
return results.map((m) => m.file);
}
down() {
let results = this.umzug.down();
return results.map((m) => m.file);
}
check() {
return __awaiter(this, void 0, void 0, function* () {
let disableConnections = 'SELECT 1 = 1;';
console.log('Checking connection to Database');
try {
yield this.sequelize.query(disableConnections);
}
catch (err) {
console.log('Error checking connection to Database');
console.log(err);
throw err;
}
});
}
drop(name) {
return __awaiter(this, void 0, void 0, function* () {
let disableConnections = `UPDATE pg_database SET datallowconn = false WHERE datname = '${name}'`;
let terminateBackend = `SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '${name}'`;
let revokeUser = `REVOKE ALL PRIVILEGES ON DATABASE ${name} FROM ${name};`;
let dropUser = `DROP ROLE ${name};`;
let dropDb = `DROP DATABASE ${name}`;
console.log(`Dropping Database and User: ${name}`);
try {
yield this.sequelize.query(disableConnections);
yield this.sequelize.query(terminateBackend);
yield this.sequelize.query(revokeUser);
yield this.sequelize.query(dropUser);
yield this.sequelize.query(dropDb);
}
catch (err) {
console.log(`Error Dropping Database and User: ${name}`);
console.log(err);
throw err;
}
});
}
create(name) {
return __awaiter(this, void 0, void 0, function* () {
let password = Math.random().toString(36).substring(2, 20);
let createDb = `CREATE DATABASE ${name}`;
let createUser = `CREATE ROLE ${name} WITH LOGIN PASSWORD '${password}' NOINHERIT`;
let grantUser = `GRANT ALL PRIVILEGES ON DATABASE ${name} TO ${name};`;
console.log(`Creating Database and User: ${name}`);
try {
yield this.sequelize.query(createDb);
yield this.sequelize.query(createUser);
yield this.sequelize.query(grantUser);
}
catch (err) {
console.log(`Error Creating Database and User: ${name}`);
throw err;
}
return password;
});
}
}
exports.Migration = Migration;