-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
89 lines (69 loc) · 2.08 KB
/
index.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
var massive = require("massive");
var fs = require("fs");
var _ = require("underscore")._;
var async = require("async");
var srcDir = __dirname + "/build/src/";
var tableDir = srcDir + "tables/"
var indexDir = srcDir + "indexes/"
var functionDir = srcDir + "functions/"
var distFile = __dirname + "/build/dist/pg_auth.sql";
var DB = "pg_auth";
var functionSql = function(){
var sqlFiles = [];
var files = fs.readdirSync(functionDir);
_.each(files, function(file){
if(file.indexOf(".sql") > 0){
var sql = fs.readFileSync(functionDir + file, {encoding : "utf-8"});
sqlFiles.push(sql);
}
});
sqlFiles.push("select 'functions installed' as result;");
return sqlFiles.join("\r\n\r\n");
};
var tableSql = function(){
var sqlFiles = [];
var files = fs.readdirSync(tableDir);
_.each(files, function(file){
if(file.indexOf(".sql") > 0){
var sql =
sqlFiles.push(fs.readFileSync(tableDir + file, {encoding : "utf-8"}));
}
});
sqlFiles.push("select 'tables installed' as result;");
//add the foreign keys
var fks = fs.readFileSync(indexDir + "foreign_keys.sql", {encoding : "utf-8"});
sqlFiles.push(fks);
return sqlFiles.join("\r\n\r\n");
};
exports.build = function(){
var buildScript = [];
buildScript.push("-- built on " + new Date());
//start off the transaction
buildScript.push("BEGIN;");
//grab the init file
var initSql = fs.readFileSync(srcDir + "init.sql", {encoding : "utf-8"});
buildScript.push(initSql);
//tables
buildScript.push(tableSql());
//functions
buildScript.push(functionSql());
//end tx
buildScript.push("COMMIT;");
var sql = buildScript.join("\r\n\r\n");
//write it out
fs.writeFileSync(distFile,sql);
return sql;
};
exports.install = function(){
var self = this;
massive.connect({db : DB}, function(err,db){
var sql = self.build();
if(!db || !db.run) {
console.log("Unable to connect to database '" + DB + "'.");
console.log("Check that it exists and/or adjust connection settings in index.js");
return;
}
db.run(sql);
});
};
this.install();