fix(tech-insights-backend): 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 12:09:11 -03:00
parent 262518caa5
commit ba2e4337d1
3 changed files with 33 additions and 13 deletions
@@ -15,7 +15,7 @@
*/
import { DateTime, Duration } from 'luxon';
import { TechInsightsStore } from '@backstage/plugin-tech-insights-node';
import { Knex } from 'knex';
import { Knex as KnexType, Knex } from 'knex';
import { TestDatabases } from '@backstage/backend-test-utils';
import { getVoidLogger } from '@backstage/backend-common';
import { initializePersistenceContext } from './persistenceContext';
@@ -165,15 +165,28 @@ const multipleSameFacts = [
},
];
function createDatabaseManager(
client: KnexType,
skipMigrations: boolean = false,
) {
return {
getClient: async () => client,
migrations: {
skip: skipMigrations,
},
};
}
describe('Tech Insights database', () => {
const databases = TestDatabases.create();
let store: TechInsightsStore;
let testDbClient: Knex<any, unknown[]>;
beforeAll(async () => {
testDbClient = await databases.init('SQLITE_3');
const database = createDatabaseManager(testDbClient);
store = (
await initializePersistenceContext(testDbClient, {
await initializePersistenceContext(database, {
logger: getVoidLogger(),
})
).techInsightsStore;
@@ -13,8 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getVoidLogger, resolvePackagePath } from '@backstage/backend-common';
import { Knex } from 'knex';
import {
getVoidLogger,
PluginDatabaseManager,
resolvePackagePath,
} from '@backstage/backend-common';
import { Logger } from 'winston';
import { TechInsightsDatabase } from './TechInsightsDatabase';
import { TechInsightsStore } from '@backstage/plugin-tech-insights-node';
@@ -47,13 +50,18 @@ const defaultOptions: CreateDatabaseOptions = {
* @public
*/
export const initializePersistenceContext = async (
knex: Knex,
database: PluginDatabaseManager,
options: CreateDatabaseOptions = defaultOptions,
): Promise<PersistenceContext> => {
await knex.migrate.latest({
directory: migrationsDir,
});
const client = await database.getClient();
if (!database.migrations?.skip) {
await client.migrate.latest({
directory: migrationsDir,
});
}
return {
techInsightsStore: new TechInsightsDatabase(knex, options.logger),
techInsightsStore: new TechInsightsDatabase(client, options.logger),
};
};
@@ -135,10 +135,9 @@ export const buildTechInsightsContext = async <
const factRetrieverRegistry = buildFactRetrieverRegistry();
const persistenceContext = await initializePersistenceContext(
await database.getClient(),
{ logger },
);
const persistenceContext = await initializePersistenceContext(database, {
logger,
});
const factRetrieverEngine = await FactRetrieverEngine.create({
scheduler,