Skip to content

Commit

Permalink
Adjust generateDdlCommands return type (#4326)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrum committed Mar 1, 2021
1 parent d807832 commit 29b8a36
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/dialects/sqlite3/schema/sqlite-compiler.js
Expand Up @@ -69,7 +69,7 @@ class SchemaCompiler_SQLite3 extends SchemaCompiler {
}
}

return result;
return { pre: [], sql: result, post: [] };
}
}

Expand Down
10 changes: 7 additions & 3 deletions lib/schema/compiler.js
Expand Up @@ -67,9 +67,13 @@ class SchemaCompiler {

async generateDdlCommands() {
const generatedCommands = this.toSQL();
return Array.isArray(generatedCommands)
? generatedCommands
: [generatedCommands];
return {
pre: [],
sql: Array.isArray(generatedCommands)
? generatedCommands
: [generatedCommands],
post: [],
};
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/integration2/schema/alter.spec.js
Expand Up @@ -130,7 +130,7 @@ describe('Schema', () => {

const queries = await builder.generateDdlCommands();

expect(queries).to.deep.equal([
expect(queries.sql).to.deep.equal([
"CREATE TABLE `_knex_temp_alter111` (`column_integer` varchar(255), `column_string` varchar(255), `column_datetime` datetime, `column_defaultTo` integer DEFAULT '0', `column_notNullable` varchar(255) NOT NULL, `column_defaultToAndNotNullable` datetime NOT NULL DEFAULT '0')",
'INSERT INTO _knex_temp_alter111 SELECT * FROM alter_table;',
'DROP TABLE "alter_table"',
Expand Down
8 changes: 4 additions & 4 deletions test/integration2/schema/foreign-keys.spec.js
Expand Up @@ -65,7 +65,7 @@ describe('Schema', () => {
const queries = await builder.generateDdlCommands();

if (isSQLite(knex)) {
expect(queries).to.eql([
expect(queries.sql).to.eql([
'CREATE TABLE `_knex_temp_alter111` (`id` integer not null primary key autoincrement, `fkey_two` integer not null, `fkey_three` integer not null, CONSTRAINT fk_fkey_threeee FOREIGN KEY (`fkey_three`) REFERENCES `foreign_keys_table_three` (`id`))',
'INSERT INTO _knex_temp_alter111 SELECT * FROM foreign_keys_table_one;',
'DROP TABLE "foreign_keys_table_one"',
Expand All @@ -74,7 +74,7 @@ describe('Schema', () => {
}

if (isPostgreSQL(knex)) {
expect(queries).to.eql([
expect(queries.sql).to.eql([
{
bindings: [],
sql:
Expand Down Expand Up @@ -102,7 +102,7 @@ describe('Schema', () => {

const queries = await builder.generateDdlCommands();

expect(queries).to.eql([
expect(queries.sql).to.eql([
'CREATE TABLE `_knex_temp_alter111` (`id` integer not null primary key autoincrement, `fkey_two` integer not null, `fkey_three` integer not null, CONSTRAINT fk_fkey_threeee FOREIGN KEY (`fkey_three`) REFERENCES `foreign_keys_table_three` (`id`) ON DELETE CASCADE)',
'INSERT INTO _knex_temp_alter111 SELECT * FROM foreign_keys_table_one;',
'DROP TABLE "foreign_keys_table_one"',
Expand All @@ -128,7 +128,7 @@ describe('Schema', () => {

const queries = await builder.generateDdlCommands();

expect(queries).to.eql([
expect(queries.sql).to.eql([
'CREATE TABLE `_knex_temp_alter111` (`id` integer not null primary key autoincrement, `fkey_two` integer not null, `fkey_three` integer not null, CONSTRAINT fk_fkey_threeee FOREIGN KEY (`fkey_three`) REFERENCES `foreign_keys_table_three` (`id`) ON UPDATE CASCADE)',
'INSERT INTO _knex_temp_alter111 SELECT * FROM foreign_keys_table_one;',
'DROP TABLE "foreign_keys_table_one"',
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Expand Up @@ -1671,7 +1671,7 @@ export declare namespace Knex {
readonly [Symbol.toStringTag]: string;
}
interface ChainableInterface<T = any> extends Pick<Promise<T>, keyof Promise<T> & ExposedPromiseKeys>, StringTagSupport {
generateDdlCommands(): Promise<string[]>;
generateDdlCommands(): Promise<{ pre: string[], sql: string[], post: string[] }>;
toQuery(): string;
options(options: Readonly<{ [key: string]: any }>): this;
connection(connection: any): this;
Expand Down

0 comments on commit 29b8a36

Please sign in to comment.