diff --git a/browserid/lib/db_mysql.js b/browserid/lib/db_mysql.js index 3773f9ea3df721bcf61f86b105c7a239c411c495..607fe7e601c0b384137ef8b9820d8e5fe3ff75f5 100644 --- a/browserid/lib/db_mysql.js +++ b/browserid/lib/db_mysql.js @@ -18,9 +18,13 @@ */ const -mysql = require('mysql'); +mysql = require('mysql'), +secrets = require('./secrets'); -var client = new mysql.Client(); +var client = undefined; + +// may get defined at open() time causing a database to be dropped upon connection closing. +var drop_on_close = undefined; const schemas = [ "CREATE TABLE IF NOT EXISTS user ( id INTEGER PRIMARY KEY, password TEXT );", @@ -30,19 +34,28 @@ const schemas = [ // open & create the mysql database exports.open = function(cfg, cb) { + if (client) throw "database is already open!"; + client = new mysql.Client(); // mysql config requires const defParams = { host: '127.0.0.1', port: "3306", user: 'test', - password: 'pass' + password: 'pass', + unit_test: false }; Object.keys(defParams).forEach(function(param) { - client[param] = cfg[param] ? config.param : defParams[param]; + client[param] = cfg[param] ? cfg.param : defParams[param]; }); - var database = cfg.database ? cfg.database : 'browserid'; + // let's figure out the database name + var database = cfg.database; + if (!database) database = "browserid"; + if (cfg.unit_test) { + database += "_" + secrets.generate(8); + drop_on_close = database; + } client.connect(function(error) { if (error) cb(error); @@ -78,9 +91,21 @@ exports.open = function(cfg, cb) { }; exports.close = function(cb) { - client.end(function(err) { - if (cb) cb(err); - }); + function endConn() { + client.end(function(err) { + client = undefined; + if (cb) cb(err); + }); + } + // when unit_test is specified at open time, we use a temporary database, + // and clean it up upon close. + if (drop_on_close) { + client.query("DROP DATABASE " + drop_on_close, function() { + endConn(); + }); + } else { + endConn(); + } }; exports.emailKnown = function() { diff --git a/browserid/tests/db-test.js b/browserid/tests/db-test.js index eba4e9f73eb776e645b78ad46a69ced9363715f8..f8907b1a0c6e28f27da3d92018516c49f8ed60d4 100755 --- a/browserid/tests/db-test.js +++ b/browserid/tests/db-test.js @@ -22,7 +22,7 @@ function addTestsForDriver(driver) { // set up mysql. suite.addBatch({ "mysql server": { - topic: function() { db.open({driver: driver}, this.callback) }, + topic: function() { db.open({driver: driver, unit_test: true}, this.callback) }, "accepting connections": function(err) { if (err) { console.log("SKIPPING MYSQL TESTING: I cannot connect to the mysql database (" + err.message + ")"); @@ -49,7 +49,7 @@ function addTestsForDriver(driver) { }, "opening the database": { topic: function() { - db.open({ driver: driver, path: dbPath }, this.callback); + db.open({ driver: driver, unit_test: true, path: dbPath }, this.callback); }, "and its ready": function(r) { assert.isUndefined(r);