Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
fix(account): update stored procedures to be more replication friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
vbudhram committed Oct 18, 2018
1 parent 76a4fdd commit 235a104
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
23 changes: 18 additions & 5 deletions lib/db/mysql.js
Expand Up @@ -1426,7 +1426,7 @@ module.exports = function (log, error) {
})
}

const VERIFY_SESSION_WITH_METHOD = 'CALL verifyTokensWithMethod_2(?, ?, ?)'
const VERIFY_SESSION_WITH_METHOD = 'CALL verifyTokensWithMethod_3(?, ?, ?)'
MySql.prototype.verifyTokensWithMethod = function (tokenId, data) {
return P.resolve()
.then(() => {
Expand All @@ -1436,18 +1436,31 @@ module.exports = function (log, error) {
throw error.invalidVerificationMethod()
}

return this.readFirstResult(VERIFY_SESSION_WITH_METHOD, [
return this.read(VERIFY_SESSION_WITH_METHOD, [
tokenId,
verificationMethod,
Date.now()
])
.then((result) => {
if (result['@updateCount'] === 0) {
.then((results) => {
if (results.affectedRows === 0) {
throw error.notFound()
}

return P.resolve({})
// Verify session in the `unverifiedTokens` table
return this.sessionToken(tokenId)
.then((session) => {
return this.verifyTokens(session.tokenVerificationId, {uid: session.uid})
.catch((err) => {
// Don't error if there wasn't a token found to verify. This token could have
// already been verified with another method.
if (err.errno === error.notFound().errno) {
return
}
throw err
})
})
})
.then(() => P.resolve({}))
})
}

Expand Down
2 changes: 1 addition & 1 deletion lib/db/patch.js
Expand Up @@ -4,4 +4,4 @@

// The expected patch level of the database. Update if you add a new
// patch in the ./schema/ directory.
module.exports.level = 86
module.exports.level = 87
23 changes: 23 additions & 0 deletions lib/db/schema/patch-086-087.sql
@@ -0,0 +1,23 @@
SET NAMES utf8mb4 COLLATE utf8mb4_bin;

CALL assertPatchLevel('86');

CREATE PROCEDURE `verifyTokensWithMethod_3` (
IN `tokenIdArg` BINARY(32),
IN `verificationMethodArg` INT,
IN `verifiedAtArg` BIGINT(1)
)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;

-- Update session verification methods
UPDATE `sessionTokens` SET verificationMethod = verificationMethodArg, verifiedAt = verifiedAtArg
WHERE tokenId = tokenIdArg;

END;

UPDATE dbMetadata SET value = '87' WHERE name = 'schema-patch-level';
5 changes: 5 additions & 0 deletions lib/db/schema/patch-087-086.sql
@@ -0,0 +1,5 @@
-- SET NAMES utf8mb4 COLLATE utf8mb4_bin;

-- DROP PROCEDURE verifyTokensWithMethod_3;

-- UPDATE dbMetadata SET value = '86' WHERE name = 'schema-patch-level';

0 comments on commit 235a104

Please sign in to comment.