diff --git a/plugins/catalog-backend/src/database/CommonDatabase.test.ts b/plugins/catalog-backend/src/database/CommonDatabase.test.ts index 8a42b39cc5..2bacec4026 100644 --- a/plugins/catalog-backend/src/database/CommonDatabase.test.ts +++ b/plugins/catalog-backend/src/database/CommonDatabase.test.ts @@ -124,7 +124,7 @@ describe('CommonDatabase', () => { { ...output, status: DatabaseLocationUpdateLogStatus.FAIL, - timestamp: expect.any(String), + timestamp: expect.anything(), }, ]), ); @@ -191,24 +191,26 @@ describe('CommonDatabase', () => { const result = await db.locationHistory( 'dd12620d-0436-422f-93bd-929aa0788123', ); - expect(result).toEqual([ - { - created_at: expect.anything(), - entity_name: null, - id: expect.anything(), - location_id: 'dd12620d-0436-422f-93bd-929aa0788123', - message: null, - status: DatabaseLocationUpdateLogStatus.SUCCESS, - }, - { - created_at: expect.anything(), - entity_name: null, - id: expect.anything(), - location_id: 'dd12620d-0436-422f-93bd-929aa0788123', - message: 'Something went wrong', - status: DatabaseLocationUpdateLogStatus.FAIL, - }, - ]); + expect(result).toEqual( + expect.arrayContaining([ + { + created_at: expect.anything(), + entity_name: null, + id: expect.anything(), + location_id: 'dd12620d-0436-422f-93bd-929aa0788123', + message: null, + status: DatabaseLocationUpdateLogStatus.SUCCESS, + }, + { + created_at: expect.anything(), + entity_name: null, + id: expect.anything(), + location_id: 'dd12620d-0436-422f-93bd-929aa0788123', + message: 'Something went wrong', + status: DatabaseLocationUpdateLogStatus.FAIL, + }, + ]), + ); }); }); diff --git a/plugins/catalog-backend/src/database/DatabaseManager.ts b/plugins/catalog-backend/src/database/DatabaseManager.ts index afff254097..eb307c9478 100644 --- a/plugins/catalog-backend/src/database/DatabaseManager.ts +++ b/plugins/catalog-backend/src/database/DatabaseManager.ts @@ -19,6 +19,7 @@ import Knex from 'knex'; import { Logger } from 'winston'; import { CommonDatabase } from './CommonDatabase'; import { Database } from './types'; +import { v4 as uuidv4 } from 'uuid'; const migrationsDir = resolvePackagePath( '@backstage/plugin-catalog-backend', @@ -60,17 +61,40 @@ export class DatabaseManager { } public static async createTestDatabase(): Promise { - const knex = Knex({ + const config: Knex.Config = { + /* + client: 'pg', + connection: { + host: 'localhost', + user: 'postgres', + password: 'postgres', + }, + */ client: 'sqlite3', connection: ':memory:', useNullAsDefault: true, - }); + }; + + let knex = Knex(config); + if (typeof config.connection !== 'string') { + const tempDbName = `d${uuidv4().replace(/-/g, '')}`; + await knex.raw(`CREATE DATABASE ${tempDbName};`); + knex = Knex({ + ...config, + connection: { + ...config.connection, + database: tempDbName, + }, + }); + } + knex.client.pool.on('createSuccess', (_eventId: any, resource: any) => { resource.run('PRAGMA foreign_keys = ON', () => {}); }); await knex.migrate.latest({ directory: migrationsDir, }); + const { logger } = defaultOptions; return new CommonDatabase(knex, logger); }