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

Commit d46e2070 by Matt Committed by Sushant

fix: sync with alter:true doesn't use field name (#9529)

1 parent 4c7b103d
...@@ -1220,6 +1220,7 @@ class Model { ...@@ -1220,6 +1220,7 @@ class Model {
options.hooks = options.hooks === undefined ? true : !!options.hooks; options.hooks = options.hooks === undefined ? true : !!options.hooks;
const attributes = this.tableAttributes; const attributes = this.tableAttributes;
const rawAttributes = this.fieldRawAttributesMap;
return Promise.try(() => { return Promise.try(() => {
if (options.hooks) { if (options.hooks) {
...@@ -1247,17 +1248,17 @@ class Model { ...@@ -1247,17 +1248,17 @@ class Model {
_.each(attributes, (columnDesc, columnName) => { _.each(attributes, (columnDesc, columnName) => {
if (!columns[columnName]) { if (!columns[columnName]) {
changes.push(() => this.QueryInterface.addColumn(this.getTableName(options), columnName, attributes[columnName])); changes.push(() => this.QueryInterface.addColumn(this.getTableName(options), attributes[columnName].field || columnName, attributes[columnName]));
} }
}); });
_.each(columns, (columnDesc, columnName) => { _.each(columns, (columnDesc, columnName) => {
const currentAttributes = attributes[columnName]; const currentAttribute = rawAttributes[columnName];
if (!currentAttributes) { if (!currentAttribute) {
changes.push(() => this.QueryInterface.removeColumn(this.getTableName(options), columnName, options)); changes.push(() => this.QueryInterface.removeColumn(this.getTableName(options), columnName, options));
} else if (!currentAttributes.primaryKey) { } else if (!currentAttribute.primaryKey) {
// Check foreign keys. If it's a foreign key, it should remove constraint first. // Check foreign keys. If it's a foreign key, it should remove constraint first.
const references = currentAttributes.references; const references = currentAttribute.references;
if (currentAttributes.references) { if (currentAttribute.references) {
const database = this.sequelize.config.database; const database = this.sequelize.config.database;
const schema = this.sequelize.config.schema; const schema = this.sequelize.config.schema;
// Find existed foreign keys // Find existed foreign keys
...@@ -1276,7 +1277,7 @@ class Model { ...@@ -1276,7 +1277,7 @@ class Model {
} }
}); });
} }
changes.push(() => this.QueryInterface.changeColumn(this.getTableName(options), columnName, attributes[columnName])); changes.push(() => this.QueryInterface.changeColumn(this.getTableName(options), columnName, currentAttribute));
} }
}); });
return changes.reduce((p, fn) => p.then(fn), Promise.resolve()); return changes.reduce((p, fn) => p.then(fn), Promise.resolve());
......
...@@ -18,7 +18,8 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -18,7 +18,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
it('should remove a column if it exists in the databases schema but not the model', function() { it('should remove a column if it exists in the databases schema but not the model', function() {
const User = this.sequelize.define('testSync', { const User = this.sequelize.define('testSync', {
name: Sequelize.STRING, name: Sequelize.STRING,
age: Sequelize.INTEGER age: Sequelize.INTEGER,
badgeNumber: { type: Sequelize.INTEGER, field: 'badge_number' }
}); });
return this.sequelize.sync() return this.sequelize.sync()
.then(() => { .then(() => {
...@@ -30,6 +31,8 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -30,6 +31,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
.then(() => User.describe()) .then(() => User.describe())
.then(data => { .then(data => {
expect(data).to.not.have.ownProperty('age'); expect(data).to.not.have.ownProperty('age');
expect(data).to.not.have.ownProperty('badge_number');
expect(data).to.not.have.ownProperty('badgeNumber');
expect(data).to.have.ownProperty('name'); expect(data).to.have.ownProperty('name');
}); });
}); });
...@@ -41,11 +44,33 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -41,11 +44,33 @@ describe(Support.getTestDialectTeaser('Model'), () => {
return this.sequelize.sync() return this.sequelize.sync()
.then(() => this.sequelize.define('testSync', { .then(() => this.sequelize.define('testSync', {
name: Sequelize.STRING, name: Sequelize.STRING,
age: Sequelize.INTEGER age: Sequelize.INTEGER,
height: { type: Sequelize.INTEGER, field: 'height_cm' }
}))
.then(() => this.sequelize.sync({alter: true}))
.then(() => testSync.describe())
.then(data => {
expect(data).to.have.ownProperty('age');
expect(data).to.have.ownProperty('height_cm');
expect(data).not.to.have.ownProperty('height');
});
});
it('should alter a column using the correct column name (#9515)', function() {
const testSync = this.sequelize.define('testSync', {
name: Sequelize.STRING
});
return this.sequelize.sync()
.then(() => this.sequelize.define('testSync', {
name: Sequelize.STRING,
badgeNumber: { type: Sequelize.INTEGER, field: 'badge_number' }
})) }))
.then(() => this.sequelize.sync({alter: true})) .then(() => this.sequelize.sync({alter: true}))
.then(() => testSync.describe()) .then(() => testSync.describe())
.then(data => expect(data).to.have.ownProperty('age')); .then(data => {
expect(data).to.have.ownProperty('badge_number');
expect(data).not.to.have.ownProperty('badgeNumber');
});
}); });
it('should change a column if it exists in the model but is different in the database', function() { it('should change a column if it exists in the model but is different in the database', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!