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

fix(mysql): Set strict mode on all new db migrations. #358

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/db/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ module.exports = function (log, error) {

// Insert : accounts
// Values : uid = $1, normalizedEmail = $2, email = $3, emailCode = $4, emailVerified = $5, kA = $6, wrapWrapKb = $7, authSalt = $8, verifierVersion = $9, verifyHash = $10, verifierSetAt = $11, createdAt = $12, locale = $13
var CREATE_ACCOUNT = 'CALL createAccount_7(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
var CREATE_ACCOUNT = 'CALL createAccount_8(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'

MySql.prototype.createAccount = function (uid, data) {
return this.write(
Expand Down
2 changes: 1 addition & 1 deletion lib/db/patch.js
Original file line number Diff line number Diff line change
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 = 80
module.exports.level = 81
89 changes: 89 additions & 0 deletions lib/db/schema/patch-080-081.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
SET SESSION sql_mode = CONCAT(@@sql_mode, ',STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION');
SET NAMES utf8mb4 COLLATE utf8mb4_bin;

CREATE PROCEDURE `createAccount_8`(
IN `inUid` BINARY(16) ,
IN `inNormalizedEmail` VARCHAR(255),
IN `inEmail` VARCHAR(255),
IN `inEmailCode` BINARY(16),
IN `inEmailVerified` TINYINT(1),
IN `inKA` BINARY(32),
IN `inWrapWrapKb` BINARY(32),
IN `inAuthSalt` BINARY(32),
IN `inVerifierVersion` TINYINT UNSIGNED,
IN `inVerifyHash` BINARY(32),
IN `inVerifierSetAt` BIGINT UNSIGNED,
IN `inCreatedAt` BIGINT UNSIGNED,
IN `inLocale` VARCHAR(255)
)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;

START TRANSACTION;

-- Check to see if the normalizedEmail exists in the emails table before creating a new user
-- with this email.
SET @emailExists = 0;
SELECT COUNT(*) INTO @emailExists FROM emails WHERE normalizedEmail = inNormalizedEmail;
IF @emailExists > 0 THEN
SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO = 1062, MESSAGE_TEXT = 'Unable to create user, email used belongs to another user.';
END IF;

INSERT INTO accounts(
uid,
normalizedEmail,
email,
emailCode,
emailVerified,
kA,
wrapWrapKb,
authSalt,
verifierVersion,
verifyHash,
verifierSetAt,
createdAt,
locale
)
VALUES(
inUid,
LOWER(inNormalizedEmail),
inEmail,
inEmailCode,
inEmailVerified,
inKA,
inWrapWrapKb,
inAuthSalt,
inVerifierVersion,
inVerifyHash,
inVerifierSetAt,
inCreatedAt,
inLocale
);

INSERT INTO emails(
normalizedEmail,
email,
uid,
emailCode,
isVerified,
isPrimary,
createdAt
)
VALUES(
LOWER(inNormalizedEmail),
inEmail,
inUid,
inEmailCode,
inEmailVerified,
true,
inCreatedAt
);

COMMIT;
END;

UPDATE dbMetadata SET value = '81' WHERE name = 'schema-patch-level';
7 changes: 7 additions & 0 deletions lib/db/schema/patch-081-080.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- SET SESSION sql_mode = CONCAT(@@sql_mode, ',STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION');
-- SET NAMES utf8mb4 COLLATE utf8mb4_bin;

-- DROP PROCEDURE createAccount_8;

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

4 changes: 3 additions & 1 deletion scripts/migration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ fi

printf "Generating migration boilerplate for patch level $NEW_LEVEL..."

echo "SET NAMES utf8mb4 COLLATE utf8mb4_bin;\n" > "$FWD_SCHEMA"
echo "SET SESSION sql_mode = CONCAT(@@sql_mode, ',STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION');" > "$FWD_SCHEMA"
echo "SET NAMES utf8mb4 COLLATE utf8mb4_bin;\n" >> "$FWD_SCHEMA"
echo "-- TODO: Implement your forward migration here\n" >> "$FWD_SCHEMA"
echo "UPDATE dbMetadata SET value = '$NEW_LEVEL' WHERE name = 'schema-patch-level';\n" >> "$FWD_SCHEMA"

echo '-- -- TODO: Implement your *commented-out* reverse migration here\n' > "$REV_SCHEMA"
echo "-- SET SESSION sql_mode = CONCAT(@@sql_mode, ',STRICT_ALL_TABLES,NO_ENGINE_SUBSTITUTION');" > "$REV_SCHEMA"
echo "-- SET NAMES utf8mb4 COLLATE utf8mb4_bin;\n" >> "$REV_SCHEMA"
echo "-- UPDATE dbMetadata SET value = '$PREV_LEVEL' WHERE name = 'schema-patch-level';\n" >> "$REV_SCHEMA"

Expand Down