From 7a250eb74375e318ab9e07f050c6e63f6925f1a3 Mon Sep 17 00:00:00 2001
From: Lloyd Hilaiel <lloyd@hilaiel.com>
Date: Wed, 21 Dec 2011 12:14:42 -0700
Subject: [PATCH] SCHEMA CHANGE: password is now nullable in schema.  also, add
 .createUserWithPrimaryEmail and .emailType to db abstractoin

---
 lib/db.js       |  4 +++-
 lib/db/json.js  | 20 +++++++++++++++++++-
 lib/db/mysql.js | 40 ++++++++++++++++++++++++++++++++++------
 3 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/lib/db.js b/lib/db.js
index 2cd53f6fb..3521c2ccc 100644
--- a/lib/db.js
+++ b/lib/db.js
@@ -112,7 +112,8 @@ exports.onReady = function(f) {
   'checkAuth',
   'listEmails',
   'lastStaged',
-  'ping'
+  'ping',
+  'emailType'
 ].forEach(function(fn) {
   exports[fn] = function() {
     checkReady();
@@ -130,6 +131,7 @@ exports.onReady = function(f) {
   'removeEmail',
   'cancelAccount',
   'updatePassword',
+  'createUserWithPrimaryEmail'
 ].forEach(function(fn) {
   exports[fn] = function() {
     if (!config.get('database').may_write) {
diff --git a/lib/db/json.js b/lib/db/json.js
index 18ef914b1..3dace705a 100644
--- a/lib/db/json.js
+++ b/lib/db/json.js
@@ -127,6 +127,12 @@ exports.emailKnown = function(email, cb) {
   setTimeout(function() { cb(m.length > 0) }, 0);
 };
 
+exports.emailType = function(email, cb) {
+  sync();
+  var m = jsel.match(".emails ." + ESC(email), db.users);
+  process.nextTick(function() { cb(m.length ? m.type : undefined); });
+};
+
 exports.isStaged = function(email, cb) {
   if (cb) {
     setTimeout(function() {
@@ -201,11 +207,23 @@ exports.stageEmail = function(existing_email, new_email, cb) {
     };
     db.stagedEmails[new_email] = secret;
     flush();
-    
+
     setTimeout(function() { cb(secret); }, 0);
   });
 };
 
+exports.createUserWithPrimaryEmail = function(email, cb) {
+  var emailVal = { };
+  emailVal[email] = { type: 'primary' };
+  db.users.push({
+    password: null,
+    emails: emailVal
+  });
+  flush();
+  process.nextTick(function() {
+    cb(undefined);
+  });
+};
 
 exports.emailForVerificationSecret = function(secret, cb) {
   setTimeout(function() {
diff --git a/lib/db/mysql.js b/lib/db/mysql.js
index 2c1a608f3..ac93d50f4 100644
--- a/lib/db/mysql.js
+++ b/lib/db/mysql.js
@@ -41,11 +41,12 @@
 /*
  * The Schema:
  *
- *    +--- user ------+       +--- email ----+
- *    |*int id        | <-\   |*int id       |
- *    | string passwd |    \- |*int user     |
- *    +---------------+       |*string address
- *                            +--------------+
+ *    +--- user ------+       +--- email -----+
+ *    |*int id        | <-\   |*int id        |
+ *    | string passwd |    \- |*int user      |
+ *    +---------------+       |*string address|
+ *                            | enum type     |
+ *                            +---------------+
  *
  *
  *    +------ staged ----------+
@@ -70,7 +71,7 @@ var client = undefined;
 const schemas = [
   "CREATE TABLE IF NOT EXISTS user (" +
     "id BIGINT AUTO_INCREMENT PRIMARY KEY," +
-    "passwd CHAR(64) NOT NULL" +
+    "passwd CHAR(64)" +
     ") ENGINE=InnoDB;",
 
   "CREATE TABLE IF NOT EXISTS email (" +
@@ -233,6 +234,16 @@ exports.emailKnown = function(email, cb) {
   );
 }
 
+exports.emailType = function(email, cb) {
+  client.query(
+    "SELECT type FROM email WHERE address = ?", [ email ],
+    function(err, rows) {
+      if (err) logUnexpectedError(err);
+      cb((rows && rows.length > 0) ? rows[0].type : null);
+    }
+  );
+}
+
 exports.isStaged = function(email, cb) {
   client.query(
     "SELECT COUNT(*) as N FROM staged WHERE email = ?", [ email ],
@@ -352,6 +363,23 @@ exports.gotVerificationSecret = function(secret, hash, cb) {
   );
 }
 
+exports.createUserWithPrimaryEmail = function(email, cb) {
+  // create a new user acct with no password
+  client.query(
+    "INSERT INTO user() VALUES()",
+    function(err, info) {
+      console.log(info.insertId);
+      if (err) { logUnexpectedError(err); cb(err); return; }
+      client.query(
+        "INSERT INTO email(user, address, type) VALUES(?, ?, ?)",
+        [ info.insertId, email, 'primary' ],
+        function(err, info) {
+          if (err) logUnexpectedError(err);
+          cb(err ? err : undefined);
+        });
+    });
+};
+
 exports.emailsBelongToSameAccount = function(lhs, rhs, cb) {
   client.query(
     'SELECT COUNT(*) AS n FROM email WHERE address = ? AND user = ( SELECT user FROM email WHERE address = ? );',
-- 
GitLab