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

Commit 2c18e084 by Mirko Jotic Committed by Sushant

feat(sync): allow altering comments (#10758)

1 parent 2f6e01b5
......@@ -265,10 +265,18 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
changeColumnQuery(tableName, attributes) {
const attrString = [],
constraintString = [];
let commentString = '';
for (const attributeName in attributes) {
const quotedAttrName = this.quoteIdentifier(attributeName);
const definition = attributes[attributeName];
let definition = attributes[attributeName];
if (definition.includes('COMMENT ')) {
const commentMatch = definition.match(/^(.+) (COMMENT.*)$/);
const commentText = commentMatch[2].replace('COMMENT', '').trim();
commentString += this.commentTemplate(commentText, tableName, attributeName);
// remove comment related substring from dataType
definition = commentMatch[1];
}
if (definition.includes('REFERENCES')) {
constraintString.push(`FOREIGN KEY (${quotedAttrName}) ${definition.replace(/.+?(?=REFERENCES)/, '')}`);
} else {
......@@ -285,7 +293,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
finalQuery += `ADD ${constraintString.join(', ')}`;
}
return `ALTER TABLE ${this.quoteTable(tableName)} ${finalQuery};`;
return `ALTER TABLE ${this.quoteTable(tableName)} ${finalQuery};${commentString}`;
}
renameColumnQuery(tableName, attrBefore, attributes) {
......
......@@ -268,7 +268,6 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
changeColumnQuery(tableName, attributes) {
const query = subQuery => `ALTER TABLE ${this.quoteTable(tableName)} ALTER COLUMN ${subQuery};`;
const sql = [];
for (const attributeName in attributes) {
let definition = this.dataTypeMapping(tableName, attributeName, attributes[attributeName]);
let attrSql = '';
......@@ -536,11 +535,13 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
}
if (attribute.comment && typeof attribute.comment === 'string') {
if (options && options.context === 'addColumn') {
if (options && (options.context === 'addColumn' || options.context === 'changeColumn')) {
const quotedAttr = this.quoteIdentifier(options.key);
const escapedCommentText = this.escape(attribute.comment);
sql += `; COMMENT ON COLUMN ${this.quoteTable(options.table)}.${quotedAttr} IS ${escapedCommentText}`;
} else {
// for createTable event which does it's own parsing
// TODO: centralize creation of comment statements here
sql += ` COMMENT ${attribute.comment}`;
}
}
......@@ -575,7 +576,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
for (const key in attributes) {
const attribute = attributes[key];
result[attribute.field || key] = this.attributeToSQL(attribute, options);
result[attribute.field || key] = this.attributeToSQL(attribute, Object.assign({ key }, options || {}));
}
return result;
......
......@@ -230,7 +230,7 @@ class QueryInterface {
});
}
attributes = this.QueryGenerator.attributesToSQL(attributes, { context: 'createTable' });
attributes = this.QueryGenerator.attributesToSQL(attributes, { table: tableName, context: 'createTable' });
sql = this.QueryGenerator.createTableQuery(tableName, attributes, options);
return promise.then(() => this.sequelize.query(sql, options));
......@@ -567,7 +567,10 @@ class QueryInterface {
// sqlite needs some special treatment as it cannot change a column
return SQLiteQueryInterface.changeColumn(this, tableName, attributes, options);
}
const query = this.QueryGenerator.attributesToSQL(attributes);
const query = this.QueryGenerator.attributesToSQL(attributes, {
context: 'changeColumn',
table: tableName
});
const sql = this.QueryGenerator.changeColumnQuery(tableName, query);
return this.sequelize.query(sql, options);
......
......@@ -220,6 +220,21 @@ describe(Support.getTestDialectTeaser('QueryInterface'), () => {
});
});
it('should change the comment of column', function() {
return this.queryInterface.describeTable({
tableName: 'users'
}).then(describedTable => {
expect(describedTable.level_id.comment).to.be.equal(null);
return this.queryInterface.changeColumn('users', 'level_id', {
type: DataTypes.INTEGER,
comment: 'FooBar'
});
}).then(() => {
return this.queryInterface.describeTable({ tableName: 'users' });
}).then(describedTable2 => {
expect(describedTable2.level_id.comment).to.be.equal('FooBar');
});
});
});
}
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!