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

Commit 2443a317 by Mick Hansen

Merge pull request #1313 from schonfeld/insert-ignore-support

Bulk Insert / Ignore Option Edit
2 parents a14b2695 e4b0af02
......@@ -790,7 +790,8 @@ module.exports = (function() {
options = Utils._.extend({
validate: false,
hooks: false
hooks: false,
ignoreDuplicates: false
}, options || {})
if (fieldsOrOptions instanceof Array) {
......@@ -800,6 +801,12 @@ module.exports = (function() {
options = Utils._.extend(options, fieldsOrOptions)
}
if(this.daoFactoryManager.sequelize.options.dialect === 'postgres' && options.ignoreDuplicates ) {
return new Utils.CustomEventEmitter(function(emitter) {
emitter.emit('error', new Error('Postgres does not support the \'ignoreDuplicates\' option.'))
}).run();
}
var self = this
, updatedAtAttr = Utils._.underscoredIf(self.options.updatedAt, self.options.underscored)
, createdAtAttr = Utils._.underscoredIf(self.options.createdAt, self.options.underscored)
......
......@@ -176,8 +176,8 @@ module.exports = (function() {
return Utils._.template(query)({ tableName: tableName, attributes: attrString.join(', ') })
},
bulkInsertQuery: function(tableName, attrValueHashes) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
bulkInsertQuery: function(tableName, attrValueHashes, options) {
var query = "INSERT<%= ignoreDuplicates %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
, tuples = []
, allAttributes = []
......@@ -196,6 +196,7 @@ module.exports = (function() {
}.bind(this))
var replacements = {
ignoreDuplicates: options && options.ignoreDuplicates ? ' IGNORE' : '',
table: this.quoteIdentifier(tableName),
attributes: allAttributes.map(function(attr){
return this.quoteIdentifier(attr)
......
......@@ -267,7 +267,7 @@ module.exports = (function() {
})
},
bulkInsertQuery: function(tableName, attrValueHashes) {
bulkInsertQuery: function(tableName, attrValueHashes, options) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %> RETURNING *;"
, tuples = []
, serials = []
......
......@@ -172,8 +172,8 @@ module.exports = (function() {
return "SELECT name FROM sqlite_master WHERE type='table' and name!='sqlite_sequence';"
},
bulkInsertQuery: function(tableName, attrValueHashes) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
bulkInsertQuery: function(tableName, attrValueHashes, options) {
var query = "INSERT<%= ignoreDuplicates %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
, tuples = []
, allAttributes = []
......@@ -192,6 +192,7 @@ module.exports = (function() {
}.bind(this))
var replacements = {
ignoreDuplicates: options && options.ignoreDuplicates ? ' OR IGNORE' : '',
table: this.quoteIdentifier(tableName),
attributes: allAttributes.map(function(attr){
return this.quoteIdentifier(attr)
......
......@@ -510,7 +510,7 @@ module.exports = (function() {
}
QueryInterface.prototype.bulkInsert = function(tableName, records, options) {
var sql = this.QueryGenerator.bulkInsertQuery(tableName, records)
var sql = this.QueryGenerator.bulkInsertQuery(tableName, records, options)
return queryAndEmit.call(this, [sql, null, options], 'bulkInsert')
}
......
......@@ -24,7 +24,8 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
data: DataTypes.STRING,
intVal: DataTypes.INTEGER,
theDate: DataTypes.DATE,
aBool: DataTypes.BOOLEAN
aBool: DataTypes.BOOLEAN,
uniqueName: { type: DataTypes.STRING, unique: true }
})
this.User.sync({ force: true }).success(function() {
......@@ -1004,6 +1005,47 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
if (Support.getTestDialect() !== 'postgres') {
it("should support the ignoreDuplicates option", function(done) {
var self = this
, data = [{ uniqueName: 'Peter', secretValue: '42' },
{ uniqueName: 'Paul', secretValue: '23' }]
this.User.bulkCreate(data, { fields: ['uniqueName', 'secretValue'] }).success(function() {
data.push({ uniqueName: 'Michael', secretValue: '26' });
self.User.bulkCreate(data, { fields: ['uniqueName', 'secretValue'], ignoreDuplicates: true }).success(function() {
self.User.findAll({order: 'id'}).success(function(users) {
expect(users.length).to.equal(3)
expect(users[0].uniqueName).to.equal("Peter")
expect(users[0].secretValue).to.equal("42");
expect(users[1].uniqueName).to.equal("Paul")
expect(users[1].secretValue).to.equal("23");
expect(users[2].uniqueName).to.equal("Michael")
expect(users[2].secretValue).to.equal("26");
done()
});
});
})
})
} else {
it("should throw an error when the ignoreDuplicates option is passed", function(done) {
var self = this
, data = [{ uniqueName: 'Peter', secretValue: '42' },
{ uniqueName: 'Paul', secretValue: '23' }]
this.User.bulkCreate(data, { fields: ['uniqueName', 'secretValue'] }).success(function() {
data.push({ uniqueName: 'Michael', secretValue: '26' });
self.User.bulkCreate(data, { fields: ['uniqueName', 'secretValue'], ignoreDuplicates: true }).error(function(err) {
expect(err).to.exist
expect(err.message).to.match(/Postgres does not support the \'ignoreDuplicates\' option./)
done();
})
})
})
}
describe('enums', function() {
it('correctly restores enum values', function(done) {
var self = this
......
......@@ -389,6 +389,9 @@ if (Support.dialectIsMySQL()) {
}, {
arguments: ['myTable', [{name: "foo", value: true}, {name: 'bar', value: false}]],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',true),('bar',false);"
}, {
arguments: ['myTable', [{name: 'foo'}, {name: 'bar'}], {ignoreDuplicates: true}],
expectation: "INSERT IGNORE INTO `myTable` (`name`) VALUES ('foo'),('bar');"
}
],
......
......@@ -385,6 +385,9 @@ if (dialect === 'sqlite') {
arguments: ['myTable', [{name: 'foo', foo: 1, nullValue: null}, {name: 'bar', foo: 2, nullValue: null}]],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL),('bar',2,NULL);",
context: {options: {omitNull: true}} // Note: As above
}, {
arguments: ['myTable', [{name: 'foo'}, {name: 'bar'}], {ignoreDuplicates: true}],
expectation: "INSERT OR IGNORE INTO `myTable` (`name`) VALUES ('foo'),('bar');"
}
],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!