Skip to content

Commit

Permalink
fix: unify and fix tests queryGenerator.arithmeticQuery/`dropTableQ…
Browse files Browse the repository at this point in the history
…uery`/`showIndexesQuery`/`describeTableQuery` (#15235)


BREAKING CHANGE: **ibmi** - dropTableQuery does not accept schema in the options anymore, use a tableName object
  • Loading branch information
WikiRik committed Nov 16, 2022
1 parent 79696f3 commit c68f8c8
Show file tree
Hide file tree
Showing 29 changed files with 814 additions and 539 deletions.
6 changes: 6 additions & 0 deletions src/dialects/abstract/index.ts
Expand Up @@ -213,6 +213,9 @@ export type DialectSupports = {

/** Whether this dialect supports changing the global timezone option */
globalTimeZoneConfig: boolean,
dropTable: {
cascade: boolean,
},
};

type TypeParser = (...params: any[]) => unknown;
Expand Down Expand Up @@ -330,6 +333,9 @@ export abstract class AbstractDialect {
escapeStringConstants: false,
milliseconds: true,
globalTimeZoneConfig: false,
dropTable: {
cascade: false,
},
};

protected static extendSupport(supportsOverwrite: DeepPartial<DialectSupports>): DialectSupports {
Expand Down
19 changes: 16 additions & 3 deletions src/dialects/abstract/query-generator.d.ts
Expand Up @@ -79,7 +79,7 @@ interface QueryGeneratorOptions {
dialect: AbstractDialect;
}

// keep CREATE_DATABASE_QUERY_OPTION_NAMES updated when modifying this
// keep CREATE_DATABASE_QUERY_SUPPORTABLE_OPTIONS updated when modifying this
export interface CreateDatabaseQueryOptions {
collate?: string;
charset?: string;
Expand All @@ -88,22 +88,29 @@ export interface CreateDatabaseQueryOptions {
template?: string;
}

// keep CREATE_SCHEMA_QUERY_OPTION_NAMES updated when modifying this
// keep CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTIONS updated when modifying this
export interface CreateSchemaQueryOptions {
collate?: string;
charset?: string;
}

// keep LIST_SCHEMAS_QUERY_OPTION_NAMES updated when modifying this
// keep DROP_TABLE_QUERY_SUPPORTABLE_OPTIONS updated when modifying this
export interface DropTableQueryOptions {
cascade?: boolean;
}

// keep LIST_SCHEMAS_QUERY_SUPPORTABLE_OPTIONS updated when modifying this
export interface ListSchemasQueryOptions {
/** List of schemas to exclude from output */
skip?: string[];
}

// keep ADD_COLUMN_QUERY_SUPPORTABLE_OPTIONS updated when modifying this
export interface AddColumnQueryOptions {
ifNotExists?: boolean;
}

// keep REMOVE_COLUMN_QUERY_SUPPORTABLE_OPTIONS updated when modifying this
export interface RemoveColumnQueryOptions {
ifExists?: boolean;
}
Expand Down Expand Up @@ -194,6 +201,12 @@ export class AbstractQueryGenerator {
options?: ArithmeticQueryOptions,
): string;

showIndexesQuery(tableName: TableName): string;

dropTableQuery(tableName: TableName, options?: DropTableQueryOptions): string;
// TODO: this should become `describeTableQuery(tableName: TableName): string`
describeTableQuery(tableName: TableName, schema?: string, schemaDelimiter?: string): string;

createSchemaQuery(schemaName: string, options?: CreateSchemaQueryOptions): string;
dropSchemaQuery(schemaName: string): string | { query: string, bind?: unknown[] };
listSchemasQuery(options?: ListSchemasQueryOptions): string;
Expand Down
26 changes: 20 additions & 6 deletions src/dialects/abstract/query-generator.js
@@ -1,5 +1,6 @@
'use strict';

import { rejectInvalidOptions } from '../../utils/check';
import { getTextDataTypeForDialect } from '../../sql-string';
import { isNullish } from '../../utils';
import { isModelStatic } from '../../utils/model-utils';
Expand Down Expand Up @@ -30,11 +31,12 @@ const { _validateIncludedElements } = require('../../model-internals');
* It is used to validate the options passed to {@link QueryGenerator#createDatabaseQuery},
* as not all of them are supported by all dialects.
*/
export const CREATE_DATABASE_QUERY_SUPPORTABLE_OPTION = new Set(['collate', 'charset', 'encoding', 'ctype', 'template']);
export const CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTION = new Set(['collate', 'charset']);
export const LIST_SCHEMAS_QUERY_SUPPORTABLE_OPTION = new Set(['skip']);
export const ADD_COLUMN_QUERY_SUPPORTABLE_OPTION = new Set(['ifNotExists']);
export const REMOVE_COLUMN_QUERY_SUPPORTABLE_OPTION = new Set(['ifExists']);
export const CREATE_DATABASE_QUERY_SUPPORTABLE_OPTIONS = new Set(['collate', 'charset', 'encoding', 'ctype', 'template']);
export const CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTIONS = new Set(['collate', 'charset']);
export const LIST_SCHEMAS_QUERY_SUPPORTABLE_OPTIONS = new Set(['skip']);
export const DROP_TABLE_QUERY_SUPPORTABLE_OPTIONS = new Set(['cascade']);
export const ADD_COLUMN_QUERY_SUPPORTABLE_OPTIONS = new Set(['ifNotExists']);
export const REMOVE_COLUMN_QUERY_SUPPORTABLE_OPTIONS = new Set(['ifExists']);

/**
* Abstract Query Generator
Expand Down Expand Up @@ -150,7 +152,19 @@ export class AbstractQueryGenerator {
return `DESCRIBE ${table};`;
}

dropTableQuery(tableName) {
dropTableQuery(tableName, options) {
const DROP_TABLE_QUERY_SUPPORTED_OPTIONS = new Set();

if (options) {
rejectInvalidOptions(
'dropTableQuery',
this.dialect.name,
DROP_TABLE_QUERY_SUPPORTABLE_OPTIONS,
DROP_TABLE_QUERY_SUPPORTED_OPTIONS,
options,
);
}

return `DROP TABLE IF EXISTS ${this.quoteTable(tableName)};`;
}

Expand Down
16 changes: 11 additions & 5 deletions src/dialects/abstract/query-interface.js
Expand Up @@ -275,10 +275,11 @@ export class QueryInterface {
*
* @returns {Promise}
*/
async dropTable(tableName, options) {
// if we're forcing we should be cascading unless explicitly stated otherwise
options = { ...options };
options.cascade = options.cascade || options.force || false;
async dropTable(tableName, options = {}) {
options.cascade = options.cascade != null ? options.cascade
// TODO: dropTable should not accept a "force" option, `sync()` should set `cascade` itself if its force option is true
: (options.force && this.queryGenerator.dialect.supports.dropTable.cascade) ? true
: undefined;

const sql = this.queryGenerator.dropTableQuery(tableName, options);

Expand All @@ -289,7 +290,12 @@ export class QueryInterface {
for (const tableName of tableNames) {
// if tableName is not in the Array of tables names then don't drop it
if (!skip.includes(tableName.tableName || tableName)) {
await this.dropTable(tableName, { ...options, cascade: true });
await this.dropTable(tableName, {
// enable "cascade" by default if supported by this dialect,
// but let the user override the default
cascade: this.queryGenerator.dialect.supports.dropTable.cascade ? true : undefined,
...options,
});
}
}
}
Expand Down
28 changes: 17 additions & 11 deletions src/dialects/db2/query-generator.js
@@ -1,12 +1,13 @@
'use strict';

import { rejectInvalidOptions } from '../../utils/check';
import { removeTrailingSemicolon } from '../../utils/string';
import { defaultValueSchemable } from '../../utils/query-builder-utils';
import { attributeTypeToSql, normalizeDataType } from '../abstract/data-types-utils';
import { rejectInvalidOptions, removeTrailingSemicolon } from '../../utils';
import {
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTION,
ADD_COLUMN_QUERY_SUPPORTABLE_OPTION,
REMOVE_COLUMN_QUERY_SUPPORTABLE_OPTION,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTIONS,
ADD_COLUMN_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_COLUMN_QUERY_SUPPORTABLE_OPTIONS,
} from '../abstract/query-generator';

const _ = require('lodash');
Expand All @@ -16,9 +17,9 @@ const { AbstractQueryGenerator } = require('../abstract/query-generator');
const randomBytes = require('crypto').randomBytes;
const { Op } = require('../../operators');

const CREATE_SCHEMA_SUPPORTED_OPTIONS = new Set();
const ADD_COLUMN_QUERY_SUPPORTED_OPTIONS = new Set([]);
const REMOVE_COLUMN_QUERY_SUPPORTED_OPTIONS = new Set([]);
const CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS = new Set();
const ADD_COLUMN_QUERY_SUPPORTED_OPTIONS = new Set();
const REMOVE_COLUMN_QUERY_SUPPORTED_OPTIONS = new Set();

/* istanbul ignore next */
function throwMethodUndefined(methodName) {
Expand All @@ -41,8 +42,8 @@ export class Db2QueryGenerator extends AbstractQueryGenerator {
rejectInvalidOptions(
'createSchemaQuery',
this.dialect.name,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTION,
CREATE_SCHEMA_SUPPORTED_OPTIONS,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTIONS,
CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS,
options,
);
}
Expand Down Expand Up @@ -180,6 +181,11 @@ export class Db2QueryGenerator extends AbstractQueryGenerator {
}

describeTableQuery(tableName, schema) {
if (typeof tableName === 'object') {
schema = tableName.schema || schema;
tableName = tableName.tableName;
}

let sql = [
'SELECT NAME AS "Name", TBNAME AS "Table", TBCREATOR AS "Schema",',
'TRIM(COLTYPE) AS "Type", LENGTH AS "Length", SCALE AS "Scale",',
Expand Down Expand Up @@ -227,7 +233,7 @@ export class Db2QueryGenerator extends AbstractQueryGenerator {
rejectInvalidOptions(
'addColumnQuery',
this.dialect.name,
ADD_COLUMN_QUERY_SUPPORTABLE_OPTION,
ADD_COLUMN_QUERY_SUPPORTABLE_OPTIONS,
ADD_COLUMN_QUERY_SUPPORTED_OPTIONS,
options,
);
Expand Down Expand Up @@ -260,7 +266,7 @@ export class Db2QueryGenerator extends AbstractQueryGenerator {
rejectInvalidOptions(
'removeColumnQuery',
this.dialect.name,
REMOVE_COLUMN_QUERY_SUPPORTABLE_OPTION,
REMOVE_COLUMN_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_COLUMN_QUERY_SUPPORTED_OPTIONS,
options,
);
Expand Down
48 changes: 28 additions & 20 deletions src/dialects/ibmi/query-generator.js
@@ -1,12 +1,14 @@
'use strict';

import { rejectInvalidOptions } from '../../utils/check';
import { removeTrailingSemicolon } from '../../utils/string';
import { defaultValueSchemable } from '../../utils/query-builder-utils';
import { attributeTypeToSql, normalizeDataType } from '../abstract/data-types-utils';
import { rejectInvalidOptions, removeTrailingSemicolon } from '../../utils';
import {
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTION,
ADD_COLUMN_QUERY_SUPPORTABLE_OPTION,
REMOVE_COLUMN_QUERY_SUPPORTABLE_OPTION,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTIONS,
DROP_TABLE_QUERY_SUPPORTABLE_OPTIONS,
ADD_COLUMN_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_COLUMN_QUERY_SUPPORTABLE_OPTIONS,
} from '../abstract/query-generator';

const Utils = require('../../utils');
Expand All @@ -18,9 +20,11 @@ const { Model } = require('../../model');
const SqlString = require('../../sql-string');

const typeWithoutDefault = new Set(['BLOB']);
const CREATE_SCHEMA_SUPPORTED_OPTIONS = new Set();
const ADD_COLUMN_QUERY_SUPPORTED_OPTIONS = new Set([]);
const REMOVE_COLUMN_QUERY_SUPPORTED_OPTIONS = new Set([]);

const CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS = new Set();
const DROP_TABLE_QUERY_SUPPORTED_OPTIONS = new Set();
const ADD_COLUMN_QUERY_SUPPORTED_OPTIONS = new Set();
const REMOVE_COLUMN_QUERY_SUPPORTED_OPTIONS = new Set();

export class IBMiQueryGenerator extends AbstractQueryGenerator {

Expand All @@ -35,8 +39,8 @@ export class IBMiQueryGenerator extends AbstractQueryGenerator {
rejectInvalidOptions(
'createSchemaQuery',
this.dialect.name,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTION,
CREATE_SCHEMA_SUPPORTED_OPTIONS,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTIONS,
CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS,
options,
);
}
Expand Down Expand Up @@ -138,20 +142,24 @@ export class IBMiQueryGenerator extends AbstractQueryGenerator {
}

dropTableQuery(tableName, options) {
let table = tableName;
let schema;

if (typeof table === 'object') {
schema = table.schema || undefined;
table = table.table;
} else if (options.schema) {
schema = options.schema;
if (options) {
rejectInvalidOptions(
'dropTableQuery',
this.dialect.name,
DROP_TABLE_QUERY_SUPPORTABLE_OPTIONS,
DROP_TABLE_QUERY_SUPPORTED_OPTIONS,
options,
);
}

return `DROP TABLE IF EXISTS ${schema ? `"${schema}".` : ''}"${table}"`;
return `DROP TABLE IF EXISTS ${this.quoteTable(tableName)}`;
}

describeTableQuery(tableName, schema) {
if (typeof tableName === 'object') {
schema = tableName.schema || schema;
tableName = tableName.tableName;
}

const sql
= `SELECT
Expand Down Expand Up @@ -186,7 +194,7 @@ export class IBMiQueryGenerator extends AbstractQueryGenerator {
rejectInvalidOptions(
'addColumnQuery',
this.dialect.name,
ADD_COLUMN_QUERY_SUPPORTABLE_OPTION,
ADD_COLUMN_QUERY_SUPPORTABLE_OPTIONS,
ADD_COLUMN_QUERY_SUPPORTED_OPTIONS,
options,
);
Expand Down Expand Up @@ -214,7 +222,7 @@ export class IBMiQueryGenerator extends AbstractQueryGenerator {
rejectInvalidOptions(
'removeColumnQuery',
this.dialect.name,
REMOVE_COLUMN_QUERY_SUPPORTABLE_OPTION,
REMOVE_COLUMN_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_COLUMN_QUERY_SUPPORTED_OPTIONS,
options,
);
Expand Down

0 comments on commit c68f8c8

Please sign in to comment.