mirror of
https://github.com/Unleash/unleash.git
synced 2025-06-09 01:17:06 +02:00
chore: cleanup old test dbs (#9539)
This cleans up old dbs before running a new test
This commit is contained in:
parent
f9c152995c
commit
9f0155e0cf
@ -1,21 +1,26 @@
|
|||||||
import { Client, type ClientConfig } from 'pg';
|
import { Client, type ClientConfig } from 'pg';
|
||||||
import { migrateDb } from '../src/migrator';
|
import { migrateDb } from '../src/migrator';
|
||||||
import { getDbConfig } from '../src/test/e2e/helpers/database-config';
|
import { getDbConfig } from '../src/test/e2e/helpers/database-config';
|
||||||
|
import { testDbPrefix } from '../src/test/e2e/helpers/database-init';
|
||||||
|
|
||||||
let initializationPromise: Promise<void> | null = null;
|
let initializationPromise: Promise<void> | null = null;
|
||||||
|
|
||||||
const initializeTemplateDb = (db: ClientConfig): Promise<void> => {
|
const initializeTemplateDb = (db: ClientConfig): Promise<void> => {
|
||||||
if (!initializationPromise) {
|
if (!initializationPromise) {
|
||||||
initializationPromise = (async () => {
|
initializationPromise = (async () => {
|
||||||
const testDBTemplateName = process.env.TEST_DB_TEMPLATE_NAME;
|
const testDBTemplateName = process.env.TEST_DB_TEMPLATE_NAME;
|
||||||
|
|
||||||
const client = new Client(db);
|
const client = new Client(db);
|
||||||
await client.connect();
|
await client.connect();
|
||||||
console.log(`Initializing template database ${testDBTemplateName}`);
|
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?
|
// first clean up databases from previous runs
|
||||||
// const result = await client.query(`select datname from pg_database where datname like 'unleashtestdb_%';`)
|
const result = await client.query(
|
||||||
// result.rows.forEach(async (row: any) => {
|
`select datname from pg_database where datname like '${testDbPrefix}%';`,
|
||||||
// console.log(`Dropping test database ${row.datname}`);
|
);
|
||||||
// await client.query(`DROP DATABASE ${row.datname}`);
|
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(`DROP DATABASE IF EXISTS ${testDBTemplateName}`);
|
||||||
await client.query(`CREATE DATABASE ${testDBTemplateName}`);
|
await client.query(`CREATE DATABASE ${testDBTemplateName}`);
|
||||||
await client.end();
|
await client.end();
|
||||||
|
@ -24,6 +24,8 @@ import { v4 as uuidv4 } from 'uuid';
|
|||||||
// because of db-migrate bug (https://github.com/Unleash/unleash/issues/171)
|
// because of db-migrate bug (https://github.com/Unleash/unleash/issues/171)
|
||||||
process.setMaxListeners(0);
|
process.setMaxListeners(0);
|
||||||
|
|
||||||
|
export const testDbPrefix = 'unleashtestdb_';
|
||||||
|
|
||||||
async function getDefaultEnvRolePermissions(knex) {
|
async function getDefaultEnvRolePermissions(knex) {
|
||||||
return knex.table('role_permission').whereIn('environment', ['default']);
|
return knex.table('role_permission').whereIn('environment', ['default']);
|
||||||
}
|
}
|
||||||
@ -108,7 +110,7 @@ export default async function init(
|
|||||||
getLogger: LogProvider = noLoggerProvider,
|
getLogger: LogProvider = noLoggerProvider,
|
||||||
configOverride: Partial<IUnleashOptions & DBTestOptions> = {},
|
configOverride: Partial<IUnleashOptions & DBTestOptions> = {},
|
||||||
): Promise<ITestDb> {
|
): Promise<ITestDb> {
|
||||||
const testDbName = `unleashtestdb_${uuidv4().replace(/-/g, '')}`;
|
const testDbName = `${testDbPrefix}${uuidv4().replace(/-/g, '')}`;
|
||||||
const useDbTemplate =
|
const useDbTemplate =
|
||||||
(configOverride.dbInitMethod ?? 'template') === 'template';
|
(configOverride.dbInitMethod ?? 'template') === 'template';
|
||||||
const testDBTemplateName = process.env.TEST_DB_TEMPLATE_NAME;
|
const testDBTemplateName = process.env.TEST_DB_TEMPLATE_NAME;
|
||||||
|
Loading…
Reference in New Issue
Block a user