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

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 {
options.hooks = options.hooks === undefined ? true : !!options.hooks;
const attributes = this.tableAttributes;
const rawAttributes = this.fieldRawAttributesMap;
return Promise.try(() => {
if (options.hooks) {
......@@ -1247,17 +1248,17 @@ class Model {
_.each(attributes, (columnDesc, 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) => {
const currentAttributes = attributes[columnName];
if (!currentAttributes) {
const currentAttribute = rawAttributes[columnName];
if (!currentAttribute) {
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.
const references = currentAttributes.references;
if (currentAttributes.references) {
const references = currentAttribute.references;
if (currentAttribute.references) {
const database = this.sequelize.config.database;
const schema = this.sequelize.config.schema;
// Find existed foreign keys
......@@ -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());
......
......@@ -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() {
const User = this.sequelize.define('testSync', {
name: Sequelize.STRING,
age: Sequelize.INTEGER
age: Sequelize.INTEGER,
badgeNumber: { type: Sequelize.INTEGER, field: 'badge_number' }
});
return this.sequelize.sync()
.then(() => {
......@@ -30,6 +31,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
.then(() => User.describe())
.then(data => {
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');
});
});
......@@ -41,11 +44,33 @@ describe(Support.getTestDialectTeaser('Model'), () => {
return this.sequelize.sync()
.then(() => this.sequelize.define('testSync', {
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'));
.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(() => testSync.describe())
.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() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!