Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,20 @@ SchemaCompiler_PG.prototype.createSchemaIfNotExists = function(schemaName) {
);
};

SchemaCompiler_PG.prototype.dropSchema = function(schemaName) {
this.pushQuery(`drop schema ${this.formatter.wrap(schemaName)}`);
SchemaCompiler_PG.prototype.dropSchema = function(schemaName, cascade = false) {
this.pushQuery(
`drop schema ${this.formatter.wrap(schemaName)}${cascade && ' CASCADE'}`
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this will work correct for cascade = false?
can you add unit test on this?

);
};

SchemaCompiler_PG.prototype.dropSchemaIfExists = function(schemaName) {
this.pushQuery(`drop schema if exists ${this.formatter.wrap(schemaName)}`);
SchemaCompiler_PG.prototype.dropSchemaIfExists = function(
schemaName,
cascade = false
) {
this.pushQuery(
`drop schema if exists ${this.formatter.wrap(schemaName)}${cascade &&
Copy link
Member

Choose a reason for hiding this comment

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

This would evaluate to drop schema if exists <tablename>false when cascade is false.

Please change to a ternary-operation.

drop schema if exists ${this.formatter.wrap(schemaName)}${cascade ? ' CASCADE' : ''}

' CASCADE'}`
);
};

SchemaCompiler_PG.prototype.dropExtension = function(extensionName) {
Expand Down
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1498,8 +1498,8 @@ 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