Skip to content

Commit

Permalink
feat(postgres): drop indices concurrently in Postgres (#13903)
Browse files Browse the repository at this point in the history
[feature] Drop indices concurrently in Postgres
This is a follow up to the feature request.
On `removeIndex` it now extends the `options` object
to take in a new attribute, `concurrently`, which
will ensure that the the SQL statement being run
includes `DROP INDEX CONCURRENTLY`...

Having this is nice so developers can use the ORM settings
when dropping an index, safely, instead of having to write
raw SQL.

`concurrently` is a supported attribute on addIndex
  • Loading branch information
shayonj authored and sdepold committed Jan 10, 2022
1 parent 62cf284 commit 37f20a6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/dialects/abstract/query-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,13 @@ class QueryInterface {
* @param {string} tableName Table name to drop index from
* @param {string|string[]} indexNameOrAttributes Index name or list of attributes that in the index
* @param {object} [options] Query options
* @param {boolean} [options.concurrently] Pass CONCURRENTLY so other operations run while the index is created
*
* @returns {Promise}
*/
async removeIndex(tableName, indexNameOrAttributes, options) {
options = options || {};
const sql = this.queryGenerator.removeIndexQuery(tableName, indexNameOrAttributes);
const sql = this.queryGenerator.removeIndexQuery(tableName, indexNameOrAttributes, options);
return await this.sequelize.query(sql, options);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/dialects/mssql/connection-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class ConnectionManager extends AbstractConnectionManager {
}

validate(connection) {
return connection && (connection.loggedIn || (connection.state.name === "LoggedIn"));
return connection && (connection.loggedIn || connection.state.name === 'LoggedIn');
}
}

Expand Down
8 changes: 6 additions & 2 deletions lib/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,18 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
].join(' ');
}

removeIndexQuery(tableName, indexNameOrAttributes) {
removeIndexQuery(tableName, indexNameOrAttributes, options) {
let indexName = indexNameOrAttributes;

if (typeof indexName !== 'string') {
indexName = Utils.underscore(`${tableName}_${indexNameOrAttributes.join('_')}`);
}

return `DROP INDEX IF EXISTS ${this.quoteIdentifiers(indexName)}`;
return [
'DROP INDEX',
options && options.concurrently && 'CONCURRENTLY',
`IF EXISTS ${this.quoteIdentifiers(indexName)}`
].filter(Boolean).join(' ');
}

addLimitAndOffset(options) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/dialects/postgres/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,9 +1101,15 @@ if (dialect.startsWith('postgres')) {
}, {
arguments: ['User', ['foo', 'bar']],
expectation: 'DROP INDEX IF EXISTS "user_foo_bar"'
}, {
arguments: ['User', ['foo', 'bar'], { concurrently: true }],
expectation: 'DROP INDEX CONCURRENTLY IF EXISTS "user_foo_bar"'
}, {
arguments: ['User', 'mySchema.user_foo_bar'],
expectation: 'DROP INDEX IF EXISTS "mySchema"."user_foo_bar"'
}, {
arguments: ['User', 'mySchema.user_foo_bar', { concurrently: true }],
expectation: 'DROP INDEX CONCURRENTLY IF EXISTS "mySchema"."user_foo_bar"'
},

// Variants when quoteIdentifiers is false
Expand All @@ -1119,6 +1125,10 @@ if (dialect.startsWith('postgres')) {
arguments: ['User', 'mySchema.user_foo_bar'],
expectation: 'DROP INDEX IF EXISTS mySchema.user_foo_bar',
context: { options: { quoteIdentifiers: false } }
}, {
arguments: ['User', 'mySchema.user_foo_bar', { concurrently: true }],
expectation: 'DROP INDEX CONCURRENTLY IF EXISTS mySchema.user_foo_bar',
context: { options: { quoteIdentifiers: false } }
}
],

Expand Down

0 comments on commit 37f20a6

Please sign in to comment.