fix(search-backend-module-pg): Allow it to skip migrations

Signed-off-by: Daniel Dias Branco Arthaud <arthaud@gmail.com>
This commit is contained in:
Daniel Dias Branco Arthaud
2022-08-14 15:49:18 -03:00
parent 04eedfc4bf
commit 833e2e4904
4 changed files with 38 additions and 10 deletions
@@ -26,7 +26,9 @@ export class DatabaseDocumentStore implements DatabaseStore {
// (undocumented)
completeInsert(tx: Knex.Transaction, type: string): Promise<void>;
// (undocumented)
static create(knex: Knex): Promise<DatabaseDocumentStore>;
static create(
database: PluginDatabaseManager,
): Promise<DatabaseDocumentStore>;
// (undocumented)
getTransaction(): Promise<Knex.Transaction>;
// (undocumented)
@@ -115,14 +115,14 @@ export class PgSearchEngine implements SearchEngine {
config: Config;
}): Promise<PgSearchEngine> {
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,
);
}
@@ -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 };
}
@@ -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<DatabaseDocumentStore> {
static async create(
database: PluginDatabaseManager,
): Promise<DatabaseDocumentStore> {
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);
}