Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(query-generator): fix addColumn create comment #10117

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 20 additions & 8 deletions lib/dialects/mssql/query-generator.js
Expand Up @@ -110,12 +110,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
const query = (table, attrs) => `IF OBJECT_ID('${table}', 'U') IS NULL CREATE TABLE ${table} (${attrs})`,
primaryKeys = [],
foreignKeys = {},
attrStr = [],
commentTemplate = (comment, table, column) => ' EXEC sp_addextendedproperty ' +
`@name = N\'MS_Description\', @value = ${comment}, ` +
'@level0type = N\'Schema\', @level0name = \'dbo\', ' +
`@level1type = N\'Table\', @level1name = ${table}, ` +
`@level2type = N\'Column\', @level2name = ${column};`;
attrStr = [];

let commentStr = '';

Expand All @@ -127,7 +122,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
if (dataType.includes('COMMENT ')) {
const commentMatch = dataType.match(/^(.+) (COMMENT.*)$/);
const commentText = commentMatch[2].replace('COMMENT', '').trim();
commentStr += commentTemplate(this.escape(commentText), this.quoteIdentifier(tableName), this.quoteIdentifier(attr));
commentStr += this.commentTemplate(commentText, tableName, attr);
// remove comment related substring from dataType
dataType = commentMatch[1];
}
Expand Down Expand Up @@ -239,11 +234,28 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
// FIXME: attributeToSQL SHOULD be using attributes in addColumnQuery
// but instead we need to pass the key along as the field here
dataType.field = key;
let commentStr = '';

if (dataType.comment && _.isString(dataType.comment)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post merge comment: Should use typeof

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimonSchick Agreed. I was trying to be consistent with the rest of the codebase and lodash is already imported and used.

commentStr = this.commentTemplate(dataType.comment, table, key);
// attributeToSQL will try to include `COMMENT 'Comment Text'` when it returns if the comment key
// is present. This is needed for createTable statement where that part is extracted with regex.
// Here we can intercept the object and remove comment property since we have the original object.
delete dataType['comment'];
}

const def = this.attributeToSQL(dataType, {
context: 'addColumn'
});
return `ALTER TABLE ${this.quoteTable(table)} ADD ${this.quoteIdentifier(key)} ${def};`;
return `ALTER TABLE ${this.quoteTable(table)} ADD ${this.quoteIdentifier(key)} ${def};${commentStr}`;
}

commentTemplate(comment, table, column) {
return ' EXEC sp_addextendedproperty ' +
`@name = N\'MS_Description\', @value = ${this.escape(comment)}, ` +
'@level0type = N\'Schema\', @level0name = \'dbo\', ' +
`@level1type = N\'Table\', @level1name = ${this.quoteIdentifier(table)}, ` +
`@level2type = N\'Column\', @level2name = ${this.quoteIdentifier(column)};`;
}

removeColumnQuery(tableName, attributeName) {
Expand Down
12 changes: 9 additions & 3 deletions lib/dialects/postgres/query-generator.js
Expand Up @@ -244,7 +244,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {

addColumnQuery(table, key, dataType) {

const dbDataType = this.attributeToSQL(dataType, { context: 'addColumn' });
const dbDataType = this.attributeToSQL(dataType, { context: 'addColumn', table, key });
const definition = this.dataTypeMapping(table, key, dbDataType);
const quotedKey = this.quoteIdentifier(key);
const quotedTable = this.quoteTable(this.extractTableDetails(table));
Expand Down Expand Up @@ -452,7 +452,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
return fragment;
}

attributeToSQL(attribute) {
attributeToSQL(attribute, option) {
mirkojotic marked this conversation as resolved.
Show resolved Hide resolved
if (!_.isPlainObject(attribute)) {
attribute = {
type: attribute
Expand Down Expand Up @@ -535,7 +535,13 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
}

if (attribute.comment && _.isString(attribute.comment)) {
sql += ` COMMENT ${attribute.comment}`;
if (option && option.context === 'addColumn') {
const quotedAttr = this.quoteIdentifier(option.key);
const escapedCommentText = this.escape(attribute.comment);
sql += `; COMMENT ON COLUMN ${this.quoteTable(option.table)}.${quotedAttr} IS ${escapedCommentText}`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this feature working?
I need to put some comments into a column to let know others devs of a team a small description of what's the purpose of the column.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@normancarcamo It was merged into master but you'll have to see which version of sequelize it will land in.

But also this is just the the addition to a PR merged a couple of months back that allowed this to be done on table creation. This PR is just so you could leverage this feature on addColumn API of query generator.

} else {
sql += ` COMMENT ${attribute.comment}`;
}
}

return sql;
Expand Down
16 changes: 16 additions & 0 deletions test/unit/dialects/mssql/query-generator.test.js
Expand Up @@ -193,6 +193,22 @@ if (current.dialect.name === 'mssql') {
});
});

it('addColumnQuery', function() {
expectsql(this.queryGenerator.addColumnQuery('myTable', 'myColumn', { type: 'VARCHAR(255)' }), {
mssql: 'ALTER TABLE [myTable] ADD [myColumn] VARCHAR(255) NULL;'
});
});

it('addColumnQuery with comment', function() {
expectsql(this.queryGenerator.addColumnQuery('myTable', 'myColumn', { type: 'VARCHAR(255)', comment: 'This is a comment' }), {
mssql: 'ALTER TABLE [myTable] ADD [myColumn] VARCHAR(255) NULL; EXEC sp_addextendedproperty ' +
'@name = N\'MS_Description\', @value = N\'This is a comment\', ' +
'@level0type = N\'Schema\', @level0name = \'dbo\', ' +
'@level1type = N\'Table\', @level1name = [myTable], ' +
'@level2type = N\'Column\', @level2name = [myColumn];'
});
});

it('removeColumnQuery', function() {
expectsql(this.queryGenerator.removeColumnQuery('myTable', 'myColumn'), {
mssql: 'ALTER TABLE [myTable] DROP COLUMN [myColumn];'
Expand Down
5 changes: 4 additions & 1 deletion test/unit/dialects/postgres/query-generator.test.js
Expand Up @@ -124,7 +124,10 @@ if (dialect.startsWith('postgres')) {
arguments: [{id: {type: 'INTEGER', unique: true, comment: 'This is my comment'}}],
expectation: {id: 'INTEGER UNIQUE COMMENT This is my comment'}
},

{
arguments: [{id: {type: 'INTEGER', unique: true, comment: 'This is my comment'}}, {context: 'addColumn', key: 'column', table: {schema: 'foo', tableName: 'bar'}}],
expectation: {id: 'INTEGER UNIQUE; COMMENT ON COLUMN "foo"."bar"."column" IS \'This is my comment\''}
},
// New references style
{
arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }}}],
Expand Down
9 changes: 9 additions & 0 deletions test/unit/sql/add-column.test.js
Expand Up @@ -50,6 +50,15 @@ if (current.dialect.name === 'mysql') {
mysql: 'ALTER TABLE `users` ADD `test_added_col_first` VARCHAR(255) FIRST;'
});
});

it('properly generates alter queries with column level comment', () => {
return expectsql(sql.addColumnQuery(Model.getTableName(), 'column_with_comment', current.normalizeAttribute({
type: DataTypes.STRING,
comment: 'This is a comment'
})), {
mysql: 'ALTER TABLE `users` ADD `column_with_comment` VARCHAR(255) COMMENT \'This is a comment\';'
});
});
});
});
}