Skip to content

Commit

Permalink
fix(sqlite/querygenerator): quote attributes in alterConstraint and r…
Browse files Browse the repository at this point in the history
…enameColumn queries for sqlite (Closes sequelize#7759)
  • Loading branch information
petey authored and janmeier committed Jul 6, 2017
1 parent 5c4ce94 commit a7f18ba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/dialects/sqlite/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ const QueryGenerator = {

const quotedTableName = this.quoteTable(tableName);
const quotedBackupTableName = this.quoteTable(backupTableName);
const attributeNames = Object.keys(attributes).join(', ');
const attributeNames = Object.keys(attributes).map(attr => this.quoteIdentifier(attr)).join(', ');

return this.createTableQuery(backupTableName, attributes).replace('CREATE TABLE', 'CREATE TEMPORARY TABLE')
+ `INSERT INTO ${quotedBackupTableName} SELECT ${attributeNames} FROM ${quotedTableName};`
Expand All @@ -411,7 +411,7 @@ const QueryGenerator = {
}
const quotedTableName = this.quoteTable(tableName);
const quotedBackupTableName = this.quoteTable(backupTableName);
const attributeNames = Object.keys(attributes).join(', ');
const attributeNames = Object.keys(attributes).map(attr => this.quoteIdentifier(attr)).join(', ');

return createTableSql.replace(`CREATE TABLE ${quotedTableName}`, `CREATE TABLE ${quotedBackupTableName}`)
+ `INSERT INTO ${quotedBackupTableName} SELECT ${attributeNames} FROM ${quotedTableName};`
Expand Down
26 changes: 26 additions & 0 deletions test/unit/dialects/sqlite/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,32 @@ if (dialect === 'sqlite') {
expectation: "UPDATE `myTable` SET `bar`=`foo` WHERE `name` = 'foo'",
needsSequelize: true
}
],
renameColumnQuery: [
{
title: 'Properly quotes column names',
arguments: ['myTable', 'foo', 'commit', {commit: 'VARCHAR(255)', bar: 'VARCHAR(255)'}],
expectation:
'CREATE TEMPORARY TABLE IF NOT EXISTS `myTable_backup` (`commit` VARCHAR(255), `bar` VARCHAR(255));' +
'INSERT INTO `myTable_backup` SELECT `foo` AS `commit`, `bar` FROM `myTable`;' +
'DROP TABLE `myTable`;' +
'CREATE TABLE IF NOT EXISTS `myTable` (`commit` VARCHAR(255), `bar` VARCHAR(255));' +
'INSERT INTO `myTable` SELECT `commit`, `bar` FROM `myTable_backup`;' +
'DROP TABLE `myTable_backup`;'
}
],
removeColumnQuery: [
{
title: 'Properly quotes column names',
arguments: ['myTable', {commit: 'VARCHAR(255)', bar: 'VARCHAR(255)'}],
expectation:
'CREATE TEMPORARY TABLE IF NOT EXISTS `myTable_backup` (`commit` VARCHAR(255), `bar` VARCHAR(255));' +
'INSERT INTO `myTable_backup` SELECT `commit`, `bar` FROM `myTable`;' +
'DROP TABLE `myTable`;' +
'CREATE TABLE IF NOT EXISTS `myTable` (`commit` VARCHAR(255), `bar` VARCHAR(255));' +
'INSERT INTO `myTable` SELECT `commit`, `bar` FROM `myTable_backup`;' +
'DROP TABLE `myTable_backup`;'
}
]
};

Expand Down

0 comments on commit a7f18ba

Please sign in to comment.