diff --git a/plugins/search-backend-module-pg/api-report.md b/plugins/search-backend-module-pg/api-report.md index d1913a535d..33d0f4318c 100644 --- a/plugins/search-backend-module-pg/api-report.md +++ b/plugins/search-backend-module-pg/api-report.md @@ -26,7 +26,9 @@ export class DatabaseDocumentStore implements DatabaseStore { // (undocumented) completeInsert(tx: Knex.Transaction, type: string): Promise; // (undocumented) - static create(knex: Knex): Promise; + static create( + database: PluginDatabaseManager, + ): Promise; // (undocumented) getTransaction(): Promise; // (undocumented) diff --git a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts index 9da5ceec0e..cd99a8c4b1 100644 --- a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts +++ b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts @@ -115,14 +115,14 @@ export class PgSearchEngine implements SearchEngine { config: Config; }): Promise { return new PgSearchEngine( - await DatabaseDocumentStore.create(await options.database.getClient()), + await DatabaseDocumentStore.create(options.database), options.config, ); } static async fromConfig(config: Config, options: PgSearchOptions) { return new PgSearchEngine( - await DatabaseDocumentStore.create(await options.database.getClient()), + await DatabaseDocumentStore.create(options.database), config, ); } diff --git a/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts b/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts index 355e41352d..59c8d85562 100644 --- a/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts +++ b/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts @@ -13,6 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +import { Knex as KnexType } from 'knex'; import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils'; import { IndexableDocument } from '@backstage/plugin-search-common'; import { PgSearchHighlightOptions } from '../PgSearchEngine'; @@ -30,6 +32,18 @@ const highlightOptions: PgSearchHighlightOptions = { fragmentDelimiter: ' ... ', }; +function createDatabaseManager( + client: KnexType, + skipMigrations: boolean = false, +) { + return { + getClient: async () => client, + migrations: { + skip: skipMigrations, + }, + }; +} + describe('DatabaseDocumentStore', () => { describe('unsupported', () => { const databases = TestDatabases.create({ @@ -51,9 +65,10 @@ describe('DatabaseDocumentStore', () => { 'should fail to create, %p', async databaseId => { const knex = await databases.init(databaseId); + const databaseManager = createDatabaseManager(knex); await expect( - async () => await DatabaseDocumentStore.create(knex), + async () => await DatabaseDocumentStore.create(databaseManager), ).rejects.toThrow(); }, 60_000, @@ -67,7 +82,9 @@ describe('DatabaseDocumentStore', () => { async function createStore(databaseId: TestDatabaseId) { const knex = await databases.init(databaseId); - const store = await DatabaseDocumentStore.create(knex); + const databaseManager = createDatabaseManager(knex); + const store = await DatabaseDocumentStore.create(databaseManager); + return { store, knex }; } diff --git a/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.ts b/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.ts index 8e73c0f16f..4da3cca082 100644 --- a/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.ts +++ b/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.ts @@ -13,7 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { resolvePackagePath } from '@backstage/backend-common'; +import { + PluginDatabaseManager, + resolvePackagePath, +} from '@backstage/backend-common'; import { IndexableDocument } from '@backstage/plugin-search-common'; import { Knex } from 'knex'; import { @@ -30,7 +33,10 @@ const migrationsDir = resolvePackagePath( ); export class DatabaseDocumentStore implements DatabaseStore { - static async create(knex: Knex): Promise { + static async create( + database: PluginDatabaseManager, + ): Promise { + const knex = await database.getClient(); try { const majorVersion = await queryPostgresMajorVersion(knex); @@ -49,9 +55,12 @@ export class DatabaseDocumentStore implements DatabaseStore { ); } - await knex.migrate.latest({ - directory: migrationsDir, - }); + if (!database.migrations?.skip) { + await knex.migrate.latest({ + directory: migrationsDir, + }); + } + return new DatabaseDocumentStore(knex); }