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

Add support for 'CASCADE' in PostgreSQL 'DROP SCHEMA' queries #4713

Merged
merged 2 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lib/dialects/postgres/schema/pg-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,20 @@ class SchemaCompiler_PG extends SchemaCompiler {
);
}

dropSchema(schemaName) {
this.pushQuery(`drop schema ${this.formatter.wrap(schemaName)}`);
dropSchema(schemaName, cascade = false) {
this.pushQuery(
`drop schema ${this.formatter.wrap(schemaName)}${
cascade ? ' cascade' : ''
}`
);
}

dropSchemaIfExists(schemaName) {
this.pushQuery(`drop schema if exists ${this.formatter.wrap(schemaName)}`);
dropSchemaIfExists(schemaName, cascade = false) {
this.pushQuery(
`drop schema if exists ${this.formatter.wrap(schemaName)}${
cascade ? ' cascade' : ''
}`
);
}

dropExtension(extensionName) {
Expand Down
52 changes: 52 additions & 0 deletions test/integration2/schema/misc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,58 @@ describe('Schema (misc)', () => {
});
});

describe('dropSchema', () => {
before(async () => {
await knex.schema
.createSchema('test')
.createSchema('test2')
.createSchema('test3')
.createSchema('test4')
.createSchema('schema_drop_1')
.createSchema('schema_drop_2')
.createSchema('schema_drop_cascade_1')
.createSchema('schema_drop_cascade_2')
.withSchema('schema_drop_cascade_1')
.createTable('table_cascade_1', () => {})
.withSchema('schema_drop_cascade_2')
.createTable('table_cascade_2', () => {});
});

it('has a dropSchema/dropSchemaIfExists method', function () {
if (!isPgBased(knex)) {
return this.skip();
}

this.timeout(process.env.KNEX_TEST_TIMEOUT || 30000);
const dbDropSchema = ['pg', 'cockroachdb'];
return Promise.all([
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any particular reason why you are doing this inside Promise.all and not by using async/await?

Copy link
Collaborator Author

@OlivierCavadenti OlivierCavadenti Oct 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi, I discover how tests are made in knex and I mostly based this test on the test below (dropTable) that seems concise way to do. But it's seems that await or the promise callback syntax are more used. I will fix that. Thanks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Yes, generally async/await is preferred.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rework tests, finally I also add checks if schema exists

knex.schema.dropSchema('test').testSql((tester) => {
tester(dbDropSchema, ['drop schema "test"']);
}),
knex.schema.dropSchemaIfExists('test2').testSql((tester) => {
tester(dbDropSchema, ['drop schema if exists "test2"']);
}),
knex.schema.dropSchema('test3', true).testSql((tester) => {
tester(dbDropSchema, ['drop schema "test3" cascade']);
}),
knex.schema.dropSchemaIfExists('test4', true).testSql((tester) => {
tester(dbDropSchema, ['drop schema if exists "test4" cascade']);
}),
knex.schema
.dropSchema('schema_drop_1')
.dropSchemaIfExists('schema_drop_2')
.dropSchema('schema_drop_cascade_1', true)
.dropSchemaIfExists('schema_drop_cascade_2', true),
knex.schema.hasTable('table_cascade_1').then((resp) => {
expect(resp).to.equal(false);
}),
knex.schema.hasTable('table_cascade_2').then((resp) => {
expect(resp).to.equal(false);
}),
]);
});
});

describe('dropTable', () => {
it('has a dropTableIfExists method', function () {
this.timeout(process.env.KNEX_TEST_TIMEOUT || 30000);
Expand Down
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1851,8 +1851,8 @@ export declare namespace Knex {
callback: (tableBuilder: AlterTableBuilder) => any
): Promise<void>;
dropTableIfExists(tableName: string): SchemaBuilder;
dropSchema(schemaName: string): SchemaBuilder;
dropSchemaIfExists(schemaName: string): SchemaBuilder;
dropSchema(schemaName: string, cascade?: boolean): SchemaBuilder;
dropSchemaIfExists(schemaName: string, cascade?: boolean): SchemaBuilder;
raw(statement: string): SchemaBuilder;
withSchema(schemaName: string): SchemaBuilder;
queryContext(context: any): SchemaBuilder;
Expand Down