1 /*jshint node:true, indent:2, curly:false, eqeqeq:true, immed:true, latedef:true, newcap:true, noarg:true,
2 regexp:true, undef:true, strict:true, trailing:true, white:true */
8 var async = require("async"),
9 exec = require('child_process').exec,
10 path = require('path'),
12 winston = require('winston'),
13 dataSource = require('../../../node-datasource/lib/ext/datasource').dataSource,
14 inspectDatabaseExtensions = require("./inspect_database").inspectDatabaseExtensions;
17 // Wipe out the database
18 // and load it from scratch using pg_restore something.backup unless
19 // we're building from source.
21 var initDatabase = function (spec, creds, callback) {
22 var databaseName = spec.database,
23 credsClone = JSON.parse(JSON.stringify(creds)),
24 dropDatabase = function (done) {
25 winston.info("Dropping database " + databaseName);
26 // the calls to drop and create the database need to be run against the database "postgres"
27 credsClone.database = "postgres";
28 dataSource.query("drop database if exists " + databaseName + ";", credsClone, done);
30 createDatabase = function (done) {
31 winston.info("Creating database " + databaseName);
32 dataSource.query("create database " + databaseName + " template template1;", credsClone, done);
34 buildSchema = function (done) {
35 var schemaPath = path.join(path.dirname(spec.source), "440_schema.sql");
36 winston.info("Building schema for database " + databaseName);
38 exec("psql -U " + creds.username + " -h " + creds.hostname + " --single-transaction -p " +
39 creds.port + " -d " + databaseName + " -f " + schemaPath,
40 {maxBuffer: 40000 * 1024 /* 200x default */}, done);
42 populateData = function (done) {
43 winston.info("Populating data for database " + databaseName + " from " + spec.source);
44 exec("psql -U " + creds.username + " -h " + creds.hostname + " --single-transaction -p " +
45 creds.port + " -d " + databaseName + " -f " + spec.source,
46 {maxBuffer: 40000 * 1024 /* 200x default */}, done);
48 // use exec to restore the backup. The alternative, reading the backup file into a string to query
49 // doesn't work because the backup file is binary.
50 restoreBackup = function (done) {
51 exec("pg_restore -U " + creds.username + " -h " + creds.hostname + " -p " +
52 creds.port + " -d " + databaseName + " -j " + os.cpus().length + " " + spec.backup, function (err, res) {
54 console.log("ignoring restore db error", err);
59 finish = function (err, results) {
61 winston.error("init database error", err.message, err.stack, err);
63 callback(err, results);
79 credsClone.database = databaseName;
80 inspectDatabaseExtensions(credsClone, function (err, paths) {
81 // in the case of a build-from-backup, we ignore any user desires and dictate the extensions
82 spec.extensions = paths;
90 exports.initDatabase = initDatabase;