From d1a4df925c3b29a204042c3dbde997db0df1f743 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Thu, 4 Aug 2011 23:07:27 -0600
Subject: [PATCH] implement basics of connection up to mysql and a reasonable
 error when mysql is not set up

---
 browserid/lib/db_mysql.js  | 49 ++++++++++++++++++++++++++++++++++----
 browserid/tests/db-test.js | 27 ++++++++++++++++++++-
 2 files changed, 71 insertions(+), 5 deletions(-)

diff --git a/browserid/lib/db_mysql.js b/browserid/lib/db_mysql.js
index d11b31cb2..a3f97297a 100644
--- a/browserid/lib/db_mysql.js
+++ b/browserid/lib/db_mysql.js
@@ -1,11 +1,52 @@
+/* This is a mysql driver for the browserid server.  It maps the data
+ * storage requirements of browserid onto a relational schema.  This
+ * driver is intended to be fast and scalable.
+ */
 
+/*
+ * The Schema:
+ *
+ *    +--- user ------+       +--- email ----+        +----- key ------+
+ *    |*int id        | <-\   |*int id       | <-\    |*int id         |
+ *    | text password |    \- |*int user     |    \-- |*int email      |
+ *    +---------------+       | text address |        | text key       |
+ *                            +--------------+        | int expires    |
+ *                                                    +----------------+
+ *
+ *
+ *
+ */
 
-exports.open = function() {
-  throw "not implemented";
+const
+mysql = require('mysql');
+
+var client = new mysql.Client();
+
+// open & create the mysql database
+exports.open = function(cfg, cb) {
+  // mysql config requires
+  const defParams = {
+    host: '127.0.0.1',
+    port: "3306",
+    user: 'test',
+    password: 'pass'
+  };
+
+  Object.keys(defParams).forEach(function(param) {
+    client[param] = cfg[param] ? config.param : defParams[param];
+  });
+
+  var database = cfg.database ? cfg.database : 'browserid';
+
+  client.connect(function(error) {
+    cb(error);
+  });
 };
 
-exports.close = function() {
-  throw "not implemented";
+exports.close = function(cb) {
+  client.end(function(err) {
+    if (cb) cb(err);
+  });
 };
 
 exports.emailKnown = function() {
diff --git a/browserid/tests/db-test.js b/browserid/tests/db-test.js
index 1feb09d21..eba4e9f73 100755
--- a/browserid/tests/db-test.js
+++ b/browserid/tests/db-test.js
@@ -15,6 +15,29 @@ suite.options.error = false;
 function addTestsForDriver(driver) {
   var dbPath = temp.path({suffix: '.db'});
 
+  if (driver === 'mysql') {
+    // let's check to see if we can connect and render a nice
+    // error message if not.  For community members making casual
+    // contributions, we should expect that they might not want to
+    // set up mysql.
+    suite.addBatch({
+      "mysql server": {
+        topic: function() { db.open({driver: driver}, this.callback) },
+        "accepting connections": function(err) {
+          if (err) {
+            console.log("SKIPPING MYSQL TESTING: I cannot connect to the mysql database (" + err.message + ")");
+          }
+        },
+        "connection closes": {
+          topic: function() { db.close(this.callback); },
+          "without error": function(err) {
+            assert.isNull(err);
+          }
+        }
+      }
+    });
+  }
+
   suite.addBatch({
     "onReady": {
       topic: function() { db.onReady(this.callback); },
@@ -400,7 +423,9 @@ files = fs.readdirSync(path.join(__dirname, "..", "lib"));
 
 files.forEach(function(f) {
   var m = /^db_(.+)\.js$/.exec(f);
-  if (m) addTestsForDriver(m[1]);
+  if (m) {
+    addTestsForDriver(m[1]);
+  }
 });
 
 // run or export the suite.
-- 
GitLab