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

Commit 7bf1b71b by Ariel Barabas Committed by Sushant

fix(sqlite): don't break when adding second constraint to a table (#11067)

1 parent ab72dfd5
...@@ -425,7 +425,9 @@ class SQLiteQueryGenerator extends MySqlQueryGenerator { ...@@ -425,7 +425,9 @@ class SQLiteQueryGenerator extends MySqlQueryGenerator {
const quotedBackupTableName = this.quoteTable(backupTableName); const quotedBackupTableName = this.quoteTable(backupTableName);
const attributeNames = Object.keys(attributes).map(attr => this.quoteIdentifier(attr)).join(', '); const attributeNames = Object.keys(attributes).map(attr => this.quoteIdentifier(attr)).join(', ');
return `${createTableSql.replace(`CREATE TABLE ${quotedTableName}`, `CREATE TABLE ${quotedBackupTableName}`) return `${createTableSql
.replace(`CREATE TABLE ${quotedTableName}`, `CREATE TABLE ${quotedBackupTableName}`)
.replace(`CREATE TABLE ${quotedTableName.replace(/`/g, '"')}`, `CREATE TABLE ${quotedBackupTableName}`)
}INSERT INTO ${quotedBackupTableName} SELECT ${attributeNames} FROM ${quotedTableName};` }INSERT INTO ${quotedBackupTableName} SELECT ${attributeNames} FROM ${quotedTableName};`
+ `DROP TABLE ${quotedTableName};` + `DROP TABLE ${quotedTableName};`
+ `ALTER TABLE ${quotedBackupTableName} RENAME TO ${quotedTableName};`; + `ALTER TABLE ${quotedBackupTableName} RENAME TO ${quotedTableName};`;
......
...@@ -113,7 +113,8 @@ function removeConstraint(qi, tableName, constraintName, options) { ...@@ -113,7 +113,8 @@ function removeConstraint(qi, tableName, constraintName, options) {
return qi.showConstraint(tableName, constraintName) return qi.showConstraint(tableName, constraintName)
.then(constraints => { .then(constraints => {
const constraint = constraints[0]; // sqlite can't show only one constraint, so we find here the one to remove
const constraint = constraints.find(constaint => constaint.constraintName === constraintName);
if (constraint) { if (constraint) {
createTableSql = constraint.sql; createTableSql = constraint.sql;
......
...@@ -490,6 +490,34 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => { ...@@ -490,6 +490,34 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
expect(constraints).to.not.include('users_email_uk'); expect(constraints).to.not.include('users_email_uk');
}); });
}); });
it('should add a constraint after another', function() {
return this.queryInterface.addConstraint('users', ['username'], {
type: 'unique'
}).then(() => this.queryInterface.addConstraint('users', ['email'], {
type: 'unique'
}))
.then(() => this.queryInterface.showConstraint('users'))
.then(constraints => {
constraints = constraints.map(constraint => constraint.constraintName);
expect(constraints).to.include('users_email_uk');
expect(constraints).to.include('users_username_uk');
return this.queryInterface.removeConstraint('users', 'users_email_uk');
})
.then(() => this.queryInterface.showConstraint('users'))
.then(constraints => {
constraints = constraints.map(constraint => constraint.constraintName);
expect(constraints).to.not.include('users_email_uk');
expect(constraints).to.include('users_username_uk');
return this.queryInterface.removeConstraint('users', 'users_username_uk');
})
.then(() => this.queryInterface.showConstraint('users'))
.then(constraints => {
constraints = constraints.map(constraint => constraint.constraintName);
expect(constraints).to.not.include('users_email_uk');
expect(constraints).to.not.include('users_username_uk');
});
});
}); });
if (current.dialect.supports.constraints.check) { if (current.dialect.supports.constraints.check) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!