{"action":"created","comment":{"url":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls/comments/27930944","id":27930944,"diff_hunk":"@@ -2,85 +2,103 @@\n  * License, v. 2.0. If a copy of the MPL was not distributed with this\n  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */\n \n+const path = require('path');\n const mysql = require('mysql');\n const buf = require('buf').hex;\n+const MysqlPatcher = require('mysql-patcher');\n \n-const AppError = require('../error');\n-const config = require('../config');\n-const logger = require('../logging')('db.mysql');\n-const P = require('../promise');\n+const AppError = require('../../error');\n+const config = require('../../config');\n+const logger = require('../../logging')('db.mysql');\n+const P = require('../../promise');\n+const patch = require('./patch');\n \n-const SCHEMA = require('fs').readFileSync(__dirname + '/schema.sql').toString();\n \n function MysqlStore(options) {\n   if (options.charset && options.charset !== 'UTF8_UNICODE_CI') {\n-    logger.warn('createDatabase', { charset:  options.charset });\n+    logger.warn('createDatabase', { charset: options.charset });\n   } else {\n     options.charset = 'UTF8_UNICODE_CI';\n   }\n+  options.typeCast = function(field, next) {\n+    if (field.type === 'TINY' && field.length === 1) {\n+      return field.string() === '1';\n+    }\n+    return next();\n+  };\n   this._pool = mysql.createPool(options);\n }\n \n-function createSchema(client, options) {\n-  logger.verbose('createSchema', options);\n+// Apply patches up to the current patch level.\n+// This will also create the DB if it is missing.\n+\n+function updateDbSchema(patcher) {\n+  logger.verbose('updateDbSchema', patcher.options);\n \n   var d = P.defer();\n-  var database = options.database;\n-\n-  logger.verbose('createDatabase', database);\n-  client.query('CREATE DATABASE IF NOT EXISTS ' + database\n-    + ' CHARACTER SET utf8 COLLATE utf8_unicode_ci', function(err) {\n-      if (err) {\n-        logger.error('createDatabase', err);\n-        return d.reject(err);\n-      }\n+  patcher.patch(function(err) {\n+    if (err) {\n+      logger.error('updateDbSchema', err);\n+      return d.reject(err);\n+    }\n+    d.resolve();\n+  });\n \n-      logger.verbose('changeUser');\n-      client.changeUser({\n-        user: options.user,\n-        password: options.password,\n-        database: database\n-      }, function(err) {\n-        if (err) {\n-          logger.error('changeUser', err);\n-          return d.reject(err);\n-        }\n-        logger.verbose('creatingSchema');\n+  return d.promise;\n+}\n+\n+// Sanity-check that we're working with a compatible patch level.\n+\n+function checkDbPatchLevel(patcher) {\n+  logger.verbose('checkDbPatchLevel', patcher.options);\n+\n+  var d = P.defer();\n+\n+  patcher.readDbPatchLevel(function(err) {\n+    if (err) {\n+      logger.error('checkDbPatchLevel', err);\n+      return d.reject(err);\n+    }\n+    // We are only guaranteed to run correctly if we're at the current\n+    // patch level for this version of the code (the normal state of\n+    // affairs) or the one immediately above it (during a deployment).\n+    if (patcher.currentPatchLevel !== patch.level) {\n+      if (patcher.currentPatchLevel !== patch.level + 1) {","path":"lib/db/mysql/index.js","position":93,"original_position":93,"commit_id":"dd176c516538703a4b50565a9edbef12baa8a6f4","original_commit_id":"dd176c516538703a4b50565a9edbef12baa8a6f4","user":{"login":"chilts","id":3048,"avatar_url":"https://avatars.githubusercontent.com/u/3048?v=3","gravatar_id":"","url":"https://api.github.com/users/chilts","html_url":"https://github.com/chilts","followers_url":"https://api.github.com/users/chilts/followers","following_url":"https://api.github.com/users/chilts/following{/other_user}","gists_url":"https://api.github.com/users/chilts/gists{/gist_id}","starred_url":"https://api.github.com/users/chilts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chilts/subscriptions","organizations_url":"https://api.github.com/users/chilts/orgs","repos_url":"https://api.github.com/users/chilts/repos","events_url":"https://api.github.com/users/chilts/events{/privacy}","received_events_url":"https://api.github.com/users/chilts/received_events","type":"User","site_admin":false},"body":"Yeah, we're doing a similar thing in a few places. Maybe mysql-patcher could also do the check?","created_at":"2015-04-07T23:24:40Z","updated_at":"2015-04-07T23:24:40Z","html_url":"https://github.com/mozilla/fxa-profile-server/pull/104#discussion_r27930944","pull_request_url":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls/104","_links":{"self":{"href":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls/comments/27930944"},"html":{"href":"https://github.com/mozilla/fxa-profile-server/pull/104#discussion_r27930944"},"pull_request":{"href":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls/104"}}},"pull_request":{"url":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls/104","id":32722486,"html_url":"https://github.com/mozilla/fxa-profile-server/pull/104","diff_url":"https://github.com/mozilla/fxa-profile-server/pull/104.diff","patch_url":"https://github.com/mozilla/fxa-profile-server/pull/104.patch","issue_url":"https://api.github.com/repos/mozilla/fxa-profile-server/issues/104","number":104,"state":"open","locked":false,"title":"feat(mysql): use mysql patcher to allow incremental schema updates","user":{"login":"zaach","id":3903,"avatar_url":"https://avatars.githubusercontent.com/u/3903?v=3","gravatar_id":"","url":"https://api.github.com/users/zaach","html_url":"https://github.com/zaach","followers_url":"https://api.github.com/users/zaach/followers","following_url":"https://api.github.com/users/zaach/following{/other_user}","gists_url":"https://api.github.com/users/zaach/gists{/gist_id}","starred_url":"https://api.github.com/users/zaach/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zaach/subscriptions","organizations_url":"https://api.github.com/users/zaach/orgs","repos_url":"https://api.github.com/users/zaach/repos","events_url":"https://api.github.com/users/zaach/events{/privacy}","received_events_url":"https://api.github.com/users/zaach/received_events","type":"User","site_admin":false},"body":"@seanmonstar or @chilts r?","created_at":"2015-04-06T23:53:29Z","updated_at":"2015-04-07T23:24:40Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3ab45e48551c14053acd310254175a5867bb8c2a","assignee":{"login":"chilts","id":3048,"avatar_url":"https://avatars.githubusercontent.com/u/3048?v=3","gravatar_id":"","url":"https://api.github.com/users/chilts","html_url":"https://github.com/chilts","followers_url":"https://api.github.com/users/chilts/followers","following_url":"https://api.github.com/users/chilts/following{/other_user}","gists_url":"https://api.github.com/users/chilts/gists{/gist_id}","starred_url":"https://api.github.com/users/chilts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chilts/subscriptions","organizations_url":"https://api.github.com/users/chilts/orgs","repos_url":"https://api.github.com/users/chilts/repos","events_url":"https://api.github.com/users/chilts/events{/privacy}","received_events_url":"https://api.github.com/users/chilts/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls/104/commits","review_comments_url":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls/104/comments","review_comment_url":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mozilla/fxa-profile-server/issues/104/comments","statuses_url":"https://api.github.com/repos/mozilla/fxa-profile-server/statuses/dd176c516538703a4b50565a9edbef12baa8a6f4","head":{"label":"mozilla:mysql-patcher","ref":"mysql-patcher","sha":"dd176c516538703a4b50565a9edbef12baa8a6f4","user":{"login":"mozilla","id":131524,"avatar_url":"https://avatars.githubusercontent.com/u/131524?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla","html_url":"https://github.com/mozilla","followers_url":"https://api.github.com/users/mozilla/followers","following_url":"https://api.github.com/users/mozilla/following{/other_user}","gists_url":"https://api.github.com/users/mozilla/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla/subscriptions","organizations_url":"https://api.github.com/users/mozilla/orgs","repos_url":"https://api.github.com/users/mozilla/repos","events_url":"https://api.github.com/users/mozilla/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla/received_events","type":"Organization","site_admin":false},"repo":{"id":16523688,"name":"fxa-profile-server","full_name":"mozilla/fxa-profile-server","owner":{"login":"mozilla","id":131524,"avatar_url":"https://avatars.githubusercontent.com/u/131524?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla","html_url":"https://github.com/mozilla","followers_url":"https://api.github.com/users/mozilla/followers","following_url":"https://api.github.com/users/mozilla/following{/other_user}","gists_url":"https://api.github.com/users/mozilla/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla/subscriptions","organizations_url":"https://api.github.com/users/mozilla/orgs","repos_url":"https://api.github.com/users/mozilla/repos","events_url":"https://api.github.com/users/mozilla/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mozilla/fxa-profile-server","description":"Firefox Accounts profile attached service.","fork":false,"url":"https://api.github.com/repos/mozilla/fxa-profile-server","forks_url":"https://api.github.com/repos/mozilla/fxa-profile-server/forks","keys_url":"https://api.github.com/repos/mozilla/fxa-profile-server/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mozilla/fxa-profile-server/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mozilla/fxa-profile-server/teams","hooks_url":"https://api.github.com/repos/mozilla/fxa-profile-server/hooks","issue_events_url":"https://api.github.com/repos/mozilla/fxa-profile-server/issues/events{/number}","events_url":"https://api.github.com/repos/mozilla/fxa-profile-server/events","assignees_url":"https://api.github.com/repos/mozilla/fxa-profile-server/assignees{/user}","branches_url":"https://api.github.com/repos/mozilla/fxa-profile-server/branches{/branch}","tags_url":"https://api.github.com/repos/mozilla/fxa-profile-server/tags","blobs_url":"https://api.github.com/repos/mozilla/fxa-profile-server/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mozilla/fxa-profile-server/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mozilla/fxa-profile-server/git/refs{/sha}","trees_url":"https://api.github.com/repos/mozilla/fxa-profile-server/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mozilla/fxa-profile-server/statuses/{sha}","languages_url":"https://api.github.com/repos/mozilla/fxa-profile-server/languages","stargazers_url":"https://api.github.com/repos/mozilla/fxa-profile-server/stargazers","contributors_url":"https://api.github.com/repos/mozilla/fxa-profile-server/contributors","subscribers_url":"https://api.github.com/repos/mozilla/fxa-profile-server/subscribers","subscription_url":"https://api.github.com/repos/mozilla/fxa-profile-server/subscription","commits_url":"https://api.github.com/repos/mozilla/fxa-profile-server/commits{/sha}","git_commits_url":"https://api.github.com/repos/mozilla/fxa-profile-server/git/commits{/sha}","comments_url":"https://api.github.com/repos/mozilla/fxa-profile-server/comments{/number}","issue_comment_url":"https://api.github.com/repos/mozilla/fxa-profile-server/issues/comments{/number}","contents_url":"https://api.github.com/repos/mozilla/fxa-profile-server/contents/{+path}","compare_url":"https://api.github.com/repos/mozilla/fxa-profile-server/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mozilla/fxa-profile-server/merges","archive_url":"https://api.github.com/repos/mozilla/fxa-profile-server/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mozilla/fxa-profile-server/downloads","issues_url":"https://api.github.com/repos/mozilla/fxa-profile-server/issues{/number}","pulls_url":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls{/number}","milestones_url":"https://api.github.com/repos/mozilla/fxa-profile-server/milestones{/number}","notifications_url":"https://api.github.com/repos/mozilla/fxa-profile-server/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mozilla/fxa-profile-server/labels{/name}","releases_url":"https://api.github.com/repos/mozilla/fxa-profile-server/releases{/id}","created_at":"2014-02-04T19:56:31Z","updated_at":"2015-03-23T17:12:40Z","pushed_at":"2015-04-06T23:54:40Z","git_url":"git://github.com/mozilla/fxa-profile-server.git","ssh_url":"git@github.com:mozilla/fxa-profile-server.git","clone_url":"https://github.com/mozilla/fxa-profile-server.git","svn_url":"https://github.com/mozilla/fxa-profile-server","homepage":"https://wiki.mozilla.org/Identity/Firefox_Accounts","size":1765,"stargazers_count":4,"watchers_count":4,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":7,"mirror_url":null,"open_issues_count":13,"forks":7,"open_issues":13,"watchers":4,"default_branch":"master"}},"base":{"label":"mozilla:master","ref":"master","sha":"eb9ee3f8823cf2668de3fc90fbdaa5cd454fb125","user":{"login":"mozilla","id":131524,"avatar_url":"https://avatars.githubusercontent.com/u/131524?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla","html_url":"https://github.com/mozilla","followers_url":"https://api.github.com/users/mozilla/followers","following_url":"https://api.github.com/users/mozilla/following{/other_user}","gists_url":"https://api.github.com/users/mozilla/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla/subscriptions","organizations_url":"https://api.github.com/users/mozilla/orgs","repos_url":"https://api.github.com/users/mozilla/repos","events_url":"https://api.github.com/users/mozilla/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla/received_events","type":"Organization","site_admin":false},"repo":{"id":16523688,"name":"fxa-profile-server","full_name":"mozilla/fxa-profile-server","owner":{"login":"mozilla","id":131524,"avatar_url":"https://avatars.githubusercontent.com/u/131524?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla","html_url":"https://github.com/mozilla","followers_url":"https://api.github.com/users/mozilla/followers","following_url":"https://api.github.com/users/mozilla/following{/other_user}","gists_url":"https://api.github.com/users/mozilla/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla/subscriptions","organizations_url":"https://api.github.com/users/mozilla/orgs","repos_url":"https://api.github.com/users/mozilla/repos","events_url":"https://api.github.com/users/mozilla/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mozilla/fxa-profile-server","description":"Firefox Accounts profile attached service.","fork":false,"url":"https://api.github.com/repos/mozilla/fxa-profile-server","forks_url":"https://api.github.com/repos/mozilla/fxa-profile-server/forks","keys_url":"https://api.github.com/repos/mozilla/fxa-profile-server/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mozilla/fxa-profile-server/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mozilla/fxa-profile-server/teams","hooks_url":"https://api.github.com/repos/mozilla/fxa-profile-server/hooks","issue_events_url":"https://api.github.com/repos/mozilla/fxa-profile-server/issues/events{/number}","events_url":"https://api.github.com/repos/mozilla/fxa-profile-server/events","assignees_url":"https://api.github.com/repos/mozilla/fxa-profile-server/assignees{/user}","branches_url":"https://api.github.com/repos/mozilla/fxa-profile-server/branches{/branch}","tags_url":"https://api.github.com/repos/mozilla/fxa-profile-server/tags","blobs_url":"https://api.github.com/repos/mozilla/fxa-profile-server/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mozilla/fxa-profile-server/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mozilla/fxa-profile-server/git/refs{/sha}","trees_url":"https://api.github.com/repos/mozilla/fxa-profile-server/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mozilla/fxa-profile-server/statuses/{sha}","languages_url":"https://api.github.com/repos/mozilla/fxa-profile-server/languages","stargazers_url":"https://api.github.com/repos/mozilla/fxa-profile-server/stargazers","contributors_url":"https://api.github.com/repos/mozilla/fxa-profile-server/contributors","subscribers_url":"https://api.github.com/repos/mozilla/fxa-profile-server/subscribers","subscription_url":"https://api.github.com/repos/mozilla/fxa-profile-server/subscription","commits_url":"https://api.github.com/repos/mozilla/fxa-profile-server/commits{/sha}","git_commits_url":"https://api.github.com/repos/mozilla/fxa-profile-server/git/commits{/sha}","comments_url":"https://api.github.com/repos/mozilla/fxa-profile-server/comments{/number}","issue_comment_url":"https://api.github.com/repos/mozilla/fxa-profile-server/issues/comments{/number}","contents_url":"https://api.github.com/repos/mozilla/fxa-profile-server/contents/{+path}","compare_url":"https://api.github.com/repos/mozilla/fxa-profile-server/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mozilla/fxa-profile-server/merges","archive_url":"https://api.github.com/repos/mozilla/fxa-profile-server/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mozilla/fxa-profile-server/downloads","issues_url":"https://api.github.com/repos/mozilla/fxa-profile-server/issues{/number}","pulls_url":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls{/number}","milestones_url":"https://api.github.com/repos/mozilla/fxa-profile-server/milestones{/number}","notifications_url":"https://api.github.com/repos/mozilla/fxa-profile-server/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mozilla/fxa-profile-server/labels{/name}","releases_url":"https://api.github.com/repos/mozilla/fxa-profile-server/releases{/id}","created_at":"2014-02-04T19:56:31Z","updated_at":"2015-03-23T17:12:40Z","pushed_at":"2015-04-06T23:54:40Z","git_url":"git://github.com/mozilla/fxa-profile-server.git","ssh_url":"git@github.com:mozilla/fxa-profile-server.git","clone_url":"https://github.com/mozilla/fxa-profile-server.git","svn_url":"https://github.com/mozilla/fxa-profile-server","homepage":"https://wiki.mozilla.org/Identity/Firefox_Accounts","size":1765,"stargazers_count":4,"watchers_count":4,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":7,"mirror_url":null,"open_issues_count":13,"forks":7,"open_issues":13,"watchers":4,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls/104"},"html":{"href":"https://github.com/mozilla/fxa-profile-server/pull/104"},"issue":{"href":"https://api.github.com/repos/mozilla/fxa-profile-server/issues/104"},"comments":{"href":"https://api.github.com/repos/mozilla/fxa-profile-server/issues/104/comments"},"review_comments":{"href":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls/104/comments"},"review_comment":{"href":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mozilla/fxa-profile-server/pulls/104/commits"},"statuses":{"href":"https://api.github.com/repos/mozilla/fxa-profile-server/statuses/dd176c516538703a4b50565a9edbef12baa8a6f4"}}}}