Created
August 29, 2013 00:41
-
-
Save yocontra/6373075 to your computer and use it in GitHub Desktop.
Dumps stuff into MySQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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