-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
109 lines (89 loc) · 2.4 KB
/
helpers.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
var _ = require('lodash');
var fs = require('fs');
var util = require('util');
var path = require('path');
var orm = require('orm');
var rimraf = require('rimraf');
var async = require('async');
var aliases = {
postgres: 'postgresql'
};
var ciConfig = {
mysql: {
protocol : 'mysql',
user : 'mysql',
password : 'mysql',
query : { pool: true },
database : 'mysql',
host : 'localhost'
},
postgresql : {
protocol : 'postgresql',
user : 'postgres',
password : 'postgres',
query : { pool: true },
database : 'postgres',
host : 'localhost'
}
};
module.exports = {
isCI: function () {
return Boolean(process.env.CI);
},
config: function () {
if (this.isCI()) {
return ciConfig;
} else {
return require('./config');
}
},
protocol: function () {
var pr = process.env.ORM_PROTOCOL;
return aliases[pr] || pr
},
connect: function (cb) {
var config = this.config();
var protocol = this.protocol();
if (!(protocol in config)) {
var emsg = "";
if (!protocol) {
emsg = "No protocol specified. Specify using: ORM_PROTOCOL=mysql mocha test/integration"
} else {
emsg = "Protocol '" + protocol + "' missing in config.js"
}
return cb(new Error(emsg));
}
// leaving this here for debugging.
// orm.settings.set("connection.debug", true);
orm.connect(config[protocol], function (err, connection) {
if (err) return cb(err);
cb(null, connection.driver);
});
},
writeMigration: function (task, name, code) {
var filePath = util.format(
"%s/%s/%s", path.normalize(path.join(__dirname, '..')), task.dir, name
);
// Because we have different migration files with the same path.
delete require.cache[filePath];
fs.writeFileSync(filePath, code);
},
cleanupDir: function (folder, cb) {
rimraf(path.join(process.cwd() ,folder), cb);
},
cleanupDb: function (conn, tables, cb) {
tables.reverse();
tables.push("orm_migrations");
tables.reverse();
var dropper = function(table, cb) {
conn.execQuery('DROP TABLE IF EXISTS ??', [table], cb);
}
async.eachSeries(tables, dropper, cb);
},
cleanupDbAndDir: function (conn, folder, tables, cb) {
async.series([
this.cleanupDir.bind(this, folder),
this.cleanupDb.bind(this, conn, tables)
], cb);
}
};