Merge pull request #2776 from spotify/freben/test-db
feat(catalog-backend): temporary named test db support to aid in debugging
This commit is contained in:
@@ -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,
|
||||
},
|
||||
]),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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<Database> {
|
||||
const knex = Knex({
|
||||
const config: Knex.Config<any> = {
|
||||
/*
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user