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

Commit 3b9faacf by Michael Schonfeld

add support for mysql insert ignore + tests

1 parent 07c67c43
......@@ -778,7 +778,8 @@ module.exports = (function() {
options = Utils._.extend({
validate: false,
hooks: false
hooks: false,
ignore: false
}, options || {})
if (fieldsOrOptions instanceof Array) {
......
......@@ -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<%= ignore %> INTO <%= table %> (<%= attributes %>) VALUES <%= tuples %>;"
, tuples = []
, allAttributes = []
......@@ -196,6 +196,7 @@ module.exports = (function() {
}.bind(this))
var replacements = {
ignore: options && options.ignore ? ' IGNORE' : '',
table: this.quoteIdentifier(tableName),
attributes: allAttributes.map(function(attr){
return this.quoteIdentifier(attr)
......
......@@ -480,7 +480,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() {
......@@ -990,6 +991,28 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
it("should support the insert ignore 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'], ignore: 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()
});
});
})
})
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'}], {ignore: true}],
expectation: "INSERT 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!