Skip to content

Instantly share code, notes, and snippets.

@yocontra
Created August 29, 2013 00:41
Show Gist options
  • Save yocontra/6373075 to your computer and use it in GitHub Desktop.
Save yocontra/6373075 to your computer and use it in GitHub Desktop.
Dumps stuff into MySQL
var child_process = require('child_process');
var ConnectionConfig = require('mysql/lib/ConnectionConfig');
// first argument = mysql://whatever.com/whatevs
// second argument = string of sql file
module.exports = function(url, sqlData, cb) {
var opt = ConnectionConfig.parseUrl(url);
var args = [];
if (opt.user) args.push("--user="+opt.user);
if (opt.password) args.push("--password="+opt.password);
var stderr = "";
var stdout = "";
var proc = child_process.spawn("mysql", args);
proc.stdout.on('data', function(data){
stdout += String(data);
});
proc.stderr.on('data', function(data){
stderr += String(data);
});
proc.on('exit', function(code){
if (code !== 1) {
return cb(new Error(stderr));
}
cb(null, stdout, code);
});
proc.stdin.write("CREATE DATABASE "+opt.database+";\n");
proc.stdin.write("USE "+opt.database+";\n");
proc.stdin.write(String(sqlData));
proc.stdin.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment