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

Commit 59b218d0 by Mick Hansen

fix(queryInterface): schema support for changeColumn

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