From 590605bf3d2b8042ec6839d93942ce58756bc0c1 Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 7 Oct 2021 18:20:55 +0200 Subject: [PATCH] chore: removed old DatabaseManager from Catalog Next Signed-off-by: blam Co-authored-by: Johan Haals Co-authored-by: Patrik Oldsberg --- .../src/next/DefaultLocationStore.test.ts | 4 +- .../src/next/DefaultRefreshService.test.ts | 4 +- .../src/next/NextCatalogBuilder.ts | 14 +-- .../src/next/NextEntitiesCatalog.test.ts | 4 +- .../src/next/database/DatabaseManager.ts | 98 ------------------- .../DefaultProcessingDatabase.test.ts | 4 +- .../src/next/database/migrations.ts | 29 ++++++ .../src/next/stitching/Stitcher.test.ts | 4 +- 8 files changed, 42 insertions(+), 119 deletions(-) delete mode 100644 plugins/catalog-backend/src/next/database/DatabaseManager.ts create mode 100644 plugins/catalog-backend/src/next/database/migrations.ts diff --git a/plugins/catalog-backend/src/next/DefaultLocationStore.test.ts b/plugins/catalog-backend/src/next/DefaultLocationStore.test.ts index 82b08f2e70..f3cefeb14f 100644 --- a/plugins/catalog-backend/src/next/DefaultLocationStore.test.ts +++ b/plugins/catalog-backend/src/next/DefaultLocationStore.test.ts @@ -15,7 +15,7 @@ */ import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils'; import { v4 as uuid } from 'uuid'; -import { DatabaseManager } from './database/DatabaseManager'; +import { applyDatabaseMigrations } from './database/migrations'; import { DefaultLocationStore } from './DefaultLocationStore'; describe('DefaultLocationStore', () => { @@ -25,7 +25,7 @@ describe('DefaultLocationStore', () => { async function createLocationStore(databaseId: TestDatabaseId) { const knex = await databases.init(databaseId); - await DatabaseManager.createDatabase(knex); + await applyDatabaseMigrations(knex); const connection = { applyMutation: jest.fn() }; const store = new DefaultLocationStore(knex); await store.connect(connection); diff --git a/plugins/catalog-backend/src/next/DefaultRefreshService.test.ts b/plugins/catalog-backend/src/next/DefaultRefreshService.test.ts index 79ced3ea43..7b6a181375 100644 --- a/plugins/catalog-backend/src/next/DefaultRefreshService.test.ts +++ b/plugins/catalog-backend/src/next/DefaultRefreshService.test.ts @@ -19,7 +19,7 @@ import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils'; import { createHash } from 'crypto'; import { Knex } from 'knex'; import { Logger } from 'winston'; -import { DatabaseManager } from './database/DatabaseManager'; +import { applyDatabaseMigrations } from './database/migrations'; import { DefaultProcessingDatabase } from './database/DefaultProcessingDatabase'; import { DbRefreshStateReferencesRow, @@ -44,7 +44,7 @@ describe('Refresh integration', () => { logger: Logger = defaultLogger, ) { const knex = await databases.init(databaseId); - await DatabaseManager.createDatabase(knex); + await applyDatabaseMigrations(knex); return { knex, db: new DefaultProcessingDatabase({ diff --git a/plugins/catalog-backend/src/next/NextCatalogBuilder.ts b/plugins/catalog-backend/src/next/NextCatalogBuilder.ts index 700c5a5163..8ca93d4bd7 100644 --- a/plugins/catalog-backend/src/next/NextCatalogBuilder.ts +++ b/plugins/catalog-backend/src/next/NextCatalogBuilder.ts @@ -14,11 +14,7 @@ * limitations under the License. */ -import { - PluginDatabaseManager, - resolvePackagePath, - UrlReader, -} from '@backstage/backend-common'; +import { PluginDatabaseManager, UrlReader } from '@backstage/backend-common'; import { DefaultNamespaceEntityPolicy, EntityPolicies, @@ -70,6 +66,7 @@ import { } from '../next/types'; import { ConfigLocationEntityProvider } from './ConfigLocationEntityProvider'; import { DefaultProcessingDatabase } from './database/DefaultProcessingDatabase'; +import { applyDatabaseMigrations } from './database/migrations'; import { DefaultCatalogProcessingEngine } from './DefaultCatalogProcessingEngine'; import { DefaultLocationService } from './DefaultLocationService'; import { DefaultLocationStore } from './DefaultLocationStore'; @@ -304,12 +301,7 @@ export class NextCatalogBuilder { const parser = this.parser || defaultEntityDataParser; const dbClient = await database.getClient(); - await dbClient.migrate.latest({ - directory: resolvePackagePath( - '@backstage/plugin-catalog-backend', - 'migrations', - ), - }); + await applyDatabaseMigrations(dbClient); const db = new CommonDatabase(dbClient, logger); diff --git a/plugins/catalog-backend/src/next/NextEntitiesCatalog.test.ts b/plugins/catalog-backend/src/next/NextEntitiesCatalog.test.ts index c173c926e8..47e92445e9 100644 --- a/plugins/catalog-backend/src/next/NextEntitiesCatalog.test.ts +++ b/plugins/catalog-backend/src/next/NextEntitiesCatalog.test.ts @@ -18,7 +18,7 @@ import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils'; import { Entity, stringifyEntityRef } from '@backstage/catalog-model'; import { Knex } from 'knex'; import { v4 as uuid } from 'uuid'; -import { DatabaseManager } from './database/DatabaseManager'; +import { applyDatabaseMigrations } from './database/migrations'; import { DbFinalEntitiesRow, DbRefreshStateReferencesRow, @@ -33,7 +33,7 @@ describe('NextEntitiesCatalog', () => { async function createDatabase(databaseId: TestDatabaseId) { const knex = await databases.init(databaseId); - await DatabaseManager.createDatabase(knex); + await applyDatabaseMigrations(knex); return { knex }; } diff --git a/plugins/catalog-backend/src/next/database/DatabaseManager.ts b/plugins/catalog-backend/src/next/database/DatabaseManager.ts deleted file mode 100644 index 3b31542025..0000000000 --- a/plugins/catalog-backend/src/next/database/DatabaseManager.ts +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2020 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { getVoidLogger, resolvePackagePath } from '@backstage/backend-common'; -import knexFactory, { Knex } from 'knex'; -import { v4 as uuid } from 'uuid'; -import { Logger } from 'winston'; -import { CommonDatabase, Database } from '../../legacy'; - -export type CreateDatabaseOptions = { - logger: Logger; -}; - -const defaultOptions: CreateDatabaseOptions = { - logger: getVoidLogger(), -}; - -export class DatabaseManager { - public static async createDatabase( - knex: Knex, - options: Partial = {}, - ): Promise { - const migrationsDir = resolvePackagePath( - '@backstage/plugin-catalog-backend', - 'migrations', - ); - - await knex.migrate.latest({ - directory: migrationsDir, - }); - const { logger } = { ...defaultOptions, ...options }; - return new CommonDatabase(knex, logger); - } - - public static async createInMemoryDatabase(): Promise { - const knex = await this.createInMemoryDatabaseConnection(); - return await this.createDatabase(knex); - } - - public static async createInMemoryDatabaseConnection(): Promise { - const knex = knexFactory({ - client: 'sqlite3', - connection: ':memory:', - useNullAsDefault: true, - }); - - knex.client.pool.on('createSuccess', (_eventId: any, resource: any) => { - resource.run('PRAGMA foreign_keys = ON', () => {}); - }); - - return knex; - } - - public static async createTestDatabase(): Promise { - const knex = await this.createTestDatabaseConnection(); - return await this.createDatabase(knex); - } - - public static async createTestDatabaseConnection(): Promise { - const config: Knex.Config = { - client: 'sqlite3', - connection: ':memory:', - useNullAsDefault: true, - }; - - let knex = knexFactory(config); - if (typeof config.connection !== 'string') { - const tempDbName = `d${uuid().replace(/-/g, '')}`; - await knex.raw(`CREATE DATABASE ${tempDbName};`); - knex = knexFactory({ - ...config, - connection: { - ...config.connection, - database: tempDbName, - }, - }); - } - - knex.client.pool.on('createSuccess', (_eventId: any, resource: any) => { - resource.run('PRAGMA foreign_keys = ON', () => {}); - }); - - return knex; - } -} diff --git a/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.test.ts b/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.test.ts index 6148325581..5f509747f3 100644 --- a/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.test.ts +++ b/plugins/catalog-backend/src/next/database/DefaultProcessingDatabase.test.ts @@ -21,7 +21,7 @@ import { Knex } from 'knex'; import * as uuid from 'uuid'; import { Logger } from 'winston'; import { DateTime } from 'luxon'; -import { DatabaseManager } from './DatabaseManager'; +import { applyDatabaseMigrations } from './migrations'; import { DefaultProcessingDatabase } from './DefaultProcessingDatabase'; import { DbRefreshStateReferencesRow, @@ -43,7 +43,7 @@ describe('Default Processing Database', () => { logger: Logger = defaultLogger, ) { const knex = await databases.init(databaseId); - await DatabaseManager.createDatabase(knex); + await applyDatabaseMigrations(knex); return { knex, db: new DefaultProcessingDatabase({ diff --git a/plugins/catalog-backend/src/next/database/migrations.ts b/plugins/catalog-backend/src/next/database/migrations.ts new file mode 100644 index 0000000000..ed3a7751b5 --- /dev/null +++ b/plugins/catalog-backend/src/next/database/migrations.ts @@ -0,0 +1,29 @@ +/* + * Copyright 2020 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { resolvePackagePath } from '@backstage/backend-common'; +import { Knex } from 'knex'; + +export async function applyDatabaseMigrations(knex: Knex): Promise { + const migrationsDir = resolvePackagePath( + '@backstage/plugin-catalog-backend', + 'migrations', + ); + + await knex.migrate.latest({ + directory: migrationsDir, + }); +} diff --git a/plugins/catalog-backend/src/next/stitching/Stitcher.test.ts b/plugins/catalog-backend/src/next/stitching/Stitcher.test.ts index 96af65fffb..be57ff7083 100644 --- a/plugins/catalog-backend/src/next/stitching/Stitcher.test.ts +++ b/plugins/catalog-backend/src/next/stitching/Stitcher.test.ts @@ -17,7 +17,7 @@ import { getVoidLogger } from '@backstage/backend-common'; import { TestDatabases } from '@backstage/backend-test-utils'; import { Entity } from '@backstage/catalog-model'; -import { DatabaseManager } from '../database/DatabaseManager'; +import { applyDatabaseMigrations } from '../database/migrations'; import { DbFinalEntitiesRow, DbRefreshStateReferencesRow, @@ -37,7 +37,7 @@ describe('Stitcher', () => { 'runs the happy path for %p', async databaseId => { const db = await databases.init(databaseId); - await DatabaseManager.createDatabase(db); + await applyDatabaseMigrations(db); const stitcher = new Stitcher(db, logger); let entities: DbFinalEntitiesRow[];