Skip to content

Commit

Permalink
fix: Silent failure in createDatabase and dropDatabase with Postgres (#…
Browse files Browse the repository at this point in the history
…7590)

* feat: implement database creation and dropping on postgres' query runner

* implement the createDatabase, dropDatabase and hasDatabase methods on postgres' query runner

Closes #6867

* test: add postgres driver on database creation and dropping tests

* Set ifNotExist=false when testing with postgres' driver

* fix: Modify ifNotExist parameter handling on postgres' database creation

* Add empty promise return when ifNotExist is true and the specified database already exists on postgres' query runner
* Set ifNotExist as true on the database creation and dropping tests
  • Loading branch information
PauloRSF committed Apr 24, 2021
1 parent 0b103dd commit 974d2d4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
* Checks if database with the given name exist.
*/
async hasDatabase(database: string): Promise<boolean> {
return Promise.resolve(false);
const result = await this.query(`SELECT * FROM pg_database WHERE datname='${database}';`);
return result.length ? true : false;
}

/**
Expand Down Expand Up @@ -320,18 +321,29 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner

/**
* Creates a new database.
* Postgres does not supports database creation inside a transaction block.
* Note: Postgres does not support database creation inside a transaction block.
*/
async createDatabase(database: string, ifNotExist?: boolean): Promise<void> {
await Promise.resolve();
if (ifNotExist) {
const databaseAlreadyExists = await this.hasDatabase(database);

if (databaseAlreadyExists)
return Promise.resolve();
}

const up = `CREATE DATABASE "${database}"`;
const down = `DROP DATABASE "${database}"`;
await this.executeQueries(new Query(up), new Query(down));
}

/**
* Drops database.
* Postgres does not supports database drop inside a transaction block.
* Note: Postgres does not support database dropping inside a transaction block.
*/
async dropDatabase(database: string, ifExist?: boolean): Promise<void> {
return Promise.resolve();
const up = ifExist ? `DROP DATABASE IF EXISTS "${database}"` : `DROP DATABASE "${database}"`;
const down = `CREATE DATABASE "${database}"`;
await this.executeQueries(new Query(up), new Query(down));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/functional/query-runner/create-and-drop-database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("query runner > create and drop database", () => {
before(async () => {
connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["mysql", "mssql", "cockroachdb"],
enabledDrivers: ["mysql", "mssql", "cockroachdb", "postgres"],
dropSchema: true,
});
});
Expand Down

0 comments on commit 974d2d4

Please sign in to comment.