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

Commit 1c412756 by Sascha Depold

Merge pull request #1644 from sequelize/hotfix/transaction-support-for-association-setter

Fix for #1637
2 parents 4ac8c613 5d912233
...@@ -52,7 +52,7 @@ module.exports = (function() { ...@@ -52,7 +52,7 @@ module.exports = (function() {
if (this.options.constraints !== false) { if (this.options.constraints !== false) {
this.options.onDelete = this.options.onDelete || 'SET NULL' this.options.onDelete = this.options.onDelete || 'SET NULL'
this.options.onUpdate = this.options.onUpdate || 'CASCADE' this.options.onUpdate = this.options.onUpdate || 'CASCADE'
} }
Helpers.addForeignKeyConstraints(this.target.rawAttributes[this.identifier], this.source, this.target, this.options) Helpers.addForeignKeyConstraints(this.target.rawAttributes[this.identifier], this.source, this.target, this.options)
// Sync attributes and setters/getters to DAO prototype // Sync attributes and setters/getters to DAO prototype
...@@ -87,32 +87,34 @@ module.exports = (function() { ...@@ -87,32 +87,34 @@ module.exports = (function() {
var instance = this var instance = this
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
instance[association.accessors.get]().success(function(oldInstance) { instance[association.accessors.get](options)
if (oldInstance) { .success(function(oldInstance) {
oldInstance[association.identifier] = null if (oldInstance) {
oldInstance oldInstance[association.identifier] = null
.save(Utils._.extend({}, options, { oldInstance
fields: [association.identifier], .save(Utils._.extend({}, options, {
allowNull: [association.identifier], fields: [association.identifier],
association: true allowNull: [association.identifier],
})) association: true
.success(function() { }))
if (associatedInstance) { .success(function() {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier)) if (associatedInstance) {
associatedInstance.save(options).proxy(emitter) associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
} else { associatedInstance.save(options).proxy(emitter)
emitter.emit('success', null) } else {
} emitter.emit('success', null)
}) }
} else { })
if (associatedInstance) {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
associatedInstance.save(options).proxy(emitter)
} else { } else {
emitter.emit('success', null) if (associatedInstance) {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
associatedInstance.save(options).proxy(emitter)
} else {
emitter.emit('success', null)
}
} }
} })
}) .proxy(emitter, { events: ['sql'] })
}).run() }).run()
} }
......
...@@ -24,7 +24,7 @@ module.exports = (function() { ...@@ -24,7 +24,7 @@ module.exports = (function() {
} }
this.client.query(this.sql, function(err, results, fields) { this.client.query(this.sql, function(err, results, fields) {
this.emit('sql', this.sql) this.emit('sql', this.sql, this.options.uuid)
if (err) { if (err) {
err.sql = sql err.sql = sql
...@@ -38,5 +38,3 @@ module.exports = (function() { ...@@ -38,5 +38,3 @@ module.exports = (function() {
return Query return Query
})() })()
...@@ -38,12 +38,12 @@ module.exports = (function() { ...@@ -38,12 +38,12 @@ module.exports = (function() {
query.on('error', function(err) { query.on('error', function(err) {
receivedError = true receivedError = true
err.sql = sql err.sql = sql
self.emit('sql', sql) self.emit('sql', sql, self.options.uuid)
self.emit('error', err, self.callee) self.emit('error', err, self.callee)
}) })
query.on('end', function(result) { query.on('end', function(result) {
self.emit('sql', self.sql) self.emit('sql', self.sql, self.options.uuid)
if (receivedError) { if (receivedError) {
return return
......
...@@ -36,13 +36,13 @@ module.exports = (function() { ...@@ -36,13 +36,13 @@ module.exports = (function() {
if (self.sql.indexOf('-- ') === 0) { if (self.sql.indexOf('-- ') === 0) {
// the sql query starts with a comment. don't bother the server with that ... // the sql query starts with a comment. don't bother the server with that ...
Utils.tick(function() { Utils.tick(function() {
self.emit('sql', self.sql) self.emit('sql', self.sql, self.options.uuid)
self.emit('success', null) self.emit('success', null)
}) })
} else { } else {
self.database[getDatabaseMethod.call(self)](self.sql, function(err, results) { self.database[getDatabaseMethod.call(self)](self.sql, function(err, results) {
// allow clients to listen to sql to do their own logging or whatnot // allow clients to listen to sql to do their own logging or whatnot
self.emit('sql', self.sql) self.emit('sql', self.sql, self.options.uuid)
if (err) { if (err) {
err.sql = self.sql err.sql = self.sql
......
...@@ -127,8 +127,9 @@ module.exports = (function() { ...@@ -127,8 +127,9 @@ module.exports = (function() {
options.events = Utils._.difference(options.events, options.skipEvents) options.events = Utils._.difference(options.events, options.skipEvents)
options.events.forEach(function (eventKey) { options.events.forEach(function (eventKey) {
this.on(eventKey, function (result) { this.on(eventKey, function () {
emitter.emit(eventKey, result) var args = [ eventKey ].concat([].slice.apply(arguments))
emitter.emit.apply(emitter, args)
}) })
}.bind(this)) }.bind(this))
......
...@@ -892,9 +892,7 @@ module.exports = (function() { ...@@ -892,9 +892,7 @@ module.exports = (function() {
this.emit(methodName, err) this.emit(methodName, err)
emitter.emit('error', err) emitter.emit('error', err)
}.bind(this)) }.bind(this))
.on('sql', function(sql) { .proxy(emitter, { events: ['sql'] })
emitter.emit('sql', sql)
})
}.bind(this) }.bind(this)
if (!!emitter) { if (!!emitter) {
......
...@@ -90,14 +90,19 @@ describe(Support.getTestDialectTeaser("HasOne"), function() { ...@@ -90,14 +90,19 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
User.create({ username: 'foo' }).success(function(user) { User.create({ username: 'foo' }).success(function(user) {
Group.create({ name: 'bar' }).success(function(group) { Group.create({ name: 'bar' }).success(function(group) {
sequelize.transaction(function(t) { sequelize.transaction(function(t) {
group.setUser(user, { transaction: t }).success(function() { group
Group.all().success(function(groups) { .setUser(user, { transaction: t })
groups[0].getUser().success(function(associatedUser) { .success(function() {
expect(associatedUser).to.be.null Group.all().success(function(groups) {
t.rollback().success(function() { done() }) groups[0].getUser().success(function(associatedUser) {
expect(associatedUser).to.be.null
t.rollback().success(function() { done() })
})
}) })
}) })
}) .on('sql', function(sql, uuid) {
expect(uuid).to.not.equal('default')
})
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!