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

Commit cf5b2f54 by Andy Edwards Committed by GitHub

refactor: replace misc bluebird API usage (#12072)

1 parent 869fdae8
......@@ -8,6 +8,7 @@ const DataTypes = require('../../data-types').mysql;
const momentTz = require('moment-timezone');
const debug = logger.debugContext('connection:mysql');
const parserStore = require('../parserStore')('mysql');
const { promisify } = require('util');
/**
* MySQL Connection Manager
......@@ -144,7 +145,7 @@ class ConnectionManager extends AbstractConnectionManager {
return Promise.resolve();
}
return Promise.fromCallback(callback => connection.end(callback));
return promisify(callback => connection.end(callback))();
}
validate(connection) {
......
......@@ -9,6 +9,7 @@ const sequelizeErrors = require('../../errors');
const semver = require('semver');
const dataTypes = require('../../data-types');
const moment = require('moment-timezone');
const { promisify } = require('util');
class ConnectionManager extends AbstractConnectionManager {
constructor(dialect, sequelize) {
......@@ -244,7 +245,7 @@ class ConnectionManager extends AbstractConnectionManager {
return Promise.resolve();
}
return Promise.fromCallback(callback => connection.end(callback));
return promisify(callback => connection.end(callback))();
}
validate(connection) {
......
......@@ -9,6 +9,7 @@ const debug = logger.debugContext('connection:sqlite');
const dataTypes = require('../../data-types').sqlite;
const sequelizeErrors = require('../../errors');
const parserStore = require('../parserStore')('sqlite');
const { promisify } = require('util');
class ConnectionManager extends AbstractConnectionManager {
constructor(dialect, sequelize) {
......@@ -27,7 +28,7 @@ class ConnectionManager extends AbstractConnectionManager {
_onProcessExit() {
const promises = Object.getOwnPropertyNames(this.connections)
.map(connection => Promise.fromCallback(callback => this.connections[connection].close(callback)));
.map(connection => promisify(callback => this.connections[connection].close(callback))());
return Promise
.all(promises)
......
......@@ -7,6 +7,7 @@ const Promise = require('./promise');
const DataTypes = require('./data-types');
const BelongsTo = require('./associations/belongs-to');
const validator = require('./utils/validator-extras').validator;
const { promisify } = require('util');
/**
* Instance Validator.
......@@ -259,9 +260,9 @@ class InstanceValidator {
if (isAsync) {
if (optAttrDefined) {
validatorFunction = Promise.promisify(validator.bind(this.modelInstance, invokeArgs));
validatorFunction = promisify(validator.bind(this.modelInstance, invokeArgs));
} else {
validatorFunction = Promise.promisify(validator.bind(this.modelInstance));
validatorFunction = promisify(validator.bind(this.modelInstance));
}
return validatorFunction()
.catch(e => this._pushError(false, errorKey, e, optValue, validatorType));
......
......@@ -1113,7 +1113,7 @@ class Sequelize {
// and reject with original error (ignore any error in rollback)
return Promise.resolve().then(() => {
if (!transaction.finished) return transaction.rollback().catch(() => {});
}).throw(err);
}).then(() => { throw err; });
});
});
}
......
This diff could not be displayed because it is too large.
......@@ -59,6 +59,7 @@
"chai-spies": "^1.x",
"cls-hooked": "^4.2.2",
"cross-env": "^7.0.2",
"delay": "^4.3.0",
"dtslint": "^2.0.5",
"env-cmd": "^10.1.0",
"esdoc": "^1.1.0",
......@@ -79,6 +80,8 @@
"mysql2": "^1.6.5",
"nyc": "^15.0.0",
"p-map": "^4.0.0",
"p-props": "^3.1.0",
"p-timeout": "^3.2.0",
"pg": "^7.8.1",
"pg-hstore": "^2.x",
"pg-types": "^2.0.0",
......
......@@ -6,7 +6,8 @@ const chai = require('chai'),
Sequelize = Support.Sequelize,
Promise = Sequelize.Promise,
cls = require('cls-hooked'),
current = Support.sequelize;
current = Support.sequelize,
delay = require('delay');
if (current.dialect.supports.transactions) {
describe(Support.getTestDialectTeaser('CLS (Async hooks)'), () => {
......@@ -73,7 +74,7 @@ if (current.dialect.supports.transactions) {
this.sequelize.transaction(() => {
transactionSetup = true;
return Promise.delay(500).then(() => {
return delay(500).then(() => {
expect(this.ns.get('transaction')).to.be.ok;
transactionEnded = true;
});
......
......@@ -7,7 +7,8 @@ const chai = require('chai'),
dialect = Support.getTestDialect(),
Sequelize = Support.Sequelize,
fs = require('fs'),
path = require('path');
path = require('path'),
{ promisify } = require('util');
let sqlite3;
if (dialect === 'sqlite') {
......@@ -74,11 +75,11 @@ describe(Support.getTestDialectTeaser('Configuration'), () => {
const createTableFoo = 'CREATE TABLE foo (faz TEXT);';
const createTableBar = 'CREATE TABLE bar (baz TEXT);';
const testAccess = Sequelize.Promise.method(() => {
return Sequelize.Promise.promisify(fs.access)(p, fs.R_OK | fs.W_OK);
});
const testAccess = () => {
return promisify(fs.access)(p, fs.R_OK | fs.W_OK);
};
return Sequelize.Promise.promisify(fs.unlink)(p)
return promisify(fs.unlink)(p)
.catch(err => {
expect(err.code).to.equal('ENOENT');
})
......@@ -135,7 +136,7 @@ describe(Support.getTestDialectTeaser('Configuration'), () => {
);
})
.finally(() => {
return Sequelize.Promise.promisify(fs.unlink)(p);
return promisify(fs.unlink)(p);
});
});
}
......
......@@ -8,7 +8,8 @@ const chai = require('chai'),
DataTypes = require('../../lib/data-types'),
_ = require('lodash'),
dialect = Support.getTestDialect(),
current = Support.sequelize;
current = Support.sequelize,
promiseProps = require('p-props');
const sortById = function(a, b) {
return a.id < b.id ? -1 : 1;
......@@ -265,7 +266,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
User.belongsTo(Group);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.props({
return promiseProps({
task: Task.create(),
user: User.create(),
group: Group.create()
......
......@@ -7,7 +7,8 @@ const chai = require('chai'),
expect = chai.expect,
Support = require('../support'),
DataTypes = require('../../../lib/data-types'),
_ = require('lodash');
_ = require('lodash'),
promiseProps = require('p-props');
const sortById = function(a, b) {
return a.id < b.id ? -1 : 1;
......@@ -631,7 +632,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
User.belongsTo(Order);
return this.sequelize.sync().then(() => {
return Promise.props({
return promiseProps({
users: User.bulkCreate([{}, {}, {}]).then(() => {
return User.findAll();
}),
......@@ -712,7 +713,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
Tag.belongsToMany(Product, { through: ProductTag });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.props({
return promiseProps({
products: Product.bulkCreate([
{ title: 'Chair' },
{ title: 'Desk' },
......@@ -766,7 +767,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
User.belongsTo(Group);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.props({
return promiseProps({
groups: Group.bulkCreate([{}, {}]).then(() => {
return Group.findAll();
}),
......@@ -797,7 +798,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
User.belongsTo(Group);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.props({
return promiseProps({
groups: Group.bulkCreate([
{ name: 'A' },
{ name: 'B' }
......@@ -835,7 +836,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
User.belongsTo(Group);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.props({
return promiseProps({
groups: Group.bulkCreate([
{ name: 'A' },
{ name: 'B' }
......@@ -916,7 +917,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
Group.hasMany(Category);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.props({
return promiseProps({
groups: Group.bulkCreate([
{ name: 'A' },
{ name: 'B' }
......@@ -969,7 +970,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
Group.hasMany(Category, { as: 'Tags' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.props({
return promiseProps({
groups: Group.bulkCreate([
{ name: 'A' },
{ name: 'B' }
......@@ -1022,7 +1023,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
Group.hasMany(Category);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.props({
return promiseProps({
groups: Group.bulkCreate([
{ name: 'A' },
{ name: 'B' }
......@@ -1071,7 +1072,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
User.hasOne(Project, { as: 'LeaderOf' });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.props({
return promiseProps({
projects: Project.bulkCreate([
{ title: 'Alpha' },
{ title: 'Beta' }
......@@ -1115,7 +1116,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
Tag.belongsToMany(Product, { through: ProductTag });
return this.sequelize.sync({ force: true }).then(() => {
return Promise.props({
return promiseProps({
products: Product.bulkCreate([
{ title: 'Chair' },
{ title: 'Desk' },
......@@ -1285,7 +1286,7 @@ describe(Support.getTestDialectTeaser('Include'), () => {
User.belongsTo(Group);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.props({
return promiseProps({
groups: Group.bulkCreate([
{ name: 'A' },
{ name: 'B' }
......
......@@ -8,7 +8,8 @@ const chai = require('chai'),
DataTypes = require('../../../lib/data-types'),
Promise = Sequelize.Promise,
dialect = Support.getTestDialect(),
_ = require('lodash');
_ = require('lodash'),
promiseProps = require('p-props');
const sortById = function(a, b) {
return a.id < b.id ? -1 : 1;
......@@ -1002,7 +1003,7 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), () => {
User.belongsTo(Group);
return this.sequelize.sync({ force: true }).then(() => {
return Promise.props({
return promiseProps({
groups: Group.bulkCreate([
{ name: 'A' },
{ name: 'B' }
......
......@@ -11,7 +11,8 @@ const chai = require('chai'),
Op = Sequelize.Op,
_ = require('lodash'),
assert = require('assert'),
current = Support.sequelize;
current = Support.sequelize,
pTimeout = require('p-timeout');
describe(Support.getTestDialectTeaser('Model'), () => {
beforeEach(function() {
......@@ -411,19 +412,15 @@ describe(Support.getTestDialectTeaser('Model'), () => {
if (times > 10) {
return true;
}
return this.Student.findOrCreate({
return pTimeout(this.Student.findOrCreate({
where: {
no: 1
}
})
.timeout(1000)
}), 1000)
.catch(e => {
if (!(e instanceof Promise.TimeoutError)) throw e;
throw new Error(e);
})
.catch(err => {
if (!(err instanceof Sequelize.ValidationError)) throw err;
return test(times + 1);
if (e instanceof Sequelize.ValidationError) return test(times + 1);
if (e instanceof pTimeout.TimeoutError) throw new Error(e);
throw e;
});
};
......
......@@ -11,7 +11,8 @@ const chai = require('chai'),
config = require('../../config/config'),
_ = require('lodash'),
moment = require('moment'),
current = Support.sequelize;
current = Support.sequelize,
promiseProps = require('p-props');
describe(Support.getTestDialectTeaser('Model'), () => {
beforeEach(function() {
......@@ -953,7 +954,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.Person.belongsTo(this.Country, { as: 'CountryResident', foreignKey: 'CountryResidentId' });
return this.sequelize.sync({ force: true }).then(() => {
return Sequelize.Promise.props({
return promiseProps({
europe: this.Continent.create({ name: 'Europe' }),
england: this.Country.create({ name: 'England' }),
coal: this.Industry.create({ name: 'Coal' }),
......@@ -1138,7 +1139,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.Person.belongsTo(this.Country, { as: 'CountryResident', foreignKey: 'CountryResidentId' });
return this.sequelize.sync({ force: true }).then(() => {
return Sequelize.Promise.props({
return promiseProps({
europe: this.Continent.create({ name: 'Europe' }),
asia: this.Continent.create({ name: 'Asia' }),
england: this.Country.create({ name: 'England' }),
......@@ -1297,7 +1298,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
this.Industry.belongsToMany(this.Country, { through: this.IndustryCountry });
return this.sequelize.sync({ force: true }).then(() => {
return Sequelize.Promise.props({
return promiseProps({
england: this.Country.create({ name: 'England' }),
france: this.Country.create({ name: 'France' }),
korea: this.Country.create({ name: 'Korea' }),
......
......@@ -6,6 +6,7 @@ const Support = require('./support');
const dialect = Support.getTestDialect();
const sinon = require('sinon');
const Sequelize = Support.Sequelize;
const delay = require('delay');
function assertSameConnection(newConnection, oldConnection) {
switch (dialect) {
......@@ -143,7 +144,7 @@ describe(Support.getTestDialectTeaser('Pooling'), () => {
await cm.releaseConnection(firstConnection);
// Wait a little and then get next available connection
await Sequelize.Promise.delay(90);
await delay(90);
const secondConnection = await cm.getConnection();
assertSameConnection(secondConnection, firstConnection);
......@@ -168,7 +169,7 @@ describe(Support.getTestDialectTeaser('Pooling'), () => {
await cm.releaseConnection(firstConnection);
// Wait a little and then get next available connection
await Sequelize.Promise.delay(110);
await delay(110);
const secondConnection = await cm.getConnection();
......
......@@ -5,7 +5,8 @@ const chai = require('chai'),
Support = require('./support'),
Promise = require('../../lib/promise'),
Transaction = require('../../lib/transaction'),
current = Support.sequelize;
current = Support.sequelize,
delay = require('delay');
if (current.dialect.supports.transactions) {
......@@ -101,7 +102,7 @@ if (current.dialect.supports.transactions) {
return Test
.create({ name: 'Peter' }, { transaction })
.then(() => {
return Promise.delay(1000).then(() => {
return delay(1000).then(() => {
return transaction
.commit()
.then(() => { return Test.count(); })
......@@ -141,7 +142,7 @@ if (current.dialect.supports.transactions) {
expect(err).to.be.ok;
return t2.rollback();
}),
Promise.delay(100).then(() => {
delay(100).then(() => {
return t1.commit();
})
]);
......
......@@ -9,7 +9,8 @@ const chai = require('chai'),
QueryTypes = require('../../lib/query-types'),
Transaction = require('../../lib/transaction'),
sinon = require('sinon'),
current = Support.sequelize;
current = Support.sequelize,
delay = require('delay');
if (current.dialect.supports.transactions) {
......@@ -393,7 +394,7 @@ if (current.dialect.supports.transactions) {
lock: 'UPDATE',
transaction
})
.then(() => Promise.delay(10))
.then(() => delay(10))
.then(() => {
return Task.update({ id: to }, {
where: {
......@@ -513,7 +514,7 @@ if (current.dialect.supports.transactions) {
const newTransactionFunc = function() {
return sequelize.transaction({ type: Support.Sequelize.Transaction.TYPES.EXCLUSIVE, retry: { match: ['NO_MATCH'] } }).then(t => {
// introduce delay to force the busy state race condition to fail
return Promise.delay(1000).then(() => {
return delay(1000).then(() => {
return User.create({ id: null, username: `test ${t.id}` }, { transaction: t }).then(() => {
return t.commit();
});
......@@ -586,7 +587,7 @@ if (current.dialect.supports.transactions) {
username: 'jan'
}
}).then(() => expect(transactionSpy).to.have.been.called ), // Update should not succeed before transaction has committed
Promise.delay(2000)
delay(2000)
.then(() => transaction.commit())
.then(transactionSpy)
));
......@@ -642,7 +643,7 @@ if (current.dialect.supports.transactions) {
transaction: t1
}).then(() => {
t1Spy();
return Promise.delay(2000).then(() => {
return delay(2000).then(() => {
return t1.commit();
});
})
......@@ -827,7 +828,7 @@ if (current.dialect.supports.transactions) {
}, {
transaction: t1
}).then(() => {
return Promise.delay(2000).then(() => {
return delay(2000).then(() => {
t1Spy();
expect(t1Spy).to.have.been.calledAfter(t2Spy);
return t1.commit();
......@@ -890,7 +891,7 @@ if (current.dialect.supports.transactions) {
}, {
transaction: t1
}).then(() => {
return Promise.delay(2000).then(() => {
return delay(2000).then(() => {
t1Spy();
return t1.commit();
});
......
......@@ -4,7 +4,8 @@ const ResourceLock = require('../../../../lib/dialects/mssql/resource-lock'),
Promise = require('../../../../lib/promise'),
assert = require('assert'),
Support = require('../../support'),
dialect = Support.getTestDialect();
dialect = Support.getTestDialect(),
delay = require('delay');
if (dialect === 'mssql') {
describe('[MSSQL Specific] ResourceLock', () => {
......@@ -23,7 +24,7 @@ if (dialect === 'mssql') {
assert.equal(last, 0);
last = 1;
return Promise.delay(15);
return delay(15);
}),
Promise.using(lock.lock(), resource => {
validateResource(resource);
......@@ -35,7 +36,7 @@ if (dialect === 'mssql') {
assert.equal(last, 2);
last = 3;
return Promise.delay(5);
return delay(5);
})
]);
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!