mecano
Version:
Common functions for system deployment.
68 lines (65 loc) • 1.91 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var db;
module.exports = function(options) {
var k, ref, ref1, v;
if (options.db == null) {
options.db = {};
}
ref = options.db;
for (k in ref) {
v = ref[k];
if (options[k] == null) {
options[k] = v;
}
}
if (!options.host) {
throw Error('Missing option: "host"');
}
if (!options.admin_username) {
throw Error('Missing option: "admin_username"');
}
if (!options.admin_password) {
throw Error('Missing option: "admin_password"');
}
if (!options.database) {
throw Error('Missing option: "database"');
}
if (!options.engine) {
throw Error('Missing option: "engine"');
}
options.engine = options.engine.toLowerCase();
if ((ref1 = options.engine) !== 'postgres') {
throw Error("Unsupport engine: " + (JSON.stringify(options.engine)));
}
if (options.port == null) {
options.port = 5432;
}
this.execute({
code_skipped: 2,
cmd: db.cmd(options, '\\dt')
}, function(err, status, stdout, stderr) {
if (err) {
throw err;
}
if (!err && !status) {
throw Error("Database does not exist " + options.database);
}
});
this.execute({
cmd: db.cmd(options, "CREATE SCHEMA " + options.schema + ";"),
unless_exec: db.cmd(options, "SELECT 1 FROM pg_namespace WHERE nspname = '" + options.schema + "';") + " | grep 1"
});
return this.execute({
"if": function() {
return options.owner != null;
},
unless_exec: db.cmd(options, '\\dn') + (" | grep '" + options.schema + "|" + options.owner + "'"),
cmd: db.cmd(options, "ALTER SCHEMA " + options.schema + " OWNER TO " + options.owner + ";"),
code_skipped: 1
}, function(err, status, stdout, stderr) {
if (/^ERROR:\s\srole.*does\snot\sexist/.test(stderr)) {
throw Error("Owner " + options.owner + " does not exists");
}
});
};
db = require('../../misc/db');