1
0
mirror of https://github.com/GOSTSec/gostexplr synced 2025-01-15 09:10:06 +00:00
gostexplr/bin/initdb.js
2018-02-03 21:14:56 +05:00

52 lines
1.7 KiB
JavaScript

#!/usr/bin/env node
var exec = require('child_process').exec;
var models = require('../models');
var env = process.env.NODE_ENV || 'development';
var config = require(__dirname + '/../config/config.json')[env];
if (process.argv.length < 4) {
console.log('Provide root user name and password for mysql');
process.exit(0);
}
const dropUserDB = `mysql -u${process.argv[2]} -p${process.argv[3]} -e "drop database ${config.database};drop user ${config.username}"`
const createdb = `mysql -u${process.argv[2]} -p${process.argv[3]} -e "create database ${config.database}"`;
const createUser = `mysql -u${process.argv[2]} -p${process.argv[3]} -e "create user ${config.username} identified by '${config.password}'"`;
const grantAccess = `mysql -u${process.argv[2]} -p${process.argv[3]} -e "grant all on ${config.database}.* to ${config.username}"`;
exec(dropUserDB, function(err,stdout,stderr) {
console.log(stdout);
exec(createdb, function(err,stdout,stderr) {
if (err) {
console.log(err);
process.exit(0);
} else {
console.log(stdout);
exec(createUser, function(err, stdout, stderr) {
if (err) {
console.log(err);
process.exit(0);
} else {
console.log(stdout);
exec(grantAccess, function(err, stdout, stderr) {
if (err) {
console.log(err);
} else {
console.log(stdout);
models.sequelize.sync({force: true})
.then(() => {
console.log(`\nUSER (${config.username}) AND DATABASE (${config.database}) CREATED SUCCESSFULLY`);
process.exit(0);
})
.catch((err) => {
console.log(err);
process.exit(0);
});
}
});
}
});
}
});
});