From 120f74bc919f081a8ed832df571d3a51f85a69c6 Mon Sep 17 00:00:00 2001 From: Kashish Mittal Date: Thu, 3 Oct 2024 16:10:54 -0400 Subject: [PATCH] add a migration to catalog-backend to alter target column of locations table to type text Signed-off-by: Kashish Mittal --- .../migrations/20200511113813_init.js | 2 +- ...0241003170511_alter_target_in_locations.js | 37 +++++++++++++++++++ .../src/tests/migrations.test.ts | 22 +++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 plugins/catalog-backend/migrations/20241003170511_alter_target_in_locations.js diff --git a/plugins/catalog-backend/migrations/20200511113813_init.js b/plugins/catalog-backend/migrations/20200511113813_init.js index 5b7e3b8dec..710ff537e0 100644 --- a/plugins/catalog-backend/migrations/20200511113813_init.js +++ b/plugins/catalog-backend/migrations/20200511113813_init.js @@ -36,7 +36,7 @@ exports.up = async function up(knex) { .comment('Auto-generated ID of the location'); table.string('type').notNullable().comment('The type of location'); table - .text('target') + .string('target') .notNullable() .comment('The actual target of the location'); }) diff --git a/plugins/catalog-backend/migrations/20241003170511_alter_target_in_locations.js b/plugins/catalog-backend/migrations/20241003170511_alter_target_in_locations.js new file mode 100644 index 0000000000..d909186a73 --- /dev/null +++ b/plugins/catalog-backend/migrations/20241003170511_alter_target_in_locations.js @@ -0,0 +1,37 @@ +/* + * Copyright 2024 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. + */ + +// @ts-check + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = async function up(knex) { + await knex.schema.alterTable('locations', table => { + table.text('target').alter(); + }); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = async function down(knex) { + await knex.schema.alterTable('locations', table => { + table.string('target').alter(); + }); +}; diff --git a/plugins/catalog-backend/src/tests/migrations.test.ts b/plugins/catalog-backend/src/tests/migrations.test.ts index d961427f5e..ee0ed8eb83 100644 --- a/plugins/catalog-backend/src/tests/migrations.test.ts +++ b/plugins/catalog-backend/src/tests/migrations.test.ts @@ -308,4 +308,26 @@ describe('migrations', () => { await knex.destroy(); }, ); + + it.each(databases.eachSupportedId())( + '20241003170511_alter_target_in_locations.js, %p', + async databaseId => { + const knex = await databases.init(databaseId); + + await migrateUntilBefore( + knex, + '20241003170511_alter_target_in_locations.js', + ); + + await migrateUpOnce(knex); + const columnInfo = await knex('locations').columnInfo(); + expect(columnInfo.target.type).toBe('text'); + + await migrateDownOnce(knex); + const revertedColumnInfo = await knex('locations').columnInfo(); + expect(revertedColumnInfo.target.type).toBe('varchar'); + + await knex.destroy(); + }, + ); });