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

You need to sign in or sign up before continuing.
Commit 7895f286 by Jan Aagaard Meier

Merge pull request #2789 from janmeier/master

Quote the name of unique indexes, and allow newlines in uniqe key names
2 parents b51e4443 e6d51a90
......@@ -82,7 +82,7 @@ module.exports = (function() {
if (!Utils._.isString(indexName)) {
indexName = 'uniq_' + tableName + '_' + columns.fields.join('_');
}
values.attributes += ', CONSTRAINT ' + indexName + ' UNIQUE (' + Utils._.map(columns.fields, self.quoteIdentifier).join(', ') + ')';
values.attributes += ', CONSTRAINT ' + self.quoteIdentifier(indexName) + ' UNIQUE (' + Utils._.map(columns.fields, self.quoteIdentifier).join(', ') + ')';
});
}
......
......@@ -163,7 +163,7 @@ module.exports = (function() {
Query.prototype.formatError = function (err) {
var match;
match = err.message.match(/Violation of UNIQUE KEY constraint '(.*)'. Cannot insert duplicate key in object '.*'. The duplicate key value is \((.*)\)./);
match = err.message.match(/Violation of UNIQUE KEY constraint '((.|\s)*)'. Cannot insert duplicate key in object '.*'. The duplicate key value is \((.*)\)./);
match = match || err.message.match(/Cannot insert duplicate key row in object .* with unique index '(.*)'/);
if (match && match.length > 1) {
var fields = {}
......
......@@ -77,7 +77,7 @@ module.exports = (function() {
if (!Utils._.isString(indexName)) {
indexName = 'uniq_' + tableName + '_' + columns.fields.join('_');
}
values.attributes += ', UNIQUE ' + indexName + ' (' + Utils._.map(columns.fields, self.quoteIdentifier).join(', ') + ')';
values.attributes += ', UNIQUE ' + self.quoteIdentifier(indexName) + ' (' + Utils._.map(columns.fields, self.quoteIdentifier).join(', ') + ')';
}
});
}
......
......@@ -110,7 +110,7 @@ module.exports = (function() {
switch (err.errno || err.code) {
case 1062:
match = err.message.match(/Duplicate entry '(.*)' for key '?(.*?)'?$/);
match = err.message.match(/Duplicate entry '(.*)' for key '?((.|\s)*?)'?$/);
var values = match[1].split('-')
, fields = {}
......
......@@ -105,7 +105,7 @@ if (Support.dialectIsMySQL()) {
},
{
arguments: ['myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}, {uniqueKeys: [{fields: ['title', 'name']}]}],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255), UNIQUE uniq_myTable_title_name (`title`, `name`)) ENGINE=InnoDB;'
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255), UNIQUE `uniq_myTable_title_name` (`title`, `name`)) ENGINE=InnoDB;'
}
],
......
......@@ -164,5 +164,24 @@ describe(Support.getTestDialectTeaser('Sequelize Errors'), function () {
});
});
it('Supports newlines in keys', function () {
var spy = sinon.spy()
, User = this.sequelize.define('user', {
name: {
type: Sequelize.STRING,
unique: 'unique \n unique',
}
});
return this.sequelize.sync({ force: true }).bind(this).then(function () {
return User.create({ name: 'jan' });
}).then(function () {
// If the error was successfully parsed, we can catch it!
return User.create({ name: 'jan' }).catch(this.sequelize.UniqueConstraintError, spy);
}).then(function () {
expect(spy).to.have.been.calledOnce;
});
});
});
});
......@@ -313,11 +313,11 @@ describe(Support.getTestDialectTeaser('Model'), function() {
User.sync({ force: true }).on('sql', _.after(2, _.once(function(sql) {
if (dialect === 'mssql') {
expect(sql).to.match(/CONSTRAINT\s*(user_and_email)?\s*UNIQUE\s*\([`"]?username[`"]?, [`"]?email[`"]?\)/);
expect(sql).to.match(/CONSTRAINT\s*(a_and_b)?\s*UNIQUE\s*\([`"]?aCol[`"]?, [`"]?bCol[`"]?\)/);
expect(sql).to.match(/CONSTRAINT\s*([`"]?user_and_email[`"]?)?\s*UNIQUE\s*\([`"]?username[`"]?, [`"]?email[`"]?\)/);
expect(sql).to.match(/CONSTRAINT\s*([`"]?a_and_b[`"]?)?\s*UNIQUE\s*\([`"]?aCol[`"]?, [`"]?bCol[`"]?\)/);
} else {
expect(sql).to.match(/UNIQUE\s*(user_and_email)?\s*\([`"]?username[`"]?, [`"]?email[`"]?\)/);
expect(sql).to.match(/UNIQUE\s*(a_and_b)?\s*\([`"]?aCol[`"]?, [`"]?bCol[`"]?\)/);
expect(sql).to.match(/UNIQUE\s*([`"]?user_and_email[`"]?)?\s*\([`"]?username[`"]?, [`"]?email[`"]?\)/);
expect(sql).to.match(/UNIQUE\s*([`"]?a_and_b[`"]?)?\s*\([`"]?aCol[`"]?, [`"]?bCol[`"]?\)/);
}
done();
})));
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!