Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 974d2d4

Browse files
authored
fix: Silent failure in createDatabase and dropDatabase with Postgres (typeorm#7590)
* feat: implement database creation and dropping on postgres' query runner * implement the createDatabase, dropDatabase and hasDatabase methods on postgres' query runner Closes typeorm#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
1 parent 0b103dd commit 974d2d4

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/driver/postgres/PostgresQueryRunner.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
271271
* Checks if database with the given name exist.
272272
*/
273273
async hasDatabase(database: string): Promise<boolean> {
274-
return Promise.resolve(false);
274+
const result = await this.query(`SELECT * FROM pg_database WHERE datname='${database}';`);
275+
return result.length ? true : false;
275276
}
276277

277278
/**
@@ -320,18 +321,29 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
320321

321322
/**
322323
* Creates a new database.
323-
* Postgres does not supports database creation inside a transaction block.
324+
* Note: Postgres does not support database creation inside a transaction block.
324325
*/
325326
async createDatabase(database: string, ifNotExist?: boolean): Promise<void> {
326-
await Promise.resolve();
327+
if (ifNotExist) {
328+
const databaseAlreadyExists = await this.hasDatabase(database);
329+
330+
if (databaseAlreadyExists)
331+
return Promise.resolve();
332+
}
333+
334+
const up = `CREATE DATABASE "${database}"`;
335+
const down = `DROP DATABASE "${database}"`;
336+
await this.executeQueries(new Query(up), new Query(down));
327337
}
328338

329339
/**
330340
* Drops database.
331-
* Postgres does not supports database drop inside a transaction block.
341+
* Note: Postgres does not support database dropping inside a transaction block.
332342
*/
333343
async dropDatabase(database: string, ifExist?: boolean): Promise<void> {
334-
return Promise.resolve();
344+
const up = ifExist ? `DROP DATABASE IF EXISTS "${database}"` : `DROP DATABASE "${database}"`;
345+
const down = `CREATE DATABASE "${database}"`;
346+
await this.executeQueries(new Query(up), new Query(down));
335347
}
336348

337349
/**

test/functional/query-runner/create-and-drop-database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe("query runner > create and drop database", () => {
88
before(async () => {
99
connections = await createTestingConnections({
1010
entities: [__dirname + "/entity/*{.js,.ts}"],
11-
enabledDrivers: ["mysql", "mssql", "cockroachdb"],
11+
enabledDrivers: ["mysql", "mssql", "cockroachdb", "postgres"],
1212
dropSchema: true,
1313
});
1414
});

0 commit comments

Comments
 (0)