|
1 | 1 | import { Client, type ClientConfig } from 'pg';
|
2 | 2 | import { migrateDb } from '../src/migrator';
|
3 | 3 | import { getDbConfig } from '../src/test/e2e/helpers/database-config';
|
| 4 | +import { testDbPrefix } from '../src/test/e2e/helpers/database-init'; |
4 | 5 |
|
5 | 6 | let initializationPromise: Promise<void> | null = null;
|
| 7 | + |
6 | 8 | const initializeTemplateDb = (db: ClientConfig): Promise<void> => {
|
7 | 9 | if (!initializationPromise) {
|
8 | 10 | initializationPromise = (async () => {
|
9 | 11 | const testDBTemplateName = process.env.TEST_DB_TEMPLATE_NAME;
|
| 12 | + |
10 | 13 | const client = new Client(db);
|
11 | 14 | await client.connect();
|
12 | 15 | console.log(`Initializing template database ${testDBTemplateName}`);
|
13 |
| - // code to clean up, but only on next run, we could do it at tear down... but is it really needed? |
14 |
| - // const result = await client.query(`select datname from pg_database where datname like 'unleashtestdb_%';`) |
15 |
| - // result.rows.forEach(async (row: any) => { |
16 |
| - // console.log(`Dropping test database ${row.datname}`); |
17 |
| - // await client.query(`DROP DATABASE ${row.datname}`); |
18 |
| - // }); |
| 16 | + // first clean up databases from previous runs |
| 17 | + const result = await client.query( |
| 18 | + `select datname from pg_database where datname like '${testDbPrefix}%';`, |
| 19 | + ); |
| 20 | + result.rows.forEach(async (row: any) => { |
| 21 | + console.log(`Dropping test database ${row.datname}`); |
| 22 | + await client.query(`DROP DATABASE ${row.datname}`); |
| 23 | + }); |
19 | 24 | await client.query(`DROP DATABASE IF EXISTS ${testDBTemplateName}`);
|
20 | 25 | await client.query(`CREATE DATABASE ${testDBTemplateName}`);
|
21 | 26 | await client.end();
|
|
0 commit comments