diff --git a/package.json b/package.json index 96a91a6..9df90d6 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "MySQL connector for loopback-datasource-juggler", "main": "index.js", "scripts": { + "pretest": "node pretest.js", "test": "mocha" }, "dependencies": { diff --git a/pretest.js b/pretest.js new file mode 100644 index 0000000..294a24b --- /dev/null +++ b/pretest.js @@ -0,0 +1,28 @@ +if (!process.env.TEST_MYSQL_USER) { + console.log('not seeding DB with example db'); + return; +} + +var fs = require('fs'); +var cp = require('child_process'); + +var sql = fs.createReadStream(require.resolve('./example/table.sql')); +var stdio = ['pipe', process.stdout, process.stderr]; +var args = ['--user=' + process.env.TEST_MYSQL_USER]; + +if (process.env.TEST_MYSQL_HOST) { + args.push('--host=' + process.env.TEST_MYSQL_HOST); +} +if (process.env.TEST_MYSQL_PORT) { + args.push('--port=' + process.env.TEST_MYSQL_PORT); +} +if (process.env.TEST_MYSQL_PASSWORD) { + args.push('--password=' + process.env.TEST_MYSQL_PASSWORD); +} + +console.log('seeding DB with example db...'); +var mysql = cp.spawn('mysql', args, {stdio: stdio}); +sql.pipe(mysql.stdin); +mysql.on('exit', function() { + console.log('done seeding DB'); +});