db.js
3.46 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
/*jslint node: true, nomen: true, stupid: true */
"use strict";
var fs = require('fs');
var path = require('path');
var pg = require('pg');
var async = require('async');
var lodash = require('lodash');
var dbConfig = { host: 'localhost', port: 5432, user: 'user', password: 'password', database: 'pg_generator_test_625393' };
var generator = require('../../lib/index.js');
var conString = 'postgres://' + dbConfig.user + ':' + dbConfig.password + '@' + dbConfig.host + ':' + dbConfig.port + '/'; //'postgres://user:pass@host:port/'
var conStringTest = conString + dbConfig.database; //'postgres://user:pass@host:port/db'
var conStringTemplate = conString + 'template1'; //'postgres://user:pass@host:port/db'
var sql = {
createDB : "CREATE DATABASE pg_generator_test_625393 WITH ENCODING = 'UTF8' TEMPLATE = template0;",
dropDB : "DROP DATABASE IF EXISTS pg_generator_test_625393;",
createSchema : function (sqlID) { return fs.readFileSync(path.join(__dirname, 'create-test-db-' + sqlID + '.sql')).toString(); },
dropConnection : "SELECT pg_terminate_backend(pid) FROM pg_stat_activity where datname='pg_generator_test_625393';"
};
pg.on('error', function (err) {
// Do nothing on termination due to admin command. We do this to drop previously created test db.
if (!err.message.match('terminating connection due to administrator command')) { console.log('Database error!', err); }
});
module.exports.dbConfig = dbConfig;
module.exports.generate = function generate(sqlId, options, callback) {
module.exports.resetDB(sqlId, function () {
generator(function (err) {
if (err) { callback(err); return; }
callback();
}, lodash.defaults(options, {
database: 'pg_generator_test_625393',
user: dbConfig.user,
password: dbConfig.password,
output: path.join(__dirname, '..', 'model'),
schema: ['public', 'other_schema'],
nolog: true
}));
});
};
module.exports.resetDB = function resetDB(sqlID, callback) {
if (sqlID === undefined) { sqlID = 1; }
var client = new pg.Client(conStringTemplate);
client.connect(function () {
async.series([
client.query.bind(client, sql.dropConnection),
client.query.bind(client, sql.dropDB),
client.query.bind(client, sql.createDB),
function (next) {
var clientTest = new pg.Client(conStringTest);
clientTest.connect(function () {
clientTest.query(sql.createSchema(sqlID), function () {
clientTest.end();
next();
});
});
}
], function (err) {
if (err) { throw err; }
client.end();
callback();
});
});
};
module.exports.dropDB = function dropDB(callback) {
var client = new pg.Client(conStringTemplate);
client.connect(function () {
async.series([
client.query.bind(client, sql.dropConnection),
client.query.bind(client, sql.dropDB)
], function (err) {
if (err) { throw err; }
client.end();
callback();
});
});
};
module.exports.dbConfig = dbConfig;