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

Commit c226b07

Browse files
deeptibaghelvladikoff
authored andcommitted
feat(mysql): STRICT_ALL_TABLES and NO_ENGINE_SUBSTITUTION required in sql (#327) r=@vladikoff,@rfk
Fixes #277 Added the query to set the sql_mode Added unit test to disallow any other sql_mode
1 parent 9c433ba commit c226b07

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

lib/db/mysql.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ const dbUtil = require('./util')
1515
const REQUIRED_CHARSET = 'UTF8MB4_BIN'
1616
const DATABASE_NAME = require('../constants').DATABASE_NAME
1717

18+
const REQUIRED_SQL_MODES = [
19+
'STRICT_ALL_TABLES',
20+
'NO_ENGINE_SUBSTITUTION',
21+
]
22+
1823
// http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html
1924
const ER_TOO_MANY_CONNECTIONS = 1040
2025
const ER_DUP_ENTRY = 1062
@@ -67,6 +72,18 @@ module.exports = function (log, error) {
6772
options.master.charset = options.charset
6873
options.slave.charset = options.charset
6974

75+
var mode = REQUIRED_SQL_MODES.join(',')
76+
if (options.sql_mode && options.sql_mode !== mode) {
77+
log.error('createPoolCluster.invalidSqlMode', { sql_mode: options.sql_mode })
78+
throw new Error(`You cannot use any sql mode other than ${mode}`)
79+
} else {
80+
options.sql_mode = REQUIRED_SQL_MODES.join(',')
81+
}
82+
83+
options.master.sql_mode = options.sql_mode
84+
options.slave.sql_mode = options.sql_mode
85+
86+
7087
// Use separate pools for master and slave connections.
7188
this.poolCluster.add('MASTER', options.master)
7289
this.poolCluster.add('SLAVE', options.slave)
@@ -1138,14 +1155,20 @@ module.exports = function (log, error) {
11381155
return resolve(connection)
11391156
}
11401157

1141-
connection.query('SET NAMES utf8mb4 COLLATE utf8mb4_bin;', (err) => {
1158+
var mode = REQUIRED_SQL_MODES.join(',')
1159+
connection.query(`SET SESSION sql_mode = '${mode}';`, (err) => {
11421160
if (err) {
11431161
return reject(err)
11441162
}
11451163

1146-
connection._fxa_initialized = true
1164+
connection.query('SET NAMES utf8mb4 COLLATE utf8mb4_bin;', (err) => {
1165+
if (err) {
1166+
return reject(err)
1167+
}
11471168

1148-
resolve(connection)
1169+
connection._fxa_initialized = true
1170+
resolve(connection)
1171+
})
11491172
})
11501173

11511174
})
@@ -1247,7 +1270,8 @@ module.exports = function (log, error) {
12471270
'collation_server',
12481271
'max_connections',
12491272
'version',
1250-
'wait_timeout'
1273+
'wait_timeout',
1274+
'sql_mode'
12511275
]
12521276

12531277
return this.getConnection(poolName)

test/local/mysql_tests.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ describe('MySQL', () => {
4141
}
4242
)
4343

44+
it(
45+
'forces REQUIRED_SQL_MODES for connections',
46+
() => {
47+
const configSqlMode = Object.assign({}, config)
48+
const REQUIRED_SQL_MODES = [
49+
'STRICT_ALL_TABLES',
50+
'NO_ENGINE_SUBSTITUTION',
51+
]
52+
var mode = REQUIRED_SQL_MODES.join(',')
53+
configSqlMode.sql_mode = 'xyz'
54+
return DB.connect(configSqlMode)
55+
.then(
56+
assert.fail,
57+
err => {
58+
assert.equal(err.message, `You cannot use any sql mode other than ${mode}`)
59+
}
60+
)
61+
}
62+
)
63+
4464
it(
4565
'a select on an unknown table should result in an error',
4666
() => {

0 commit comments

Comments
 (0)