1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-03-18 00:19:49 +01:00

chore: cleanup old test dbs ()

This cleans up old dbs before running a new test
This commit is contained in:
Gastón Fournier 2025-03-14 10:44:58 +01:00 committed by GitHub
parent f9c152995c
commit 9f0155e0cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 7 deletions
scripts
src/test/e2e/helpers

View File

@ -1,21 +1,26 @@
import { Client, type ClientConfig } from 'pg';
import { migrateDb } from '../src/migrator';
import { getDbConfig } from '../src/test/e2e/helpers/database-config';
import { testDbPrefix } from '../src/test/e2e/helpers/database-init';
let initializationPromise: Promise<void> | null = null;
const initializeTemplateDb = (db: ClientConfig): Promise<void> => {
if (!initializationPromise) {
initializationPromise = (async () => {
const testDBTemplateName = process.env.TEST_DB_TEMPLATE_NAME;
const client = new Client(db);
await client.connect();
console.log(`Initializing template database ${testDBTemplateName}`);
// code to clean up, but only on next run, we could do it at tear down... but is it really needed?
// const result = await client.query(`select datname from pg_database where datname like 'unleashtestdb_%';`)
// result.rows.forEach(async (row: any) => {
// console.log(`Dropping test database ${row.datname}`);
// await client.query(`DROP DATABASE ${row.datname}`);
// });
// first clean up databases from previous runs
const result = await client.query(
`select datname from pg_database where datname like '${testDbPrefix}%';`,
);
result.rows.forEach(async (row: any) => {
console.log(`Dropping test database ${row.datname}`);
await client.query(`DROP DATABASE ${row.datname}`);
});
await client.query(`DROP DATABASE IF EXISTS ${testDBTemplateName}`);
await client.query(`CREATE DATABASE ${testDBTemplateName}`);
await client.end();

View File

@ -24,6 +24,8 @@ import { v4 as uuidv4 } from 'uuid';
// because of db-migrate bug (https://github.com/Unleash/unleash/issues/171)
process.setMaxListeners(0);
export const testDbPrefix = 'unleashtestdb_';
async function getDefaultEnvRolePermissions(knex) {
return knex.table('role_permission').whereIn('environment', ['default']);
}
@ -108,7 +110,7 @@ export default async function init(
getLogger: LogProvider = noLoggerProvider,
configOverride: Partial<IUnleashOptions & DBTestOptions> = {},
): Promise<ITestDb> {
const testDbName = `unleashtestdb_${uuidv4().replace(/-/g, '')}`;
const testDbName = `${testDbPrefix}${uuidv4().replace(/-/g, '')}`;
const useDbTemplate =
(configOverride.dbInitMethod ?? 'template') === 'template';
const testDBTemplateName = process.env.TEST_DB_TEMPLATE_NAME;