Skip to content

Commit

Permalink
fix: type createTableQuery and overhaul unit test suite (#15526)
Browse files Browse the repository at this point in the history
  • Loading branch information
WikiRik committed Mar 24, 2023
1 parent 0435174 commit b5db02d
Show file tree
Hide file tree
Showing 14 changed files with 491 additions and 403 deletions.
21 changes: 21 additions & 0 deletions packages/core/src/dialects/abstract/query-generator.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ export interface CreateSchemaQueryOptions {
charset?: string;
}

export interface CreateTableQueryOptions {
collate?: string;
charset?: string;
engine?: string;
rowFormat?: string;
comment?: string;
initialAutoIncrement?: number;
/**
* Used for compound unique keys.
*/
uniqueKeys?: Array<{ fields: string[] }>
| { [indexName: string]: { fields: string[] } };
}

// keep DROP_TABLE_QUERY_SUPPORTABLE_OPTIONS updated when modifying this
export interface DropTableQueryOptions {
cascade?: boolean;
Expand Down Expand Up @@ -158,6 +172,13 @@ export class AbstractQueryGenerator extends AbstractQueryGeneratorTypeScript {
options?: ArithmeticQueryOptions,
): string;

createTableQuery(
tableName: TableNameOrModel,
// TODO: rename attributes to columns and accept a map of attributes in the implementation when migrating to TS, see https://github.com/sequelize/sequelize/pull/15526/files#r1143840411
columns: { [columnName: string]: string },
// TODO: throw when using invalid options when migrating to TS
options?: CreateTableQueryOptions
): string;
dropTableQuery(tableName: TableName, options?: DropTableQueryOptions): string;

createSchemaQuery(schemaName: string, options?: CreateSchemaQueryOptions): string;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/dialects/db2/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class Db2QueryGenerator extends Db2QueryGeneratorTypeScript {
const commentText = commentMatch[2].replace(/COMMENT/, '').trim();
commentStr += _.template(commentTemplate, this._templateSettings)({
table: this.quoteTable(tableName),
comment: this.escape(commentText, { replacements: options.replacements }),
comment: this.escape(commentText),
column: this.quoteIdentifier(attr),
});
// remove comment related substring from dataType
Expand Down
11 changes: 3 additions & 8 deletions packages/core/src/dialects/ibmi/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class IBMiQueryGenerator extends IBMiQueryGeneratorTypeScript {
let attributesClause = attrStr.join(', ');
const pkString = primaryKeys.map(pk => this.quoteIdentifier(pk)).join(', ');

if (options.uniqueKeys) {
if (options?.uniqueKeys) {
// only need to sort primary keys once, don't do it in place
const sortedPrimaryKeys = [...primaryKeys];
sortedPrimaryKeys.sort();
Expand Down Expand Up @@ -127,17 +127,12 @@ export class IBMiQueryGenerator extends IBMiQueryGeneratorTypeScript {
}
}

let tableObject;
if (typeof tableName === 'string') {
tableObject = { table: tableName };
} else {
tableObject = tableName;
}
const quotedTable = this.quoteTable(tableName);

return `BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE VALUE '42710'
BEGIN END;
CREATE TABLE ${tableName.schema ? `"${tableObject.schema}".` : ''}"${tableObject.table ? tableObject.table : tableObject.tableName}" (${attributesClause});
CREATE TABLE ${quotedTable} (${attributesClause});
END`;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/dialects/mssql/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class MsSqlQueryGenerator extends MsSqlQueryGeneratorTypeScript {

const pkString = primaryKeys.map(pk => this.quoteIdentifier(pk)).join(', ');

if (options.uniqueKeys) {
if (options?.uniqueKeys) {
_.each(options.uniqueKeys, (columns, indexName) => {
if (typeof indexName !== 'string') {
indexName = generateIndexName(tableName, columns);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class PostgresQueryGenerator extends PostgresQueryGeneratorTypeScript {
}

return acc;
}, []).join(',');
}, []).join(', ');

if (pks.length > 0) {
attributesClause += `, PRIMARY KEY (${pks})`;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/dialects/snowflake/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class SnowflakeQueryGenerator extends SnowflakeQueryGeneratorTypeScript {
'CREATE TABLE IF NOT EXISTS',
table,
`(${attributesClause})`,
options.comment && typeof options.comment === 'string' && `COMMENT ${this.escape(options.comment, options)}`,
options.comment && typeof options.comment === 'string' && `COMMENT ${this.escape(options.comment)}`,
options.charset && `DEFAULT CHARSET=${options.charset}`,
options.collate && `COLLATE ${options.collate}`,
options.rowFormat && `ROW_FORMAT=${options.rowFormat}`,
Expand Down
47 changes: 0 additions & 47 deletions packages/core/test/unit/dialects/db2/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,53 +92,6 @@ if (dialect === 'db2') {
},
],

createTableQuery: [
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }],
expectation: 'CREATE TABLE IF NOT EXISTS "myTable" ("title" VARCHAR(255), "name" VARCHAR(255));',
},
{
arguments: ['myTable', { data: 'BLOB' }],
expectation: 'CREATE TABLE IF NOT EXISTS "myTable" ("data" BLOB);',
},
{
arguments: ['myTable', { data: 'BLOB(16M)' }],
expectation: 'CREATE TABLE IF NOT EXISTS "myTable" ("data" BLOB(16M));',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { engine: 'MyISAM' }],
expectation: 'CREATE TABLE IF NOT EXISTS "myTable" ("title" VARCHAR(255), "name" VARCHAR(255));',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { charset: 'utf8', collate: 'utf8_unicode_ci' }],
expectation: 'CREATE TABLE IF NOT EXISTS "myTable" ("title" VARCHAR(255), "name" VARCHAR(255));',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { charset: 'latin1' }],
expectation: 'CREATE TABLE IF NOT EXISTS "myTable" ("title" VARCHAR(255), "name" VARCHAR(255));',
},
{
arguments: ['myTable', { title: 'ENUM("A", "B", "C")', name: 'VARCHAR(255)' }, { charset: 'latin1' }],
expectation: 'CREATE TABLE IF NOT EXISTS "myTable" ("title" ENUM("A", "B", "C"), "name" VARCHAR(255));',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { rowFormat: 'default' }],
expectation: 'CREATE TABLE IF NOT EXISTS "myTable" ("title" VARCHAR(255), "name" VARCHAR(255));',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)', id: 'INTEGER PRIMARY KEY' }],
expectation: 'CREATE TABLE IF NOT EXISTS "myTable" ("title" VARCHAR(255), "name" VARCHAR(255), "id" INTEGER , PRIMARY KEY ("id"));',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)', otherId: 'INTEGER REFERENCES otherTable (id) ON DELETE CASCADE ON UPDATE NO ACTION' }],
expectation: 'CREATE TABLE IF NOT EXISTS "myTable" ("title" VARCHAR(255), "name" VARCHAR(255), "otherId" INTEGER, FOREIGN KEY ("otherId") REFERENCES otherTable (id) ON DELETE CASCADE ON UPDATE NO ACTION);',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { uniqueKeys: [{ fields: ['title', 'name'] }] }],
expectation: 'CREATE TABLE IF NOT EXISTS "myTable" ("title" VARCHAR(255) NOT NULL, "name" VARCHAR(255) NOT NULL, CONSTRAINT "uniq_myTable_title_name" UNIQUE ("title", "name"));',
},
],

