From d91bac0013884992da3435f06ca2e889e07f1e59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Thu, 8 Dec 2022 19:29:41 +0100 Subject: [PATCH 1/3] add the beginnings of a migrations test for the catalog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .../src/database/migrations.test.ts | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 plugins/catalog-backend/src/database/migrations.test.ts diff --git a/plugins/catalog-backend/src/database/migrations.test.ts b/plugins/catalog-backend/src/database/migrations.test.ts new file mode 100644 index 0000000000..d939d04ac6 --- /dev/null +++ b/plugins/catalog-backend/src/database/migrations.test.ts @@ -0,0 +1,90 @@ +/* + * Copyright 2022 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 { Knex } from 'knex'; +import { TestDatabases } from '@backstage/backend-test-utils'; +import fs from 'fs'; + +const migrationsDir = `${__dirname}/../../migrations`; +const migrationsFiles = fs.readdirSync(migrationsDir).sort(); + +async function migrateOnce(knex: Knex): Promise { + await knex.migrate.up({ directory: migrationsDir }); +} + +async function migrateUntilBefore(knex: Knex, target: string): Promise { + const index = migrationsFiles.indexOf(target); + if (index === -1) { + throw new Error(`Migration ${target} not found`); + } + for (let i = 0; i < index; i++) { + await migrateOnce(knex); + } +} + +describe('migrations', () => { + const databases = TestDatabases.create({ + ids: ['MYSQL_8', 'POSTGRES_13', 'POSTGRES_9', 'SQLITE_3'], + }); + + it.each(databases.eachSupportedId())( + '20221109192547_search_add_original_value_column.js, %p', + async databaseId => { + const knex = await databases.init(databaseId); + + await migrateUntilBefore( + knex, + '20221109192547_search_add_original_value_column.js', + ); + + await knex + .insert({ + entity_id: 'i', + entity_ref: 'k:ns/n', + unprocessed_entity: '{}', + errors: '[]', + next_update_at: new Date(), + last_discovery_at: new Date(), + }) + .into('refresh_state'); + await knex + .insert({ entity_id: 'i', key: 'k1', value: 'v1' }) + .into('search'); + await knex + .insert({ entity_id: 'i', key: 'k2', value: null }) + .into('search'); + + await expect(knex('search')).resolves.toEqual( + expect.arrayContaining([ + { entity_id: 'i', key: 'k1', value: 'v1' }, + { entity_id: 'i', key: 'k2', value: null }, + ]), + ); + + await knex.migrate.up({ directory: migrationsDir }); + + await expect(knex('search')).resolves.toEqual( + expect.arrayContaining([ + { entity_id: 'i', key: 'k1', value: 'v1', original_value: 'v1' }, + { entity_id: 'i', key: 'k2', value: null, original_value: null }, + ]), + ); + + await knex.destroy(); + }, + 60_000, + ); +}); From 2cb4cd1a7ba7a96cbcebb993c2514059766d46de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Thu, 8 Dec 2022 20:04:11 +0100 Subject: [PATCH 2/3] down too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .../src/database/migrations.test.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/plugins/catalog-backend/src/database/migrations.test.ts b/plugins/catalog-backend/src/database/migrations.test.ts index d939d04ac6..8287ed882d 100644 --- a/plugins/catalog-backend/src/database/migrations.test.ts +++ b/plugins/catalog-backend/src/database/migrations.test.ts @@ -21,17 +21,21 @@ import fs from 'fs'; const migrationsDir = `${__dirname}/../../migrations`; const migrationsFiles = fs.readdirSync(migrationsDir).sort(); -async function migrateOnce(knex: Knex): Promise { +async function migrateUpOnce(knex: Knex): Promise { await knex.migrate.up({ directory: migrationsDir }); } +async function migrateDownOnce(knex: Knex): Promise { + await knex.migrate.down({ directory: migrationsDir }); +} + async function migrateUntilBefore(knex: Knex, target: string): Promise { const index = migrationsFiles.indexOf(target); if (index === -1) { throw new Error(`Migration ${target} not found`); } for (let i = 0; i < index; i++) { - await migrateOnce(knex); + await migrateUpOnce(knex); } } @@ -74,7 +78,7 @@ describe('migrations', () => { ]), ); - await knex.migrate.up({ directory: migrationsDir }); + await migrateUpOnce(knex); await expect(knex('search')).resolves.toEqual( expect.arrayContaining([ @@ -83,6 +87,15 @@ describe('migrations', () => { ]), ); + await migrateDownOnce(knex); + + await expect(knex('search')).resolves.toEqual( + expect.arrayContaining([ + { entity_id: 'i', key: 'k1', value: 'v1' }, + { entity_id: 'i', key: 'k2', value: null }, + ]), + ); + await knex.destroy(); }, 60_000, From 25e38ebb8ca7c3e1e7e111e4ea91d7e8ff013979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Thu, 8 Dec 2022 20:06:23 +0100 Subject: [PATCH 3/3] move to root MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- plugins/catalog-backend/src/{database => }/migrations.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename plugins/catalog-backend/src/{database => }/migrations.test.ts (98%) diff --git a/plugins/catalog-backend/src/database/migrations.test.ts b/plugins/catalog-backend/src/migrations.test.ts similarity index 98% rename from plugins/catalog-backend/src/database/migrations.test.ts rename to plugins/catalog-backend/src/migrations.test.ts index 8287ed882d..9d9b02fa82 100644 --- a/plugins/catalog-backend/src/database/migrations.test.ts +++ b/plugins/catalog-backend/src/migrations.test.ts @@ -18,7 +18,7 @@ import { Knex } from 'knex'; import { TestDatabases } from '@backstage/backend-test-utils'; import fs from 'fs'; -const migrationsDir = `${__dirname}/../../migrations`; +const migrationsDir = `${__dirname}/../migrations`; const migrationsFiles = fs.readdirSync(migrationsDir).sort(); async function migrateUpOnce(knex: Knex): Promise {