From 04eedfc4bf2f9550a398236783ffab9402760310 Mon Sep 17 00:00:00 2001 From: Daniel Dias Branco Arthaud Date: Sun, 14 Aug 2022 15:00:36 -0300 Subject: [PATCH] fix(app-backend): Allow it to skip migrations Signed-off-by: Daniel Dias Branco Arthaud --- .../src/lib/assets/StaticAssetsStore.test.ts | 32 +++++++++++++++---- .../src/lib/assets/StaticAssetsStore.ts | 27 ++++++++++------ plugins/app-backend/src/service/router.ts | 2 +- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/plugins/app-backend/src/lib/assets/StaticAssetsStore.test.ts b/plugins/app-backend/src/lib/assets/StaticAssetsStore.test.ts index 95de905e7a..3d0973e18c 100644 --- a/plugins/app-backend/src/lib/assets/StaticAssetsStore.test.ts +++ b/plugins/app-backend/src/lib/assets/StaticAssetsStore.test.ts @@ -14,12 +14,25 @@ * limitations under the License. */ +import { Knex as KnexType } from 'knex'; import { getVoidLogger } from '@backstage/backend-common'; import { TestDatabases } from '@backstage/backend-test-utils'; import { StaticAssetsStore } from './StaticAssetsStore'; const logger = getVoidLogger(); +function createDatabaseManager( + client: KnexType, + skipMigrations: boolean = false, +) { + return { + getClient: async () => client, + migrations: { + skip: skipMigrations, + }, + }; +} + describe('StaticAssetsStore', () => { const databases = TestDatabases.create({ ids: ['POSTGRES_13', 'POSTGRES_9', 'SQLITE_3'], @@ -28,9 +41,11 @@ describe('StaticAssetsStore', () => { it.each(databases.eachSupportedId())( 'should store and retrieve assets, %p', async databaseId => { + const client = await databases.init(databaseId); + const database = createDatabaseManager(client); const store = await StaticAssetsStore.create({ logger, - database: await databases.init(databaseId), + database, }); await store.storeAssets([ @@ -69,9 +84,11 @@ describe('StaticAssetsStore', () => { it.each(databases.eachSupportedId())( 'should update assets timestamps, but not contents, %p', async databaseId => { + const client = await databases.init(databaseId); + const database = createDatabaseManager(client); const store = await StaticAssetsStore.create({ logger, - database: await databases.init(databaseId), + database, }); await store.storeAssets([ @@ -119,7 +136,8 @@ describe('StaticAssetsStore', () => { it.each(databases.eachSupportedId())( 'should trim old assets, %p', async databaseId => { - const database = await databases.init(databaseId); + const knex = await databases.init(databaseId); + const database = createDatabaseManager(knex); const store = await StaticAssetsStore.create({ logger, database, @@ -137,12 +155,12 @@ describe('StaticAssetsStore', () => { ]); // Rewrite modified time of "old" to be 1h in the past - const updated = await database('static_assets_cache') + const updated = await knex('static_assets_cache') .where({ path: 'old' }) .update({ - last_modified_at: database.client.config.client.includes('sqlite3') - ? database.raw(`datetime('now', '-3600 seconds')`) - : database.raw(`now() + interval '-3600 seconds'`), + last_modified_at: knex.client.config.client.includes('sqlite3') + ? knex.raw(`datetime('now', '-3600 seconds')`) + : knex.raw(`now() + interval '-3600 seconds'`), }); expect(updated).toBe(1); diff --git a/plugins/app-backend/src/lib/assets/StaticAssetsStore.ts b/plugins/app-backend/src/lib/assets/StaticAssetsStore.ts index 70d664307e..6905d63779 100644 --- a/plugins/app-backend/src/lib/assets/StaticAssetsStore.ts +++ b/plugins/app-backend/src/lib/assets/StaticAssetsStore.ts @@ -14,7 +14,10 @@ * limitations under the License. */ -import { resolvePackagePath } from '@backstage/backend-common'; +import { + PluginDatabaseManager, + resolvePackagePath, +} from '@backstage/backend-common'; import { Knex } from 'knex'; import { Logger } from 'winston'; import { DateTime } from 'luxon'; @@ -34,7 +37,7 @@ interface StaticAssetRow { /** @internal */ export interface StaticAssetsStoreOptions { - database: Knex; + database: PluginDatabaseManager; logger: Logger; } @@ -48,15 +51,21 @@ export class StaticAssetsStore implements StaticAssetProvider { #logger: Logger; static async create(options: StaticAssetsStoreOptions) { - await options.database.migrate.latest({ - directory: migrationsDir, - }); - return new StaticAssetsStore(options); + const { database } = options; + const client = await database.getClient(); + + if (!database.migrations?.skip) { + await client.migrate.latest({ + directory: migrationsDir, + }); + } + + return new StaticAssetsStore(client, options.logger); } - private constructor(options: StaticAssetsStoreOptions) { - this.#db = options.database; - this.#logger = options.logger; + private constructor(client: Knex, logger: Logger) { + this.#db = client; + this.#logger = logger; } /** diff --git a/plugins/app-backend/src/service/router.ts b/plugins/app-backend/src/service/router.ts index b666f323db..46ecd470da 100644 --- a/plugins/app-backend/src/service/router.ts +++ b/plugins/app-backend/src/service/router.ts @@ -130,7 +130,7 @@ export async function createRouter( if (options.database) { const store = await StaticAssetsStore.create({ logger, - database: await options.database.getClient(), + database: options.database, }); const assets = await findStaticAssets(staticDir);