selectQuery: [
{
arguments: ['myTable'],
Expand Down
51 changes: 0 additions & 51 deletions packages/core/test/unit/dialects/mariadb/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,57 +100,6 @@ if (dialect === 'mariadb') {
},
],

createTableQuery: [
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB;',
},
{
arguments: ['myTable', { data: 'BLOB' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`data` BLOB) ENGINE=InnoDB;',
},
{
arguments: ['myTable', { data: 'LONGBLOB' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`data` LONGBLOB) ENGINE=InnoDB;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { engine: 'MyISAM' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=MyISAM;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { charset: 'utf8', collate: 'utf8_unicode_ci' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { charset: 'latin1' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB DEFAULT CHARSET=latin1;',
},
{
arguments: ['myTable', { title: 'ENUM("A", "B", "C")', name: 'VARCHAR(255)' }, { charset: 'latin1' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` ENUM("A", "B", "C"), `name` VARCHAR(255)) ENGINE=InnoDB DEFAULT CHARSET=latin1;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { rowFormat: 'default' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB ROW_FORMAT=default;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)', id: 'INTEGER PRIMARY KEY' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255), `id` INTEGER , PRIMARY KEY (`id`)) ENGINE=InnoDB;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)', otherId: 'INTEGER REFERENCES `otherTable` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255), `otherId` INTEGER, FOREIGN KEY (`otherId`) REFERENCES `otherTable` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION) ENGINE=InnoDB;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { uniqueKeys: [{ fields: ['title', 'name'] }] }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255), UNIQUE `uniq_myTable_title_name` (`title`, `name`)) ENGINE=InnoDB;',
},
{
arguments: ['myTable', { id: 'INTEGER auto_increment PRIMARY KEY' }, { initialAutoIncrement: 1_000_001 }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`id` INTEGER auto_increment , PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1000001;',
},
],

selectQuery: [
{
arguments: ['myTable'],
Expand Down
14 changes: 0 additions & 14 deletions packages/core/test/unit/dialects/mssql/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,6 @@ if (current.dialect.name === 'mssql') {
});
});

it('createTableQuery', function () {
expectsql(this.queryGenerator.createTableQuery('myTable', { int: 'INTEGER' }, {}), {
mssql: `IF OBJECT_ID(N'[myTable]', 'U') IS NULL CREATE TABLE [myTable] ([int] INTEGER);`,
});
});

it('createTableQuery with comments', function () {
expectsql(this.queryGenerator.createTableQuery('myTable', { int: 'INTEGER COMMENT Foo Bar', varchar: 'VARCHAR(50) UNIQUE COMMENT Bar Foo' }, {}), { mssql: `IF OBJECT_ID(N'[myTable]', 'U') IS NULL CREATE TABLE [myTable] ([int] INTEGER, [varchar] VARCHAR(50) UNIQUE); EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'Foo Bar', @level0type = N'Schema', @level0name = 'dbo', @level1type = N'Table', @level1name = [myTable], @level2type = N'Column', @level2name = [int]; EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'Bar Foo', @level0type = N'Schema', @level0name = 'dbo', @level1type = N'Table', @level1name = [myTable], @level2type = N'Column', @level2name = [varchar];` });
});

it('createTableQuery with comments and table object', function () {
expectsql(this.queryGenerator.createTableQuery({ tableName: 'myTable' }, { int: 'INTEGER COMMENT Foo Bar', varchar: 'VARCHAR(50) UNIQUE COMMENT Bar Foo' }, {}), { mssql: `IF OBJECT_ID(N'[myTable]', 'U') IS NULL CREATE TABLE [myTable] ([int] INTEGER, [varchar] VARCHAR(50) UNIQUE); EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'Foo Bar', @level0type = N'Schema', @level0name = 'dbo', @level1type = N'Table', @level1name = [myTable], @level2type = N'Column', @level2name = [int]; EXEC sp_addextendedproperty @name = N'MS_Description', @value = N'Bar Foo', @level0type = N'Schema', @level0name = 'dbo', @level1type = N'Table', @level1name = [myTable], @level2type = N'Column', @level2name = [varchar];` });
});

it('getDefaultConstraintQuery', function () {
expectsql(this.queryGenerator.getDefaultConstraintQuery({ tableName: 'myTable', schema: 'mySchema' }, 'myColumn'), {
mssql: `SELECT name FROM sys.default_constraints WHERE PARENT_OBJECT_ID = OBJECT_ID('[mySchema].[myTable]', 'U') AND PARENT_COLUMN_ID = (SELECT column_id FROM sys.columns WHERE NAME = ('myColumn') AND object_id = OBJECT_ID('[mySchema].[myTable]', 'U'));`,
Expand Down
51 changes: 0 additions & 51 deletions packages/core/test/unit/dialects/mysql/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,57 +100,6 @@ if (dialect === 'mysql') {
},
],

createTableQuery: [
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB;',
},
{
arguments: ['myTable', { data: 'BLOB' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`data` BLOB) ENGINE=InnoDB;',
},
{
arguments: ['myTable', { data: 'LONGBLOB' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`data` LONGBLOB) ENGINE=InnoDB;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { engine: 'MyISAM' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=MyISAM;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { charset: 'utf8', collate: 'utf8_unicode_ci' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { charset: 'latin1' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB DEFAULT CHARSET=latin1;',
},
{
arguments: ['myTable', { title: 'ENUM("A", "B", "C")', name: 'VARCHAR(255)' }, { charset: 'latin1' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` ENUM("A", "B", "C"), `name` VARCHAR(255)) ENGINE=InnoDB DEFAULT CHARSET=latin1;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { rowFormat: 'default' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB ROW_FORMAT=default;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)', id: 'INTEGER PRIMARY KEY' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255), `id` INTEGER , PRIMARY KEY (`id`)) ENGINE=InnoDB;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)', otherId: 'INTEGER REFERENCES `otherTable` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255), `otherId` INTEGER, FOREIGN KEY (`otherId`) REFERENCES `otherTable` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION) ENGINE=InnoDB;',
},
{
arguments: ['myTable', { title: 'VARCHAR(255)', name: 'VARCHAR(255)' }, { uniqueKeys: [{ fields: ['title', 'name'] }] }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255), UNIQUE `uniq_myTable_title_name` (`title`, `name`)) ENGINE=InnoDB;',
},
{
arguments: ['myTable', { id: 'INTEGER auto_increment PRIMARY KEY' }, { initialAutoIncrement: 1_000_001 }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`id` INTEGER auto_increment , PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1000001;',
},
],

selectQuery: [
{
arguments: ['myTable'],
Expand Down

0 comments on commit b5db02d

Please sign in to comment.