Skip to content

Commit

Permalink
fix(postgres): escape identifier in createSchema and dropSchema (#15752)
Browse files Browse the repository at this point in the history
Fixes #15651. This patch backports a part of #14999 to Sequelize v6 to
quote schema identifiers in PostgreSQL `createSchema()` and
`dropSchema()`.
  • Loading branch information
meyfa committed Mar 9, 2023
1 parent 1b94462 commit 1ad9a64
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
const databaseVersion = _.get(this, 'sequelize.options.databaseVersion', 0);

if (databaseVersion && semver.gte(databaseVersion, '9.2.0')) {
return `CREATE SCHEMA IF NOT EXISTS ${schema};`;
return `CREATE SCHEMA IF NOT EXISTS ${this.quoteIdentifier(schema)};`;
}

return `CREATE SCHEMA ${schema};`;
return `CREATE SCHEMA ${this.quoteIdentifier(schema)};`;
}

dropSchema(schema) {
return `DROP SCHEMA IF EXISTS ${schema} CASCADE;`;
return `DROP SCHEMA IF EXISTS ${this.quoteIdentifier(schema)} CASCADE;`;
}

showSchemasQuery() {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/sql/create-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
describe('dropSchema', () => {
it('IF EXISTS', () => {
expectsql(sql.dropSchema('foo'), {
postgres: 'DROP SCHEMA IF EXISTS foo CASCADE;'
postgres: 'DROP SCHEMA IF EXISTS "foo" CASCADE;'
});
});
});
Expand All @@ -27,14 +27,14 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
it('9.2.0 or above', () => {
current.options.databaseVersion = '9.2.0';
expectsql(sql.createSchema('foo'), {
postgres: 'CREATE SCHEMA IF NOT EXISTS foo;'
postgres: 'CREATE SCHEMA IF NOT EXISTS "foo";'
});
});

it('below 9.2.0', () => {
current.options.databaseVersion = '9.0.0';
expectsql(sql.createSchema('foo'), {
postgres: 'CREATE SCHEMA foo;'
postgres: 'CREATE SCHEMA "foo";'
});
});
});
Expand Down

0 comments on commit 1ad9a64

Please sign in to comment.