mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01: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 { 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(); | ||||
|  | ||||
| @ -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; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user