Fix comment escaping for MySQL and PostgreSQL (#4084)

This commit is contained in:
Igor Savin 2020-10-27 10:05:21 +02:00 committed by GitHub
parent 4edd56f156
commit 20629af916
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 3 deletions

View File

@ -4,6 +4,8 @@ const { inherits } = require('util');
const ColumnCompiler = require('../../../schema/columncompiler');
const { isObject } = require('../../../util/is');
const commentEscapeRegex = /(?<!\\)'/g;
function ColumnCompiler_MySQL() {
ColumnCompiler.apply(this, arguments);
this.modifiers = [
@ -150,7 +152,7 @@ Object.assign(ColumnCompiler_MySQL.prototype, {
'Your comment is longer than the max comment length for MySQL'
);
}
return comment && `comment '${comment}'`;
return comment && `comment '${comment.replace(commentEscapeRegex, "\\'")}'`;
},
first() {

View File

@ -4,6 +4,7 @@
const { inherits } = require('util');
const ColumnCompiler = require('../../../schema/columncompiler');
const { isObject } = require('../../../util/is');
const commentEscapeRegex = /(?<!')'(?!')/g;
function ColumnCompiler_PG() {
ColumnCompiler.apply(this, arguments);
@ -101,13 +102,15 @@ Object.assign(ColumnCompiler_PG.prototype, {
// ------
comment(comment) {
const columnName = this.args[0] || this.defaults('columnName');
const escapedComment = comment
? `'${comment.replace(commentEscapeRegex, "''")}'`
: 'NULL';
this.pushAdditional(function () {
this.pushQuery(
`comment on column ${this.tableCompiler.tableName()}.` +
this.formatter.wrap(columnName) +
' is ' +
(comment ? `'${comment}'` : 'NULL')
` is ${escapedComment}`
);
}, comment);
},

View File

@ -890,6 +890,38 @@ module.exports = function (dialect) {
expect(tableSql[0].sql).to.equal("alter table `users` comment = ''");
});
it('test column comment with quotes', function () {
tableSql = client
.schemaBuilder()
.createTable('test', (t) => {
t.text('column1').comment(
"The table's first column and it's escaped"
);
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
"create table `test` (`column1` text comment 'The table\\'s first column and it\\'s escaped')"
);
});
it('test column comment with pre-escaped quotes', function () {
tableSql = client
.schemaBuilder()
.createTable('test', (t) => {
t.text('column1').comment(
"The table\\'s first column and it\\'s escaped"
);
})
.toSQL();
equal(1, tableSql.length);
expect(tableSql[0].sql).to.equal(
"create table `test` (`column1` text comment 'The table\\'s first column and it\\'s escaped')"
);
});
it('set comment to undefined', function () {
expect(function () {
client

View File

@ -1190,6 +1190,36 @@ describe('PostgreSQL SchemaBuilder', function () {
expect(tableSql[0].sql).to.equal('comment on table "user" is \'\'');
});
it('test column comment with quotes', function () {
tableSql = client
.schemaBuilder()
.createTable('test', (t) => {
t.text('column1').comment("The table's first column and it's escaped");
})
.toSQL();
equal(tableSql.length, 2);
expect(tableSql[1].sql).to.equal(
"comment on column \"test\".\"column1\" is 'The table''s first column and it''s escaped'"
);
});
it('test column comment with pre-escaped quotes', function () {
tableSql = client
.schemaBuilder()
.createTable('test', (t) => {
t.text('column1').comment(
"The table''s first column and it''s escaped"
);
})
.toSQL();
equal(tableSql.length, 2);
expect(tableSql[1].sql).to.equal(
"comment on column \"test\".\"column1\" is 'The table''s first column and it''s escaped'"
);
});
it('set comment to undefined', function () {
expect(function () {
client