-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec-helper.js
50 lines (40 loc) · 1.27 KB
/
spec-helper.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
var fs = require('fs');
var path = require('path');
var knex = require('knex');
var knextancy;
var knexConnection;
var truncateTablesSQL;
exports.setup = function (options) {
var knexConfig = options.knexConfig;
var knextancyEnabled = options.knextancyEnabled || false;
var tenantId = options.tenantId || 1;
var truncateEnabled = options.truncateEnabled || false;
if (knextancyEnabled) {
knextancy = require('knextancy');
}
if (truncateEnabled && !truncateTablesSQL) {
var filename = 'spec-helper-' + knexConfig.client + '.sql';
truncateTablesSQL = fs.readFileSync(path.join(__dirname, filename), { encoding: 'utf8' });
}
if (!knexConnection) {
knexConnection = knex(knexConfig);
}
if (!truncateEnabled) {
return knexConnection;
}
return truncateAllTables(knexConnection, truncateTablesSQL).then(function () {
if (!knextancyEnabled) {
return knexConnection;
}
return knextancy.tenant(knexConnection, tenantId || 1);
});
};
function truncateAllTables () {
return knexConnection
.raw(truncateTablesSQL)
.then(function (sqlQuery) {
var rows = sqlQuery.rows || sqlQuery[0];
var query = rows.map(function (sql) { return sql.trucate_table_cmd; }).join('');
return knexConnection.raw(query);
});
}