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 11 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
15 changes: 12 additions & 3 deletions src/dialects/abstract/query-generator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type HandleSequelizeMethodOptions = ParameterOptions & {

};

// 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 @@ -74,13 +74,18 @@ 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[];
Expand Down Expand Up @@ -144,6 +149,10 @@ 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

createSchemaQuery(schemaName: string, options?: CreateSchemaQueryOptions): string;
dropSchemaQuery(schemaName: string): string | { query: string, bind?: unknown[] };
listSchemasQuery(options?: ListSchemasQueryOptions): string;
Expand Down
22 changes: 18 additions & 4 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 { isModelStatic } from '../../utils/model-utils';
import { injectReplacements } from '../../utils/sql';

Expand All @@ -26,9 +27,10 @@ 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 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 DROP_TABLE_QUERY_SUPPORTABLE_OPTIONS = new Set(['cascade']);
export const LIST_SCHEMAS_QUERY_SUPPORTABLE_OPTIONS = new Set(['skip']);

/**
* Abstract Query Generator
Expand Down Expand Up @@ -144,7 +146,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
9 changes: 8 additions & 1 deletion src/dialects/abstract/query-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,14 @@ export class QueryInterface {
options = { ...options };
options.cascade = options.cascade || options.force || false;

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

// TODO: this should be refactored to probably use DialectSupports
if (this.queryGenerator.dialect.name === 'postgres') {
sql = this.queryGenerator.dropTableQuery(tableName, options);
} else {
sql = this.queryGenerator.dropTableQuery(tableName);
}
ephys marked this conversation as resolved.
Show resolved Hide resolved

await this.sequelize.queryRaw(sql, options);
}
Expand Down
11 changes: 6 additions & 5 deletions src/dialects/db2/query-generator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

import { rejectInvalidOptions, removeTrailingSemicolon } from '../../utils';
import { CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTION } from '../abstract/query-generator';
import { rejectInvalidOptions } from '../../utils/check';
import { removeTrailingSemicolon } from '../../utils/string';
import { CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTIONS } from '../abstract/query-generator';

const _ = require('lodash');
const Utils = require('../../utils');
Expand All @@ -10,7 +11,7 @@ const { AbstractQueryGenerator } = require('../abstract/query-generator');
const randomBytes = require('crypto').randomBytes;
const { Op } = require('../../operators');

const CREATE_SCHEMA_SUPPORTED_OPTIONS = new Set();
const CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS = new Set();

/* istanbul ignore next */
function throwMethodUndefined(methodName) {
Expand All @@ -33,8 +34,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
34 changes: 20 additions & 14 deletions src/dialects/ibmi/query-generator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use strict';

import { rejectInvalidOptions, removeTrailingSemicolon } from '../../utils';
import { CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTION } from '../abstract/query-generator';
import { rejectInvalidOptions } from '../../utils/check';
import { removeTrailingSemicolon } from '../../utils/string';
import {
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTIONS,
DROP_TABLE_QUERY_SUPPORTABLE_OPTIONS,
} from '../abstract/query-generator';

const Utils = require('../../utils');
const util = require('util');
Expand All @@ -12,7 +16,9 @@ const { Model } = require('../../model');
const SqlString = require('../../sql-string');

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

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

export class IBMiQueryGenerator extends AbstractQueryGenerator {

Expand All @@ -27,8 +33,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 @@ -130,17 +136,17 @@ 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) {
Expand Down
32 changes: 22 additions & 10 deletions src/dialects/mssql/query-generator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

import { rejectInvalidOptions } from '../../utils';
import { rejectInvalidOptions } from '../../utils/check';
import {
CREATE_DATABASE_QUERY_SUPPORTABLE_OPTION,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTION,
CREATE_DATABASE_QUERY_SUPPORTABLE_OPTIONS,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTIONS,
DROP_TABLE_QUERY_SUPPORTABLE_OPTIONS,
} from '../abstract/query-generator';

const _ = require('lodash');
Expand All @@ -20,17 +21,18 @@ function throwMethodUndefined(methodName) {
throw new Error(`The method "${methodName}" is not defined! Please add it to your sql dialect.`);
}

const CREATE_DATABASE_SUPPORTED_OPTIONS = new Set(['collate']);
const CREATE_SCHEMA_SUPPORTED_OPTIONS = new Set();
const CREATE_DATABASE_QUERY_SUPPORTED_OPTIONS = new Set(['collate']);
const CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS = new Set();
const DROP_TABLE_QUERY_SUPPORTED_OPTIONS = new Set();

export class MsSqlQueryGenerator extends AbstractQueryGenerator {
createDatabaseQuery(databaseName, options) {
if (options) {
rejectInvalidOptions(
'createDatabaseQuery',
this.dialect.name,
CREATE_DATABASE_QUERY_SUPPORTABLE_OPTION,
CREATE_DATABASE_SUPPORTED_OPTIONS,
CREATE_DATABASE_QUERY_SUPPORTABLE_OPTIONS,
CREATE_DATABASE_QUERY_SUPPORTED_OPTIONS,
options,
);
}
Expand Down Expand Up @@ -64,8 +66,8 @@ export class MsSqlQueryGenerator 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 @@ -276,7 +278,17 @@ export class MsSqlQueryGenerator extends AbstractQueryGenerator {
return `SELECT TABLE_NAME, TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = ${this.escape(tableName)} AND TABLE_SCHEMA = ${this.escape(schemaName)}`;
}

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

const quoteTbl = this.quoteTable(tableName);

return Utils.joinSQLFragments([
Expand Down
32 changes: 21 additions & 11 deletions src/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

import { rejectInvalidOptions } from '../../utils';
import { rejectInvalidOptions } from '../../utils/check';
import {
CREATE_DATABASE_QUERY_SUPPORTABLE_OPTION,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTION,
CREATE_DATABASE_QUERY_SUPPORTABLE_OPTIONS,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTIONS,
DROP_TABLE_QUERY_SUPPORTABLE_OPTIONS,
} from '../abstract/query-generator';

const Utils = require('../../utils');
Expand All @@ -21,8 +22,9 @@ const _ = require('lodash');
*/
const POSTGRES_RESERVED_WORDS = 'all,analyse,analyze,and,any,array,as,asc,asymmetric,authorization,binary,both,case,cast,check,collate,collation,column,concurrently,constraint,create,cross,current_catalog,current_date,current_role,current_schema,current_time,current_timestamp,current_user,default,deferrable,desc,distinct,do,else,end,except,false,fetch,for,foreign,freeze,from,full,grant,group,having,ilike,in,initially,inner,intersect,into,is,isnull,join,lateral,leading,left,like,limit,localtime,localtimestamp,natural,not,notnull,null,offset,on,only,or,order,outer,overlaps,placing,primary,references,returning,right,select,session_user,similar,some,symmetric,table,tablesample,then,to,trailing,true,union,unique,user,using,variadic,verbose,when,where,window,with'.split(',');

const CREATE_DATABASE_SUPPORTED_OPTIONS = new Set(['encoding', 'collate', 'ctype', 'template']);
const CREATE_SCHEMA_SUPPORTED_OPTIONS = new Set([]);
const CREATE_DATABASE_QUERY_SUPPORTED_OPTIONS = new Set(['encoding', 'collate', 'ctype', 'template']);
const CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS = new Set();
const DROP_TABLE_QUERY_SUPPORTED_OPTIONS = new Set(['cascade']);

export class PostgresQueryGenerator extends AbstractQueryGenerator {
setSearchPath(searchPath) {
Expand All @@ -34,8 +36,8 @@ export class PostgresQueryGenerator extends AbstractQueryGenerator {
rejectInvalidOptions(
'createDatabaseQuery',
this.dialect.name,
CREATE_DATABASE_QUERY_SUPPORTABLE_OPTION,
CREATE_DATABASE_SUPPORTED_OPTIONS,
CREATE_DATABASE_QUERY_SUPPORTABLE_OPTIONS,
CREATE_DATABASE_QUERY_SUPPORTED_OPTIONS,
options,
);
}
Expand All @@ -62,8 +64,8 @@ export class PostgresQueryGenerator 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 @@ -151,9 +153,17 @@ export class PostgresQueryGenerator extends AbstractQueryGenerator {
}

dropTableQuery(tableName, options) {
options = options || {};
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)}${options.cascade ? ' CASCADE' : ''};`;
return `DROP TABLE IF EXISTS ${this.quoteTable(tableName)}${options?.cascade ? ' CASCADE' : ''};`;
}

showTablesQuery() {
Expand Down
4 changes: 3 additions & 1 deletion src/dialects/snowflake/query-generator.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AbstractQueryGenerator } from '../abstract/query-generator.js';

export class SnowflakeQueryGenerator extends AbstractQueryGenerator {}
export class SnowflakeQueryGenerator extends AbstractQueryGenerator {
showIndexesQuery(): string;
ephys marked this conversation as resolved.
Show resolved Hide resolved
}
25 changes: 13 additions & 12 deletions src/dialects/snowflake/query-generator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

import { rejectInvalidOptions } from '../../utils';
import { rejectInvalidOptions } from '../../utils/check';
import {
CREATE_DATABASE_QUERY_SUPPORTABLE_OPTION,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTION, LIST_SCHEMAS_QUERY_SUPPORTABLE_OPTION,
CREATE_DATABASE_QUERY_SUPPORTABLE_OPTIONS,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTIONS,
LIST_SCHEMAS_QUERY_SUPPORTABLE_OPTIONS,
} from '../abstract/query-generator';

const _ = require('lodash');
Expand Down Expand Up @@ -40,9 +41,9 @@ const SNOWFLAKE_RESERVED_WORDS = 'account,all,alter,and,any,as,between,by,case,c

const typeWithoutDefault = new Set(['BLOB', 'TEXT', 'GEOMETRY', 'JSON']);

const CREATE_DATABASE_SUPPORTED_OPTIONS = new Set(['charset', 'collate']);
const CREATE_SCHEMA_SUPPORTED_OPTIONS = new Set();
const LIST_SCHEMAS_SUPPORTED_OPTIONS = new Set();
const CREATE_DATABASE_QUERY_SUPPORTED_OPTIONS = new Set(['charset', 'collate']);
const CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS = new Set();
const LIST_SCHEMAS_QUERY_SUPPORTED_OPTIONS = new Set();

export class SnowflakeQueryGenerator extends AbstractQueryGenerator {
constructor(options) {
Expand All @@ -60,8 +61,8 @@ export class SnowflakeQueryGenerator extends AbstractQueryGenerator {
rejectInvalidOptions(
'createDatabaseQuery',
this.dialect.name,
CREATE_DATABASE_QUERY_SUPPORTABLE_OPTION,
CREATE_DATABASE_SUPPORTED_OPTIONS,
CREATE_DATABASE_QUERY_SUPPORTABLE_OPTIONS,
CREATE_DATABASE_QUERY_SUPPORTED_OPTIONS,
options,
);
}
Expand All @@ -88,8 +89,8 @@ export class SnowflakeQueryGenerator 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 All @@ -106,8 +107,8 @@ export class SnowflakeQueryGenerator extends AbstractQueryGenerator {
rejectInvalidOptions(
'listSchemasQuery',
this.dialect.name,
LIST_SCHEMAS_QUERY_SUPPORTABLE_OPTION,
LIST_SCHEMAS_SUPPORTED_OPTIONS,
LIST_SCHEMAS_QUERY_SUPPORTABLE_OPTIONS,
LIST_SCHEMAS_QUERY_SUPPORTED_OPTIONS,
options,
);
}
Expand Down