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

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() {
if (this.options.constraints !== false) {
this.options.onDelete = this.options.onDelete || 'SET NULL'
this.options.onUpdate = this.options.onUpdate || 'CASCADE'
}
}
Helpers.addForeignKeyConstraints(this.target.rawAttributes[this.identifier], this.source, this.target, this.options)
// Sync attributes and setters/getters to DAO prototype
......@@ -87,32 +87,34 @@ module.exports = (function() {
var instance = this
return new Utils.CustomEventEmitter(function(emitter) {
instance[association.accessors.get]().success(function(oldInstance) {
if (oldInstance) {
oldInstance[association.identifier] = null
oldInstance
.save(Utils._.extend({}, options, {
fields: [association.identifier],
allowNull: [association.identifier],
association: true
}))
.success(function() {
if (associatedInstance) {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
associatedInstance.save(options).proxy(emitter)
} else {
emitter.emit('success', null)
}
})
} else {
if (associatedInstance) {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
associatedInstance.save(options).proxy(emitter)
instance[association.accessors.get](options)
.success(function(oldInstance) {
if (oldInstance) {
oldInstance[association.identifier] = null
oldInstance
.save(Utils._.extend({}, options, {
fields: [association.identifier],
allowNull: [association.identifier],
association: true
}))
.success(function() {
if (associatedInstance) {
associatedInstance.set(association.identifier, instance.get(association.sourceIdentifier))
associatedInstance.save(options).proxy(emitter)
} else {
emitter.emit('success', null)
}
})
} 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()
}
......
......@@ -24,7 +24,7 @@ module.exports = (function() {
}
this.client.query(this.sql, function(err, results, fields) {
this.emit('sql', this.sql)
this.emit('sql', this.sql, this.options.uuid)
if (err) {
err.sql = sql
......@@ -38,5 +38,3 @@ module.exports = (function() {
return Query
})()
......@@ -38,12 +38,12 @@ module.exports = (function() {
query.on('error', function(err) {
receivedError = true
err.sql = sql
self.emit('sql', sql)
self.emit('sql', sql, self.options.uuid)
self.emit('error', err, self.callee)
})
query.on('end', function(result) {
self.emit('sql', self.sql)
self.emit('sql', self.sql, self.options.uuid)
if (receivedError) {
return
......
......@@ -36,13 +36,13 @@ module.exports = (function() {
if (self.sql.indexOf('-- ') === 0) {
// the sql query starts with a comment. don't bother the server with that ...
Utils.tick(function() {
self.emit('sql', self.sql)
self.emit('sql', self.sql, self.options.uuid)
self.emit('success', null)
})
} else {
self.database[getDatabaseMethod.call(self)](self.sql, function(err, results) {
// 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) {
err.sql = self.sql
......
......@@ -127,8 +127,9 @@ module.exports = (function() {
options.events = Utils._.difference(options.events, options.skipEvents)
options.events.forEach(function (eventKey) {
this.on(eventKey, function (result) {
emitter.emit(eventKey, result)
this.on(eventKey, function () {
var args = [ eventKey ].concat([].slice.apply(arguments))
emitter.emit.apply(emitter, args)
})
}.bind(this))
......
......@@ -892,9 +892,7 @@ module.exports = (function() {
this.emit(methodName, err)
emitter.emit('error', err)
}.bind(this))
.on('sql', function(sql) {
emitter.emit('sql', sql)
})
.proxy(emitter, { events: ['sql'] })
}.bind(this)
if (!!emitter) {
......
......@@ -90,14 +90,19 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
User.create({ username: 'foo' }).success(function(user) {
Group.create({ name: 'bar' }).success(function(group) {
sequelize.transaction(function(t) {
group.setUser(user, { transaction: t }).success(function() {
Group.all().success(function(groups) {
groups[0].getUser().success(function(associatedUser) {
expect(associatedUser).to.be.null
t.rollback().success(function() { done() })
group
.setUser(user, { transaction: t })
.success(function() {
Group.all().success(function(groups) {
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!