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

Commit 59b218d0 by Mick Hansen

fix(queryInterface): schema support for changeColumn

1 parent 8086e72b
...@@ -132,7 +132,7 @@ module.exports = (function() { ...@@ -132,7 +132,7 @@ module.exports = (function() {
}, },
changeColumnQuery: function(tableName, attributes) { changeColumnQuery: function(tableName, attributes) {
var query = 'ALTER TABLE `<%= tableName %>` CHANGE <%= attributes %>;'; var query = 'ALTER TABLE <%= tableName %> CHANGE <%= attributes %>;';
var attrString = []; var attrString = [];
for (var attrName in attributes) { for (var attrName in attributes) {
...@@ -144,7 +144,7 @@ module.exports = (function() { ...@@ -144,7 +144,7 @@ module.exports = (function() {
})); }));
} }
return Utils._.template(query)({ tableName: tableName, attributes: attrString.join(', ') }); return Utils._.template(query)({ tableName: this.quoteTable(tableName), attributes: attrString.join(', ') });
}, },
renameColumnQuery: function(tableName, attrBefore, attributes) { renameColumnQuery: function(tableName, attrBefore, attributes) {
......
...@@ -250,30 +250,30 @@ module.exports = (function() { ...@@ -250,30 +250,30 @@ module.exports = (function() {
var definition = this.pgDataTypeMapping(tableName, attributeName, attributes[attributeName]); var definition = this.pgDataTypeMapping(tableName, attributeName, attributes[attributeName]);
var attrSql = ''; var attrSql = '';
if (definition.indexOf('NOT NULL') > 0) { /*if (definition.indexOf('NOT NULL') > 0) {
attrSql += Utils._.template(query)({ attrSql += Utils._.template(query)({
tableName: this.quoteIdentifiers(tableName), tableName: this.quoteTable(tableName),
query: this.quoteIdentifier(attributeName) + ' SET NOT NULL' query: this.quoteIdentifier(attributeName) + ' SET NOT NULL'
}); });
definition = definition.replace('NOT NULL', '').trim(); definition = definition.replace('NOT NULL', '').trim();
} else { } else {
attrSql += Utils._.template(query)({ attrSql += Utils._.template(query)({
tableName: this.quoteIdentifiers(tableName), tableName: this.quoteTable(tableName),
query: this.quoteIdentifier(attributeName) + ' DROP NOT NULL' query: this.quoteIdentifier(attributeName) + ' DROP NOT NULL'
}); });
} }
if (definition.indexOf('DEFAULT') > 0) { if (definition.indexOf('DEFAULT') > 0) {
attrSql += Utils._.template(query)({ attrSql += Utils._.template(query)({
tableName: this.quoteIdentifiers(tableName), tableName: this.quoteTable(tableName),
query: this.quoteIdentifier(attributeName) + ' SET DEFAULT ' + definition.match(/DEFAULT ([^;]+)/)[1] query: this.quoteIdentifier(attributeName) + ' SET DEFAULT ' + definition.match(/DEFAULT ([^;]+)/)[1]
}); });
definition = definition.replace(/(DEFAULT[^;]+)/, '').trim(); definition = definition.replace(/(DEFAULT[^;]+)/, '').trim();
} else { } else {
attrSql += Utils._.template(query)({ attrSql += Utils._.template(query)({
tableName: this.quoteIdentifiers(tableName), tableName: this.quoteTable(tableName),
query: this.quoteIdentifier(attributeName) + ' DROP DEFAULT' query: this.quoteIdentifier(attributeName) + ' DROP DEFAULT'
}); });
} }
...@@ -288,13 +288,13 @@ module.exports = (function() { ...@@ -288,13 +288,13 @@ module.exports = (function() {
definition = definition.replace(/UNIQUE;*$/, ''); definition = definition.replace(/UNIQUE;*$/, '');
attrSql += Utils._.template(query.replace('ALTER COLUMN', ''))({ attrSql += Utils._.template(query.replace('ALTER COLUMN', ''))({
tableName: this.quoteIdentifiers(tableName), tableName: this.quoteTable(tableName),
query: 'ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_unique_idx') + ' UNIQUE (' + this.quoteIdentifier(attributeName) + ')' query: 'ADD CONSTRAINT ' + this.quoteIdentifier(attributeName + '_unique_idx') + ' UNIQUE (' + this.quoteIdentifier(attributeName) + ')'
}); });
} }*/
attrSql += Utils._.template(query)({ attrSql += Utils._.template(query)({
tableName: this.quoteIdentifiers(tableName), tableName: this.quoteTable(tableName),
query: this.quoteIdentifier(attributeName) + ' TYPE ' + definition query: this.quoteIdentifier(attributeName) + ' TYPE ' + definition
}); });
......
...@@ -369,20 +369,32 @@ module.exports = (function() { ...@@ -369,20 +369,32 @@ module.exports = (function() {
}, },
removeColumnQuery: function(tableName, attributes) { removeColumnQuery: function(tableName, attributes) {
var backupTableName
, query;
attributes = this.attributesToSQL(attributes); attributes = this.attributesToSQL(attributes);
var backupTableName = tableName + "_backup"; if (typeof tableName === 'object') {
var query = [ backupTableName = {
tableName: tableName.tableName + "_backup",
schema: tableName.schema
};
} else {
backupTableName = tableName + "_backup";
}
query = [
this.createTableQuery(backupTableName, attributes).replace('CREATE TABLE', 'CREATE TEMPORARY TABLE'), this.createTableQuery(backupTableName, attributes).replace('CREATE TABLE', 'CREATE TEMPORARY TABLE'),
"INSERT INTO <%= tableName %>_backup SELECT <%= attributeNames %> FROM <%= tableName %>;", "INSERT INTO <%= backupTableName %> SELECT <%= attributeNames %> FROM <%= tableName %>;",
"DROP TABLE <%= tableName %>;", "DROP TABLE <%= tableName %>;",
this.createTableQuery(tableName, attributes), this.createTableQuery(tableName, attributes),
"INSERT INTO <%= tableName %> SELECT <%= attributeNames %> FROM <%= tableName %>_backup;", "INSERT INTO <%= tableName %> SELECT <%= attributeNames %> FROM <%= backupTableName %>;",
"DROP TABLE <%= tableName %>_backup;" "DROP TABLE <%= backupTableName %>;"
].join(""); ].join("");
return Utils._.template(query, { return Utils._.template(query, {
tableName: tableName, tableName: this.quoteTable(tableName),
backupTableName: this.quoteTable(backupTableName),
attributeNames: Utils._.keys(attributes).join(', ') attributeNames: Utils._.keys(attributes).join(', ')
}); });
}, },
......
...@@ -9,9 +9,9 @@ chai.config.includeStack = true ...@@ -9,9 +9,9 @@ chai.config.includeStack = true
describe(Support.getTestDialectTeaser("QueryInterface"), function () { describe(Support.getTestDialectTeaser("QueryInterface"), function () {
beforeEach(function(done) { beforeEach(function(done) {
this.sequelize.options.quoteIdenifiers = true this.sequelize.options.quoteIdenifiers = true;
this.queryInterface = this.sequelize.getQueryInterface() this.queryInterface = this.sequelize.getQueryInterface();
done() done();
}) })
describe('dropAllTables', function() { describe('dropAllTables', function() {
...@@ -244,7 +244,7 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () { ...@@ -244,7 +244,7 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
return self.queryInterface.renameColumn('_Users', 'active', 'enabled') return self.queryInterface.renameColumn('_Users', 'active', 'enabled')
}) })
}) })
it('renames a column primary key autoincrement column', function() { it('renames a column primary key autoIncrement column', function() {
var self = this var self = this
var Fruits = self.sequelize.define('Fruit', { var Fruits = self.sequelize.define('Fruit', {
fruitId: { fruitId: {
...@@ -261,13 +261,40 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () { ...@@ -261,13 +261,40 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
}) })
}); });
describe('changeColumn', function () {
it('should support schemas', function () {
return this.sequelize.dropAllSchemas().bind(this).then(function () {
return this.sequelize.createSchema("archive");
}).then(function () {
return this.queryInterface.createTable({
tableName: 'users',
schema: 'archive'
}, {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
currency: DataTypes.INTEGER
}).bind(this).then(function () {
return this.queryInterface.changeColumn({
tableName: 'users',
schema: 'archive'
}, 'currency', {
type: DataTypes.FLOAT
});
});
});
});
});
describe('addColumn', function () { describe('addColumn', function () {
beforeEach(function () { beforeEach(function () {
return this.queryInterface.createTable('users', { return this.queryInterface.createTable('users', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
autoincrement: true autoIncrement: true
} }
}); });
}); });
...@@ -277,7 +304,7 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () { ...@@ -277,7 +304,7 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
autoincrement: true autoIncrement: true
} }
}).bind(this).then(function () { }).bind(this).then(function () {
return this.queryInterface.addColumn('users', 'level_id', { return this.queryInterface.addColumn('users', 'level_id', {
...@@ -290,6 +317,26 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () { ...@@ -290,6 +317,26 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
}); });
}); });
it('should work with schemas', function () {
return this.queryInterface.createTable({
tableName: 'users',
schema: 'archive'
}, {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
}
}).bind(this).then(function () {
return this.queryInterface.addColumn({
tableName: 'users',
schema: 'archive'
}, 'level_id', {
type: DataTypes.INTEGER
});
});
});
it('should work with enums (1)', function () { it('should work with enums (1)', function () {
return this.queryInterface.addColumn('users', 'someEnum', DataTypes.ENUM('value1', 'value2', 'value3')); return this.queryInterface.addColumn('users', 'someEnum', DataTypes.ENUM('value1', 'value2', 'value3'));
}); });
...@@ -308,14 +355,14 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () { ...@@ -308,14 +355,14 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
autoincrement: true autoIncrement: true
}, },
}).bind(this).then(function () { }).bind(this).then(function () {
return this.queryInterface.createTable('hosts', { return this.queryInterface.createTable('hosts', {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
primaryKey: true, primaryKey: true,
autoincrement: true autoIncrement: true
}, },
admin: { admin: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!