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

Commit 93016a05 by Sascha Depold

Merge pull request #738 from durango/bulkcreate

bulkCreate would have problems with a disparate field list. Closes #736
2 parents 0e5b69db dec38af4
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
- [BUG] Fields should be escaped by quoteIdentifier for max/min functions which allows SQL reserved keywords to be used. [#719](https://github.com/sequelize/sequelize/pull/719). thanks to durango - [BUG] Fields should be escaped by quoteIdentifier for max/min functions which allows SQL reserved keywords to be used. [#719](https://github.com/sequelize/sequelize/pull/719). thanks to durango
- [BUG] Fixed bug when trying to save objects with eagerly loaded attributes [#716](https://github.com/sequelize/sequelize/pull/716). thanks to iamjochen - [BUG] Fixed bug when trying to save objects with eagerly loaded attributes [#716](https://github.com/sequelize/sequelize/pull/716). thanks to iamjochen
- [BUG] Strings for .find() should be fixed. Also added support for string primary keys to be found easily. [#737](https://github.com/sequelize/sequelize/pull/737). thanks to durango - [BUG] Strings for .find() should be fixed. Also added support for string primary keys to be found easily. [#737](https://github.com/sequelize/sequelize/pull/737). thanks to durango
- [BUG] bulkCreate would have problems with a disparate field list [#738](https://github.com/sequelize/sequelize/pull/738). thanks to durango
- [BUG] Fixed problems with quoteIdentifiers and {raw: false} option on raw queries [#751](https://github.com/sequelize/sequelize/pull/751). thanks to janmeier - [BUG] Fixed problems with quoteIdentifiers and {raw: false} option on raw queries [#751](https://github.com/sequelize/sequelize/pull/751). thanks to janmeier
- [BUG] Fixed SQL escaping with sqlite and unified escaping [#700](https://github.com/sequelize/sequelize/pull/700). thanks to PiPeep - [BUG] Fixed SQL escaping with sqlite and unified escaping [#700](https://github.com/sequelize/sequelize/pull/700). thanks to PiPeep
- [FEATURE] Validate a model before it gets saved. [#601](https://github.com/sequelize/sequelize/pull/601). thanks to durango - [FEATURE] Validate a model before it gets saved. [#601](https://github.com/sequelize/sequelize/pull/601). thanks to durango
......
...@@ -440,39 +440,26 @@ module.exports = (function() { ...@@ -440,39 +440,26 @@ module.exports = (function() {
, updatedAtAttr = self.options.underscored ? 'updated_at' : 'updatedAt' , updatedAtAttr = self.options.underscored ? 'updated_at' : 'updatedAt'
, createdAtAttr = self.options.underscored ? 'created_at' : 'createdAt' , createdAtAttr = self.options.underscored ? 'created_at' : 'createdAt'
fields = fields || []
// we will re-create from DAOs, which may have set up default attributes // we will re-create from DAOs, which may have set up default attributes
records = [] records = []
var found = false
if (fields) { daos.forEach(function(dao) {
var values = fields.length > 0 ? {} : dao.dataValues
// Always insert updated and created time stamps fields.forEach(function(field) {
if (self.options.timestamps) { values[field] = dao.dataValues[field]
if (fields.indexOf(updatedAtAttr) === -1) { })
fields.push(updatedAtAttr)
}
if (fields.indexOf(createdAtAttr) === -1) { if (self.options.timestamps) {
fields.push(createdAtAttr) values[createdAtAttr] = Utils.now()
} values[updatedAtAttr] = Utils.now()
} }
// Build records for the fields we know about records.push(values);
daos.forEach(function(dao) { })
var values = {};
fields.forEach(function(field) {
values[field] = dao.dataValues[field]
})
if (self.options.timestamps) {
values[updatedAtAttr] = Utils.now()
}
records.push(values);
})
} else {
daos.forEach(function(dao) {
records.push(dao.dataValues)
})
}
// Validate enums // Validate enums
records.forEach(function(values) { records.forEach(function(values) {
......
...@@ -401,6 +401,20 @@ module.exports = (function() { ...@@ -401,6 +401,20 @@ module.exports = (function() {
for (key in attrs) { for (key in attrs) {
this.addAttribute(key, attrs[key]) this.addAttribute(key, attrs[key])
} }
// this.addAttributes COMPLETELY destroys the structure of our DAO due to __defineGetter__ resetting the object
// so now we have to rebuild for bulkInserts, bulkUpdates, etc.
var rebuild = {}
// Get the correct map....
Utils._.each(this.attributes, function(key) {
if (this.dataValues.hasOwnProperty(key)) {
rebuild[key] = this.dataValues[key]
}
}.bind(this))
// This allows for aliases, etc.
this.dataValues = Utils._.extend(rebuild, this.dataValues)
} }
return DAO return DAO
......
...@@ -436,7 +436,7 @@ describe(Helpers.getTestDialectTeaser("HasMany"), function() { ...@@ -436,7 +436,7 @@ describe(Helpers.getTestDialectTeaser("HasMany"), function() {
this.Task.create({ title: 'task2' }).success(function(task2) { this.Task.create({ title: 'task2' }).success(function(task2) {
user.setTasks([task1, task2]).on('sql', spy).on('sql', _.after(2, function (sql) { user.setTasks([task1, task2]).on('sql', spy).on('sql', _.after(2, function (sql) {
expect(sql).toMatch("INSERT INTO") expect(sql).toMatch("INSERT INTO")
expect(sql).toMatch("VALUES (1,1),(1,2)") expect(sql).toMatch("VALUES (1,1),(2,1)")
})).success(function () { })).success(function () {
expect(spy).toHaveBeenCalledTwice() // Once for SELECT, once for INSERT into expect(spy).toHaveBeenCalledTwice() // Once for SELECT, once for INSERT into
done() done()
......
...@@ -576,6 +576,23 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() { ...@@ -576,6 +576,23 @@ describe(Helpers.getTestDialectTeaser("DAOFactory"), function() {
}) })
describe('bulkCreate', function() { describe('bulkCreate', function() {
it('properly handles disparate field lists', function(done) {
var self = this
, data = [{username: 'Peter', secretValue: '42' },
{username: 'Paul'},
{username: 'Steve'}]
this.User.bulkCreate(data).success(function() {
self.User.findAll({where: {username: 'Paul'}}).success(function(users) {
expect(users.length).toEqual(1)
expect(users[0].username).toEqual("Paul")
expect(users[0].secretValue).toBeNull()
done()
})
})
})
it('inserts multiple values respecting the white list', function(done) { it('inserts multiple values respecting the white list', function(done) {
var self = this var self = this
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!