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

Commit 8086e72b by Mick Hansen

fix(queryInterface): migration support for renameColumn

1 parent e913ba3f
......@@ -555,7 +555,7 @@ module.exports = (function() {
table += this.quoteIdentifier(param.tableName);
} else {
if (param.schema) {
table += param.schema + param.delimiter;
table += param.schema + (param.delimiter || '.');
}
table += param.tableName;
......
......@@ -148,7 +148,7 @@ module.exports = (function() {
},
renameColumnQuery: function(tableName, attrBefore, attributes) {
var query = 'ALTER TABLE `<%= tableName %>` CHANGE <%= attributes %>;';
var query = 'ALTER TABLE <%= tableName %> CHANGE <%= attributes %>;';
var attrString = [];
for (var attrName in attributes) {
......@@ -161,7 +161,7 @@ module.exports = (function() {
}));
}
return Utils._.template(query)({ tableName: tableName, attributes: attrString.join(', ') });
return Utils._.template(query)({ tableName: this.quoteTable(tableName), attributes: attrString.join(', ') });
},
upsertQuery: function (tableName, insertValues, updateValues, where, rawAttributes, options) {
......
......@@ -316,7 +316,7 @@ module.exports = (function() {
}
return Utils._.template(query)({
tableName: this.quoteIdentifiers(tableName),
tableName: this.quoteTable(tableName),
attributes: attrString.join(', ')
});
},
......
......@@ -388,20 +388,32 @@ module.exports = (function() {
},
renameColumnQuery: function(tableName, attrNameBefore, attrNameAfter, 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 <%= attributeNamesImport %> FROM <%= tableName %>;",
"INSERT INTO <%= backupTableName %> SELECT <%= attributeNamesImport %> FROM <%= tableName %>;",
"DROP TABLE <%= tableName %>;",
this.createTableQuery(tableName, attributes),
"INSERT INTO <%= tableName %> SELECT <%= attributeNamesExport %> FROM <%= tableName %>_backup;",
"DROP TABLE <%= tableName %>_backup;"
"INSERT INTO <%= tableName %> SELECT <%= attributeNamesExport %> FROM <%= backupTableName %>;",
"DROP TABLE <%= backupTableName %>;"
].join("");
return Utils._.template(query, {
tableName: tableName,
tableName: this.quoteTable(tableName),
backupTableName: this.quoteTable(backupTableName),
attributeNamesImport: Utils._.keys(attributes).map(function(attr) {
return (attrNameAfter === attr) ? this.quoteIdentifier(attrNameBefore) + ' AS ' + this.quoteIdentifier(attr) : this.quoteIdentifier(attr);
}.bind(this)).join(', '),
......
......@@ -300,6 +300,11 @@ module.exports = (function() {
schemaDelimiter = options.schemaDelimiter || null;
}
if (typeof tableName === 'object') {
schema = tableName.schema;
tableName = tableName.tableName;
}
var sql = this.QueryGenerator.describeTableQuery(tableName, schema, schemaDelimiter);
return this.sequelize.query(sql, null, { raw: true }).then(function(data) {
......
......@@ -195,6 +195,28 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
return self.queryInterface.renameColumn('_Users', 'username', 'pseudo')
})
})
it('works with schemas', function () {
var self = this
var Users = self.sequelize.define('User', {
username: DataTypes.STRING
}, {
tableName: 'Users',
schema: 'archive'
})
return self.sequelize.dropAllSchemas().then(function () {
return self.sequelize.createSchema("archive");
}).then(function () {
return Users.sync({ force: true }).then(function() {
return self.queryInterface.renameColumn({
schema: 'archive',
tableName: 'Users'
}, 'username', 'pseudo');
});
});
});
it('rename a column non-null without default value', function() {
var self = this
var Users = self.sequelize.define('_Users', {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!