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

Commit f778e402 by Jan Aagaard Meier

Add changelog for #5298. Replaced delay in upsert test with fake timers

1 parent 11a92a2b
# Future # Future
- [FIXED] Prevent race condition after transaction finished [#5222](https://github.com/sequelize/sequelize/issues/5222) - [FIXED] Prevent race condition after transaction finished [#5222](https://github.com/sequelize/sequelize/issues/5222)
# Future
- [FIXED] Fixed Instance.reload issues ([#4844](https://github.com/sequelize/sequelize/issues/4844) and [#4452](https://github.com/sequelize/sequelize/issues/4452)) - [FIXED] Fixed Instance.reload issues ([#4844](https://github.com/sequelize/sequelize/issues/4844) and [#4452](https://github.com/sequelize/sequelize/issues/4452))
- [FIXED] Fix upsert when primary key contains `.field` (internal API change for `queryInterface.upsert`) [#4755](https://github.com/sequelize/sequelize/issues/4755)
# 3.18.0 # 3.18.0
- [ADDED] Support silent: true in bulk update [#5200](https://github.com/sequelize/sequelize/issues/5200) - [ADDED] Support silent: true in bulk update [#5200](https://github.com/sequelize/sequelize/issues/5200)
......
...@@ -2048,7 +2048,7 @@ Model.prototype.upsert = function (values, options) { ...@@ -2048,7 +2048,7 @@ Model.prototype.upsert = function (values, options) {
delete updateValues[this.primaryKeyField]; delete updateValues[this.primaryKeyField];
} }
return this.QueryInterface.upsert(this.getTableName(options), insertValues, updateValues, this, options); return this.QueryInterface.upsert(this.getTableName(options), insertValues, updateValues, instance.where(), this, options);
}); });
}; };
......
...@@ -495,24 +495,12 @@ QueryInterface.prototype.insert = function(instance, tableName, values, options) ...@@ -495,24 +495,12 @@ QueryInterface.prototype.insert = function(instance, tableName, values, options)
}); });
}; };
QueryInterface.prototype.upsert = function(tableName, valuesByField, updateValues, model, options) { QueryInterface.prototype.upsert = function(tableName, valuesByField, updateValues, where, model, options) {
var wheres = [] var wheres = []
, where
, indexFields , indexFields
, indexes = [] , indexes = []
, attributes = Object.keys(valuesByField); , attributes = Object.keys(valuesByField);
where = model.primaryKeyAttributes.reduce(function (where, key) {
var attribute = model.rawAttributes[key];
if (attribute.field && attribute.field in valuesByField) {
where[attribute.field] = valuesByField[attribute.field];
} else if (key in valuesByField) {
where[key] = valuesByField[key];
}
return where;
}, {} );
if (!Utils._.isEmpty(where)) { if (!Utils._.isEmpty(where)) {
wheres.push(where); wheres.push(where);
} }
......
...@@ -12,6 +12,10 @@ var chai = require('chai') ...@@ -12,6 +12,10 @@ var chai = require('chai')
, current = Support.sequelize; , current = Support.sequelize;
describe(Support.getTestDialectTeaser('Model'), function() { describe(Support.getTestDialectTeaser('Model'), function() {
before(function () {
this.clock = sinon.useFakeTimers();
});
beforeEach(function() { beforeEach(function() {
this.User = this.sequelize.define('user', { this.User = this.sequelize.define('user', {
username: DataTypes.STRING, username: DataTypes.STRING,
...@@ -47,6 +51,10 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -47,6 +51,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
return this.sequelize.sync({ force: true }); return this.sequelize.sync({ force: true });
}); });
after(function () {
this.clock.restore();
});
if (current.dialect.supports.upserts) { if (current.dialect.supports.upserts) {
describe('upsert', function() { describe('upsert', function() {
it('works with upsert on id', function() { it('works with upsert on id', function() {
...@@ -57,9 +65,8 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -57,9 +65,8 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(created).to.be.ok; expect(created).to.be.ok;
} }
return this.sequelize.Promise.delay(1000).bind(this).then(function() { this.clock.tick(1000);
return this.User.upsert({ id: 42, username: 'doe' }); return this.User.upsert({ id: 42, username: 'doe' });
});
}).then(function(created) { }).then(function(created) {
if (dialect === 'sqlite') { if (dialect === 'sqlite') {
expect(created).to.be.undefined; expect(created).to.be.undefined;
...@@ -83,9 +90,8 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -83,9 +90,8 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(created).to.be.ok; expect(created).to.be.ok;
} }
return this.sequelize.Promise.delay(1000).bind(this).then(function() { this.clock.tick(1000);
return this.User.upsert({ foo: 'baz', bar: 19, username: 'doe' }); return this.User.upsert({ foo: 'baz', bar: 19, username: 'doe' });
});
}).then(function(created) { }).then(function(created) {
if (dialect === 'sqlite') { if (dialect === 'sqlite') {
expect(created).to.be.undefined; expect(created).to.be.undefined;
...@@ -149,10 +155,10 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -149,10 +155,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(created2).to.be.ok; expect(created2).to.be.ok;
} }
return Promise.delay(1000).bind(this).then(function() {
// Update the first one this.clock.tick(1000);
return User.upsert({ a: 'a', b: 'b', username: 'doe' }); // Update the first one
}); return User.upsert({ a: 'a', b: 'b', username: 'doe' });
}).then(function(created) { }).then(function(created) {
if (dialect === 'sqlite') { if (dialect === 'sqlite') {
expect(created).to.be.undefined; expect(created).to.be.undefined;
...@@ -196,9 +202,9 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -196,9 +202,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(created).to.be.ok; expect(created).to.be.ok;
} }
return this.sequelize.Promise.delay(1000).bind(this).then(function() {
return this.User.upsert({ id: 42, username: 'doe', blob: new Buffer('andrea') }); this.clock.tick(1000);
}); return this.User.upsert({ id: 42, username: 'doe', blob: new Buffer('andrea') });
}).then(function(created) { }).then(function(created) {
if (dialect === 'sqlite') { if (dialect === 'sqlite') {
expect(created).to.be.undefined; expect(created).to.be.undefined;
...@@ -223,9 +229,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -223,9 +229,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(created).to.be.ok; expect(created).to.be.ok;
} }
return this.sequelize.Promise.delay(1000).bind(this).then(function() { return this.User.upsert({ id: 42, baz: 'oof' });
return this.User.upsert({ id: 42, baz: 'oof' });
});
}).then(function(created) { }).then(function(created) {
if (dialect === 'sqlite') { if (dialect === 'sqlite') {
expect(created).to.be.undefined; expect(created).to.be.undefined;
...@@ -247,9 +251,9 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -247,9 +251,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(created).to.be.ok; expect(created).to.be.ok;
} }
return this.sequelize.Promise.delay(1000).bind(this).then(function() {
return this.ModelWithFieldPK.upsert({ userId: 42, foo: 'second' }); this.clock.tick(1000);
}); return this.ModelWithFieldPK.upsert({ userId: 42, foo: 'second' });
}).then(function(created) { }).then(function(created) {
if (dialect === 'sqlite') { if (dialect === 'sqlite') {
expect(created).to.be.undefined; expect(created).to.be.undefined;
...@@ -270,9 +274,10 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -270,9 +274,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
} else { } else {
expect(created).to.be.ok; expect(created).to.be.ok;
} }
return this.sequelize.Promise.delay(1000).bind(this).then(function() {
return this.User.upsert({ id: 42, username: 'doe', foo: this.sequelize.fn('upper', 'mixedCase2') });
}); this.clock.tick(1000);
return this.User.upsert({ id: 42, username: 'doe', foo: this.sequelize.fn('upper', 'mixedCase2') });
}).then(function(created) { }).then(function(created) {
if (dialect === 'sqlite') { if (dialect === 'sqlite') {
expect(created).to.be.undefined; expect(created).to.be.undefined;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!