不要怂,就是干,撸起袖子干!

Commit 1432cfd1 by Andy Edwards Committed by GitHub

refactor(dialects/mysql): asyncify methods (#12130)

1 parent 722ed505
...@@ -54,7 +54,7 @@ class ConnectionManager extends AbstractConnectionManager { ...@@ -54,7 +54,7 @@ class ConnectionManager extends AbstractConnectionManager {
* @returns {Promise<Connection>} * @returns {Promise<Connection>}
* @private * @private
*/ */
connect(config) { async connect(config) {
const connectionConfig = Object.assign({ const connectionConfig = Object.assign({
host: config.host, host: config.host,
port: config.port, port: config.port,
...@@ -68,7 +68,8 @@ class ConnectionManager extends AbstractConnectionManager { ...@@ -68,7 +68,8 @@ class ConnectionManager extends AbstractConnectionManager {
supportBigNumbers: true supportBigNumbers: true
}, config.dialectOptions); }, config.dialectOptions);
return new Promise((resolve, reject) => { try {
const connection = await new Promise((resolve, reject) => {
const connection = this.lib.createConnection(connectionConfig); const connection = this.lib.createConnection(connectionConfig);
const errorHandler = e => { const errorHandler = e => {
...@@ -90,11 +91,9 @@ class ConnectionManager extends AbstractConnectionManager { ...@@ -90,11 +91,9 @@ class ConnectionManager extends AbstractConnectionManager {
// if we will use `once.error` node process will crash on 2nd error emit // if we will use `once.error` node process will crash on 2nd error emit
connection.on('error', errorHandler); connection.on('error', errorHandler);
connection.once('connect', connectHandler); connection.once('connect', connectHandler);
}) });
.then(result => {
debug('connection acquired');return result; debug('connection acquired');
})
.then(connection => {
connection.on('error', error => { connection.on('error', error => {
switch (error.code) { switch (error.code) {
case 'ESOCKET': case 'ESOCKET':
...@@ -105,22 +104,16 @@ class ConnectionManager extends AbstractConnectionManager { ...@@ -105,22 +104,16 @@ class ConnectionManager extends AbstractConnectionManager {
} }
}); });
return new Promise((resolve, reject) => {
if (!this.sequelize.config.keepDefaultTimezone) { if (!this.sequelize.config.keepDefaultTimezone) {
// set timezone for this connection // set timezone for this connection
// but named timezone are not directly supported in mysql, so get its offset first // but named timezone are not directly supported in mysql, so get its offset first
let tzOffset = this.sequelize.options.timezone; let tzOffset = this.sequelize.options.timezone;
tzOffset = /\//.test(tzOffset) ? momentTz.tz(tzOffset).format('Z') : tzOffset; tzOffset = /\//.test(tzOffset) ? momentTz.tz(tzOffset).format('Z') : tzOffset;
return connection.query(`SET time_zone = '${tzOffset}'`, err => { await promisify(cb => connection.query(`SET time_zone = '${tzOffset}'`, cb))();
if (err) { reject(err); } else { resolve(connection); }
});
} }
// return connection without executing SET time_zone query return connection;
resolve(connection); } catch (err) {
});
})
.catch(err => {
switch (err.code) { switch (err.code) {
case 'ECONNREFUSED': case 'ECONNREFUSED':
throw new SequelizeErrors.ConnectionRefusedError(err); throw new SequelizeErrors.ConnectionRefusedError(err);
...@@ -135,17 +128,17 @@ class ConnectionManager extends AbstractConnectionManager { ...@@ -135,17 +128,17 @@ class ConnectionManager extends AbstractConnectionManager {
default: default:
throw new SequelizeErrors.ConnectionError(err); throw new SequelizeErrors.ConnectionError(err);
} }
}); }
} }
disconnect(connection) { async disconnect(connection) {
// Don't disconnect connections with CLOSED state // Don't disconnect connections with CLOSED state
if (connection._closing) { if (connection._closing) {
debug('connection tried to disconnect but was already at CLOSED state'); debug('connection tried to disconnect but was already at CLOSED state');
return Promise.resolve(); return;
} }
return promisify(callback => connection.end(callback))(); return await promisify(callback => connection.end(callback))();
} }
validate(connection) { validate(connection) {
......
...@@ -21,31 +21,29 @@ const sequelizeErrors = require('../../errors'); ...@@ -21,31 +21,29 @@ const sequelizeErrors = require('../../errors');
@private @private
*/ */
function removeColumn(qi, tableName, columnName, options) { async function removeColumn(qi, tableName, columnName, options) {
options = options || {}; options = options || {};
return qi.sequelize.query( const [results] = await qi.sequelize.query(
qi.QueryGenerator.getForeignKeyQuery(tableName.tableName ? tableName : { qi.QueryGenerator.getForeignKeyQuery(tableName.tableName ? tableName : {
tableName, tableName,
schema: qi.sequelize.config.database schema: qi.sequelize.config.database
}, columnName), }, columnName),
Object.assign({ raw: true }, options) Object.assign({ raw: true }, options)
) );
.then(([results]) => {
//Exclude primary key constraint //Exclude primary key constraint
if (!results.length || results[0].constraint_name === 'PRIMARY') { if (results.length && results[0].constraint_name !== 'PRIMARY') {
// No foreign key constraints found, so we can remove the column await Promise.all(results.map(constraint => qi.sequelize.query(
return;
}
return Promise.all(results.map(constraint => qi.sequelize.query(
qi.QueryGenerator.dropForeignKeyQuery(tableName, constraint.constraint_name), qi.QueryGenerator.dropForeignKeyQuery(tableName, constraint.constraint_name),
Object.assign({ raw: true }, options) Object.assign({ raw: true }, options)
))); )));
}) }
.then(() => qi.sequelize.query(
return await qi.sequelize.query(
qi.QueryGenerator.removeColumnQuery(tableName, columnName), qi.QueryGenerator.removeColumnQuery(tableName, columnName),
Object.assign({ raw: true }, options) Object.assign({ raw: true }, options)
)); );
} }
/** /**
...@@ -56,16 +54,16 @@ function removeColumn(qi, tableName, columnName, options) { ...@@ -56,16 +54,16 @@ function removeColumn(qi, tableName, columnName, options) {
* *
* @private * @private
*/ */
function removeConstraint(qi, tableName, constraintName, options) { async function removeConstraint(qi, tableName, constraintName, options) {
const sql = qi.QueryGenerator.showConstraintsQuery( const sql = qi.QueryGenerator.showConstraintsQuery(
tableName.tableName ? tableName : { tableName.tableName ? tableName : {
tableName, tableName,
schema: qi.sequelize.config.database schema: qi.sequelize.config.database
}, constraintName); }, constraintName);
return qi.sequelize.query(sql, Object.assign({}, options, const constraints = await qi.sequelize.query(sql, Object.assign({}, options,
{ type: qi.sequelize.QueryTypes.SHOWCONSTRAINTS })) { type: qi.sequelize.QueryTypes.SHOWCONSTRAINTS }));
.then(constraints => {
const constraint = constraints[0]; const constraint = constraints[0];
let query; let query;
if (!constraint || !constraint.constraintType) { if (!constraint || !constraint.constraintType) {
...@@ -83,8 +81,7 @@ function removeConstraint(qi, tableName, constraintName, options) { ...@@ -83,8 +81,7 @@ function removeConstraint(qi, tableName, constraintName, options) {
query = qi.QueryGenerator.removeIndexQuery(constraint.tableName, constraint.constraintName); query = qi.QueryGenerator.removeIndexQuery(constraint.tableName, constraint.constraintName);
} }
return qi.sequelize.query(query, options); return await qi.sequelize.query(query, options);
});
} }
exports.removeConstraint = removeConstraint; exports.removeConstraint = removeConstraint;
......
'use strict'; 'use strict';
const Utils = require('../../utils');
const AbstractQuery = require('../abstract/query'); const AbstractQuery = require('../abstract/query');
const sequelizeErrors = require('../../errors'); const sequelizeErrors = require('../../errors');
const _ = require('lodash'); const _ = require('lodash');
...@@ -27,7 +26,7 @@ class Query extends AbstractQuery { ...@@ -27,7 +26,7 @@ class Query extends AbstractQuery {
return [sql, bindParam.length > 0 ? bindParam : undefined]; return [sql, bindParam.length > 0 ? bindParam : undefined];
} }
run(sql, parameters) { async run(sql, parameters) {
this.sql = sql; this.sql = sql;
const { connection, options } = this; const { connection, options } = this;
...@@ -36,7 +35,7 @@ class Query extends AbstractQuery { ...@@ -36,7 +35,7 @@ class Query extends AbstractQuery {
const complete = this._logQuery(sql, debug, parameters); const complete = this._logQuery(sql, debug, parameters);
return new Utils.Promise((resolve, reject) => { const results = await new Promise((resolve, reject) => {
const handler = (err, results) => { const handler = (err, results) => {
complete(); complete();
...@@ -59,16 +58,13 @@ class Query extends AbstractQuery { ...@@ -59,16 +58,13 @@ class Query extends AbstractQuery {
} else { } else {
connection.query({ sql }, handler).setMaxListeners(100); connection.query({ sql }, handler).setMaxListeners(100);
} }
}) });
// Log warnings if we've got them. // Log warnings if we've got them.
.then(results => {
if (showWarnings && results && results.warningStatus > 0) { if (showWarnings && results && results.warningStatus > 0) {
return this.logWarnings(results); await this.logWarnings(results);
} }
return results;
})
// Return formatted results... // Return formatted results...
.then(results => this.formatResults(results)); return this.formatResults(results);
} }
/** /**
...@@ -165,8 +161,8 @@ class Query extends AbstractQuery { ...@@ -165,8 +161,8 @@ class Query extends AbstractQuery {
return result; return result;
} }
logWarnings(results) { async logWarnings(results) {
return this.run('SHOW WARNINGS').then(warningResults => { const warningResults = await this.run('SHOW WARNINGS');
const warningMessage = `MySQL Warnings (${this.connection.uuid || 'default'}): `; const warningMessage = `MySQL Warnings (${this.connection.uuid || 'default'}): `;
const messages = []; const messages = [];
for (const _warningRow of warningResults) { for (const _warningRow of warningResults) {
...@@ -185,7 +181,6 @@ class Query extends AbstractQuery { ...@@ -185,7 +181,6 @@ class Query extends AbstractQuery {
this.sequelize.log(warningMessage + messages.join('; '), this.options); this.sequelize.log(warningMessage + messages.join('; '), this.options);
return results; return results;
});
} }
formatError(err) { formatError(err) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!