Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unify and fix tests queryGenerator.arithmeticQuery/dropTableQuery/showIndexesQuery/describeTableQuery #15235

Merged
merged 25 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
abc2789
meta: unify queryGenerator.arithmeticQuery tests
WikiRik Nov 4, 2022
e24af3a
meta: unify queryGenerator.dropTableQuery tests
WikiRik Nov 4, 2022
ccacacf
meta: unify queryGenerator.dropTableQuery cascade tests
WikiRik Nov 4, 2022
ff61b38
meta: unify queryGenerator.createTableQuery tests
WikiRik Nov 4, 2022
d2e713a
meta: unify queryGenerator.showIndexesQuery tests
WikiRik Nov 5, 2022
4f6d068
fix: improve dropTableQuery
WikiRik Nov 5, 2022
ea9e4ec
meta: add showIndexesQuery types
WikiRik Nov 5, 2022
1c7c5b6
Revert "meta: unify queryGenerator.createTableQuery tests"
WikiRik Nov 5, 2022
956237d
meta: fix sqlite unit tests
WikiRik Nov 5, 2022
47291e4
meta: reduce skipped tests
WikiRik Nov 5, 2022
bbf8331
meta: not pass options.cascade to dropTableQuery when not supported […
WikiRik Nov 5, 2022
5f768c7
Merge branch 'main' into WikiRik/query-generator-unit-tests-refactor
WikiRik Nov 5, 2022
e01d3ad
meta: add describeTableQuery tests
WikiRik Nov 5, 2022
e0fd934
fix: add support for tableName object in describeTableQuery for final…
WikiRik Nov 5, 2022
d8b784d
Merge branch 'main' into WikiRik/query-generator-unit-tests-refactor
WikiRik Nov 6, 2022
21af3c2
meta: implement review suggestions
WikiRik Nov 10, 2022
85ad38f
Merge branch 'main' into WikiRik/query-generator-unit-tests-refactor
WikiRik Nov 10, 2022
8c9788c
fix(mssql): fix ifExists
WikiRik Nov 10, 2022
3a8a57b
fix(mssql): undo changes to removeColumnQuery
WikiRik Nov 10, 2022
7d3099d
meta: only cascade when supported
WikiRik Nov 10, 2022
7bbd64d
Update test/unit/query-generator/arithmetic-query.test.ts
WikiRik Nov 16, 2022
2f17e6e
meta: implement review comments
WikiRik Nov 16, 2022
9847be0
Merge branch 'main' into WikiRik/query-generator-unit-tests-refactor
WikiRik Nov 16, 2022
11939bc
meta: implement _dropAllTables suggestion and add unit test
WikiRik Nov 16, 2022
ccbdf1e
meta: add integration test
WikiRik Nov 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/dialects/abstract/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
ephys marked this conversation as resolved.
Show resolved Hide resolved
// TODO: this should become `describeTableQuery(tableName: TableName): string`
describeTableQuery(tableName: TableName, schema?: string, schemaDelimiter?: string): string;
ephys marked this conversation as resolved.
Show resolved Hide resolved

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
Original file line number Diff line number Diff line change
@@ -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) {
WikiRik marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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') {
ephys marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
@@ -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) {
ephys marked this conversation as resolved.
Show resolved Hide resolved
let table = tableName;
let schema;

if (typeof table === 'object') {
schema = table.schema || undefined;
table = table.table;
} else if (options.schema) {
schema = options.schema;
WikiRik marked this conversation as resolved.
Show resolved Hide resolved
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