From 5aeb0024b90f4d99b3867c19f2b3c3e45e535814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Tue, 6 Oct 2020 19:40:34 +0200 Subject: [PATCH] feat(catalog-backend): temporary named test db support to aid in debugging Fully aware that this isn't pretty. But since I am in the process of debugging migrations and catalog stuff and want to easily run manually against several databases while waiting for proper testcontainers and e2e tests, this at least lets me gain some confidence short term. --- .../src/database/CommonDatabase.test.ts | 40 ++++++++++--------- .../src/database/DatabaseManager.ts | 28 ++++++++++++- 2 files changed, 47 insertions(+), 21 deletions(-) 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); }