Skip to content

Commit

Permalink
fix: migrate removeIndexQuery to TypeScript (#15406)
Browse files Browse the repository at this point in the history
* fix(mssql): updated quoting methods

* meta: add remove index query options type

* feat: migrate removeIndexQuery to typescript

* feat: unify and fix removeIndexQuery tests

* fix(postgres): indexes can be schema qualified

* fix: remove blank lines in test

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>

* fix: remove unneeded test

* fix(mariadb): migrate function to typescript

* fix: remove unneeded type argument

* fix(postgres): cannot have casecade & concurrently

* fix(snowflake): remove unneeded joinSQLFragments

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>

* fix(snowflake): remove unused import

* fix(ibmi): use quoteIdentifier for if check

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>

* fix: should return string

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>

* fix(snowflake): indexes are not supported

* fix: only retrieve table details when required

* fix(postgres): remove check since all supported

* fix: use keyof for key checking

* fix: use REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS

* fix(ibmi): update test for quoteIdentifier

* fix: declare supported options outside of class

* fix: options should be optional in queries

* fix(postgres): always include schema in query

* fix(types): move removeIndex options to interface

* fix(types): update type to object

* fix(types): change options to interface

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
lohart13 and WikiRik committed Dec 13, 2022
1 parent 1690031 commit b44f9e0
Show file tree
Hide file tree
Showing 26 changed files with 421 additions and 183 deletions.
17 changes: 17 additions & 0 deletions src/dialects/abstract/query-generator-typescript.ts
Expand Up @@ -10,6 +10,15 @@ import type { AbstractDialect } from './index.js';

export type TableNameOrModel = TableName | ModelStatic;

// keep REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS updated when modifying this
export interface RemoveIndexQueryOptions {
concurrently?: boolean;
ifExists?: boolean;
cascade?: boolean;
}

export const REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS = new Set<keyof RemoveIndexQueryOptions>(['concurrently', 'ifExists', 'cascade']);

export interface QueryGeneratorOptions {
sequelize: Sequelize;
dialect: AbstractDialect;
Expand Down Expand Up @@ -48,6 +57,14 @@ export class AbstractQueryGeneratorTypeScript {
throw new Error(`showIndexesQuery has not been implemented in ${this.dialect.name}.`);
}

removeIndexQuery(
_tableName: TableNameOrModel,
_indexNameOrAttributes: string | string [],
_options?: RemoveIndexQueryOptions,
): string {
throw new Error(`removeIndexQuery has not been implemented in ${this.dialect.name}.`);
}

extractTableDetails(
tableNameOrModel: TableNameOrModel,
options?: { schema?: string, delimiter?: string },
Expand Down
16 changes: 13 additions & 3 deletions src/dialects/abstract/query-interface.d.ts
Expand Up @@ -16,7 +16,7 @@ import type { Sequelize, QueryRawOptions, QueryRawOptionsWithModel } from '../..
import type { Transaction } from '../../transaction';
import type { Fn, Literal, Col } from '../../utils/sequelize-method.js';
import type { DataType } from './data-types.js';
import type { TableNameOrModel } from './query-generator-typescript';
import type { RemoveIndexQueryOptions, TableNameOrModel } from './query-generator-typescript';
import type { AbstractQueryGenerator, AddColumnQueryOptions, RemoveColumnQueryOptions } from './query-generator.js';
import { AbstractQueryInterfaceTypeScript } from './query-interface-typescript';

Expand Down Expand Up @@ -185,6 +185,8 @@ export interface IndexOptions {

export interface QueryInterfaceIndexOptions extends IndexOptions, Omit<QiOptionsWithReplacements, 'type'> {}

export interface QueryInterfaceRemoveIndexOptions extends QueryInterfaceIndexOptions, RemoveIndexQueryOptions {}

export interface BaseConstraintOptions {
name?: string;
fields: string[];
Expand Down Expand Up @@ -438,8 +440,16 @@ export class AbstractQueryInterface extends AbstractQueryInterfaceTypeScript {
/**
* Removes an index of a table
*/
removeIndex(tableName: TableName, indexName: string, options?: QueryInterfaceIndexOptions): Promise<void>;
removeIndex(tableName: TableName, attributes: string[], options?: QueryInterfaceIndexOptions): Promise<void>;
removeIndex(
tableName: TableName,
indexName: string,
options?: QueryInterfaceRemoveIndexOptions
): Promise<void>;
removeIndex(
tableName: TableName,
attributes: string[],
options?: QueryInterfaceRemoveIndexOptions
): Promise<void>;

/**
* Adds constraints to a table
Expand Down
33 changes: 32 additions & 1 deletion src/dialects/db2/query-generator-typescript.ts
@@ -1,6 +1,11 @@
import { rejectInvalidOptions } from '../../utils/check';
import { joinSQLFragments } from '../../utils/join-sql-fragments';
import { generateIndexName } from '../../utils/string';
import { AbstractQueryGenerator } from '../abstract/query-generator';
import type { TableNameOrModel } from '../abstract/query-generator-typescript';
import { REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS } from '../abstract/query-generator-typescript';
import type { RemoveIndexQueryOptions, TableNameOrModel } from '../abstract/query-generator-typescript';

const REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS = new Set<keyof RemoveIndexQueryOptions>();

/**
* Temporary class to ease the TypeScript migration
Expand Down Expand Up @@ -33,4 +38,30 @@ export class Db2QueryGeneratorTypeScript extends AbstractQueryGenerator {
'ORDER BY NAME;',
]);
}

removeIndexQuery(
tableName: TableNameOrModel,
indexNameOrAttributes: string | string[],
options?: RemoveIndexQueryOptions,
) {
if (options) {
rejectInvalidOptions(
'removeIndexQuery',
this.dialect.name,
REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS,
options,
);
}

let indexName: string;
if (Array.isArray(indexNameOrAttributes)) {
const table = this.extractTableDetails(tableName);
indexName = generateIndexName(table, { fields: indexNameOrAttributes });
} else {
indexName = indexNameOrAttributes;
}

return `DROP INDEX ${this.quoteIdentifier(indexName)}`;
}
}
16 changes: 0 additions & 16 deletions src/dialects/db2/query-generator.js
Expand Up @@ -598,22 +598,6 @@ export class Db2QueryGenerator extends Db2QueryGeneratorTypeScript {
return `${sql} ORDER BY CONSTNAME;`;
}

removeIndexQuery(tableName, indexNameOrAttributes) {
const sql = 'DROP INDEX <%= indexName %>';
let indexName = indexNameOrAttributes;

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

const values = {
tableName: this.quoteIdentifiers(tableName),
indexName: this.quoteIdentifiers(indexName),
};

return _.template(sql, this._templateSettings)(values);
}

attributeToSQL(attribute, options) {
if (!_.isPlainObject(attribute)) {
attribute = {
Expand Down
40 changes: 39 additions & 1 deletion src/dialects/ibmi/query-generator-typescript.ts
@@ -1,6 +1,11 @@
import { rejectInvalidOptions } from '../../utils/check';
import { joinSQLFragments } from '../../utils/join-sql-fragments';
import { generateIndexName } from '../../utils/string';
import { AbstractQueryGenerator } from '../abstract/query-generator';
import type { TableNameOrModel } from '../abstract/query-generator-typescript';
import { REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS } from '../abstract/query-generator-typescript';
import type { RemoveIndexQueryOptions, TableNameOrModel } from '../abstract/query-generator-typescript';

const REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS = new Set<keyof RemoveIndexQueryOptions>(['ifExists']);

/**
* Temporary class to ease the TypeScript migration
Expand Down Expand Up @@ -45,4 +50,37 @@ export class IBMiQueryGeneratorTypeScript extends AbstractQueryGenerator {
`and QSYS2.SYSINDEXES.TABLE_NAME = ${this.escape(table.tableName)}`,
]);
}

removeIndexQuery(
tableName: TableNameOrModel,
indexNameOrAttributes: string | string[],
options?: RemoveIndexQueryOptions,
) {
if (options) {
rejectInvalidOptions(
'removeIndexQuery',
this.dialect.name,
REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS,
options,
);
}

let indexName: string;
if (Array.isArray(indexNameOrAttributes)) {
const table = this.extractTableDetails(tableName);
indexName = generateIndexName(table, { fields: indexNameOrAttributes });
} else {
indexName = indexNameOrAttributes;
}

return joinSQLFragments([
'BEGIN',
options?.ifExists ? `IF EXISTS (SELECT * FROM QSYS2.SYSINDEXES WHERE INDEX_NAME = ${this.quoteIdentifier(indexName)}) THEN` : '',
`DROP INDEX ${this.quoteIdentifier(indexName)};`,
'COMMIT;',
options?.ifExists ? 'END IF;' : '',
'END',
]);
}
}
10 changes: 0 additions & 10 deletions src/dialects/ibmi/query-generator.js
Expand Up @@ -598,16 +598,6 @@ export class IBMiQueryGenerator extends IBMiQueryGeneratorTypeScript {
return sql;
}

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

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

return `BEGIN IF EXISTS (SELECT * FROM QSYS2.SYSINDEXES WHERE INDEX_NAME = '${indexName}') THEN DROP INDEX "${indexName}"; COMMIT; END IF; END`;
}

// bindParam(bind) {
// return value => {
// bind.push(value);
Expand Down
45 changes: 45 additions & 0 deletions src/dialects/mariadb/query-generator-typescript.ts
@@ -0,0 +1,45 @@
import { rejectInvalidOptions } from '../../utils/check';
import { joinSQLFragments } from '../../utils/join-sql-fragments';
import { generateIndexName } from '../../utils/string';
import { REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS } from '../abstract/query-generator-typescript';
import type { RemoveIndexQueryOptions, TableNameOrModel } from '../abstract/query-generator-typescript';
import { MySqlQueryGenerator } from '../mysql/query-generator.js';

const REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS = new Set<keyof RemoveIndexQueryOptions>(['ifExists']);

/**
* Temporary class to ease the TypeScript migration
*/
export class MariaDbQueryGeneratorTypeScript extends MySqlQueryGenerator {
removeIndexQuery(
tableName: TableNameOrModel,
indexNameOrAttributes: string | string[],
options?: RemoveIndexQueryOptions,
) {
if (options) {
rejectInvalidOptions(
'removeIndexQuery',
this.dialect.name,
REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS,
options,
);
}

let indexName;
if (Array.isArray(indexNameOrAttributes)) {
const table = this.extractTableDetails(tableName);
indexName = generateIndexName(table, { fields: indexNameOrAttributes });
} else {
indexName = indexNameOrAttributes;
}

return joinSQLFragments([
'DROP INDEX',
options?.ifExists ? 'IF EXISTS' : '',
this.quoteIdentifier(indexName),
'ON',
this.quoteTable(tableName),
]);
}
}
4 changes: 2 additions & 2 deletions src/dialects/mariadb/query-generator.js
Expand Up @@ -2,11 +2,11 @@

import { normalizeDataType } from '../abstract/data-types-utils';
import { joinSQLFragments } from '../../utils/join-sql-fragments.js';
import { MariaDbQueryGeneratorTypeScript } from './query-generator-typescript';

const { MySqlQueryGenerator } = require('../mysql/query-generator');
const _ = require('lodash');

export class MariaDbQueryGenerator extends MySqlQueryGenerator {
export class MariaDbQueryGenerator extends MariaDbQueryGeneratorTypeScript {

_getTechnicalSchemaNames() {
return ['MYSQL', 'INFORMATION_SCHEMA', 'PERFORMANCE_SCHEMA', 'mysql', 'information_schema', 'performance_schema'];
Expand Down
39 changes: 38 additions & 1 deletion src/dialects/mssql/query-generator-typescript.ts
@@ -1,6 +1,11 @@
import { rejectInvalidOptions } from '../../utils/check';
import { joinSQLFragments } from '../../utils/join-sql-fragments';
import { generateIndexName } from '../../utils/string';
import { AbstractQueryGenerator } from '../abstract/query-generator';
import type { TableNameOrModel } from '../abstract/query-generator-typescript';
import { REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS } from '../abstract/query-generator-typescript';
import type { RemoveIndexQueryOptions, TableNameOrModel } from '../abstract/query-generator-typescript';

const REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS = new Set<keyof RemoveIndexQueryOptions>(['ifExists']);

/**
* Temporary class to ease the TypeScript migration
Expand Down Expand Up @@ -46,4 +51,36 @@ export class MsSqlQueryGeneratorTypeScript extends AbstractQueryGenerator {
showIndexesQuery(tableName: TableNameOrModel) {
return `EXEC sys.sp_helpindex @objname = ${this.escape(this.quoteTable(tableName))};`;
}

removeIndexQuery(
tableName: TableNameOrModel,
indexNameOrAttributes: string | string[],
options?: RemoveIndexQueryOptions,
) {
if (options) {
rejectInvalidOptions(
'removeIndexQuery',
this.dialect.name,
REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS,
options,
);
}

let indexName: string;
if (Array.isArray(indexNameOrAttributes)) {
const table = this.extractTableDetails(tableName);
indexName = generateIndexName(table, { fields: indexNameOrAttributes });
} else {
indexName = indexNameOrAttributes;
}

return joinSQLFragments([
'DROP INDEX',
options?.ifExists ? 'IF EXISTS' : '',
this.quoteIdentifier(indexName),
'ON',
this.quoteTable(tableName),
]);
}
}
10 changes: 0 additions & 10 deletions src/dialects/mssql/query-generator.js
Expand Up @@ -601,16 +601,6 @@ export class MsSqlQueryGenerator extends MsSqlQueryGeneratorTypeScript {
return `EXEC sp_helpconstraint @objname = ${this.escape(this.quoteTable(tableName))};`;
}

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

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

return `DROP INDEX ${this.quoteIdentifiers(indexName)} ON ${this.quoteIdentifiers(tableName)}`;
}

attributeToSQL(attribute, options) {
if (!_.isPlainObject(attribute)) {
attribute = {
Expand Down
33 changes: 32 additions & 1 deletion src/dialects/mysql/query-generator-typescript.ts
@@ -1,5 +1,10 @@
import { rejectInvalidOptions } from '../../utils/check';
import { generateIndexName } from '../../utils/string';
import { AbstractQueryGenerator } from '../abstract/query-generator';
import type { TableNameOrModel } from '../abstract/query-generator-typescript';
import { REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS } from '../abstract/query-generator-typescript';
import type { RemoveIndexQueryOptions, TableNameOrModel } from '../abstract/query-generator-typescript';

const REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS = new Set<keyof RemoveIndexQueryOptions>();

/**
* Temporary class to ease the TypeScript migration
Expand All @@ -12,4 +17,30 @@ export class MySqlQueryGeneratorTypeScript extends AbstractQueryGenerator {
showIndexesQuery(tableName: TableNameOrModel) {
return `SHOW INDEX FROM ${this.quoteTable(tableName)}`;
}

removeIndexQuery(
tableName: TableNameOrModel,
indexNameOrAttributes: string | string[],
options?: RemoveIndexQueryOptions,
) {
if (options) {
rejectInvalidOptions(
'removeIndexQuery',
this.dialect.name,
REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS,
options,
);
}

let indexName: string;
if (Array.isArray(indexNameOrAttributes)) {
const table = this.extractTableDetails(tableName);
indexName = generateIndexName(table, { fields: indexNameOrAttributes });
} else {
indexName = indexNameOrAttributes;
}

return `DROP INDEX ${this.quoteIdentifier(indexName)} ON ${this.quoteTable(tableName)}`;
}
}
15 changes: 0 additions & 15 deletions src/dialects/mysql/query-generator.js
Expand Up @@ -375,21 +375,6 @@ export class MySqlQueryGenerator extends MySqlQueryGeneratorTypeScript {
]);
}

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

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

return joinSQLFragments([
'DROP INDEX',
this.quoteIdentifier(indexName),
'ON',
this.quoteTable(tableName),
]);
}

attributeToSQL(attribute, options) {
if (!_.isPlainObject(attribute)) {
attribute = {
Expand Down

0 comments on commit b44f9e0

Please sign in to comment.