From 222964508b0ce585761c0e3f08afd9832f0343c2 Mon Sep 17 00:00:00 2001
From: Pete Fritchman <petef@databits.net>
Date: Wed, 19 Oct 2011 12:11:24 -0700
Subject: [PATCH] SCHEMA CHANGES: Improve MySQL schema: (closes #480) -
 explicitly ask for InnoDB - use BIGINTs as primary keys - add a BIGINT
 primary key to staged; chars as primary keys hurt indexing - INDEX(foo) when
 foo is already UNIQUE makes a duplicate index - email.user is now a foreign
 key to user.id - add 'NOT NULL' constraints - for varchar(N) where N < 255
 and the strings are fixed length,   it's slightly more efficient to use
 char(N)

Signed-off-by: Lloyd Hilaiel <lloyd@hilaiel.com>
---
 browserid/lib/db_mysql.js | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/browserid/lib/db_mysql.js b/browserid/lib/db_mysql.js
index 06bd219bf..c3645a44b 100644
--- a/browserid/lib/db_mysql.js
+++ b/browserid/lib/db_mysql.js
@@ -49,6 +49,7 @@
  *
  *
  *    +------ staged ----------+
+ *    |*int id                 |
  *    |*string secret          |
  *    | bool new_acct          |
  *    | string existing        |
@@ -67,10 +68,28 @@ var client = undefined;
 // may get defined at open() time causing a database to be dropped upon connection closing.
 var drop_on_close = undefined;
 
+// If you change these schemas, please notify <services-ops@mozilla.com>
 const schemas = [
-  "CREATE TABLE IF NOT EXISTS user   ( id INTEGER AUTO_INCREMENT PRIMARY KEY, passwd VARCHAR(64) );",
-  "CREATE TABLE IF NOT EXISTS email  ( id INTEGER AUTO_INCREMENT PRIMARY KEY, user INTEGER, INDEX(user), address VARCHAR(255) UNIQUE, INDEX(address) );",
-  "CREATE TABLE IF NOT EXISTS staged ( secret VARCHAR(48) PRIMARY KEY, new_acct BOOL, existing VARCHAR(255), email VARCHAR(255) UNIQUE, INDEX(email), ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP);"
+  "CREATE TABLE IF NOT EXISTS user (" +
+    "id BIGINT AUTO_INCREMENT PRIMARY KEY," +
+    "passwd CHAR(64) NOT NULL" +
+    ") ENGINE=InnoDB;",
+
+  "CREATE TABLE IF NOT EXISTS email (" +
+    "id BIGINT AUTO_INCREMENT PRIMARY KEY," +
+    "user BIGINT NOT NULL," +
+    "address VARCHAR(255) UNIQUE NOT NULL," + 
+    "FOREIGN KEY user_fkey (user) REFERENCES user(id)" +
+    ") ENGINE=InnoDB;",
+
+  "CREATE TABLE IF NOT EXISTS staged (" +
+    "id BIGINT AUTO_INCREMENT PRIMARY KEY," +
+    "secret CHAR(48) UNIQUE NOT NULL," +
+    "new_acct BOOL NOT NULL," +
+    "existing VARCHAR(255)," +
+    "email VARCHAR(255) UNIQUE NOT NULL," +
+    "ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL" + 
+    ") ENGINE=InnoDB;",
 ];
 
 // log an unexpected database error
-- 
GitLab