diff --git a/plugins/catalog-backend/migrations/20241003170511_alter_target_in_locations.js b/plugins/catalog-backend/migrations/20241003170511_alter_target_in_locations.js index d909186a73..855e1a7f8d 100644 --- a/plugins/catalog-backend/migrations/20241003170511_alter_target_in_locations.js +++ b/plugins/catalog-backend/migrations/20241003170511_alter_target_in_locations.js @@ -31,6 +31,16 @@ exports.up = async function up(knex) { * @returns { Promise } */ exports.down = async function down(knex) { + const oversizedEntries = await knex('locations').where( + knex.raw('LENGTH(target) > 255'), + ); + + if (oversizedEntries.length > 0) { + throw new Error( + `Migration aborted: Found ${oversizedEntries.length} entries with 'target' exceeding 255 characters. Manual intervention required.`, + ); + } + 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 370241dda1..873746ac2b 100644 --- a/plugins/catalog-backend/src/tests/migrations.test.ts +++ b/plugins/catalog-backend/src/tests/migrations.test.ts @@ -345,7 +345,7 @@ describe('migrations', () => { .into('locations'); // Verify that both the simple and long URLs exist after the migration - await expect(knex('locations')).resolves.toEqual( + await expect(knex('locations').where({ type: 'url' })).resolves.toEqual( expect.arrayContaining([ { id: '1f99f7f6-1d87-4e8c-994a-8a2ba1d542f6', @@ -361,6 +361,18 @@ describe('migrations', () => { ]), ); + await expect(migrateDownOnce(knex)).rejects.toThrow( + `Migration aborted: Found 1 entries with 'target' exceeding 255 characters. Manual intervention required.`, + ); + + // Now remove the long URL + await knex('locations') + .where({ + id: '1f99f6f7-1d87-4e8c-994a-2a8ba1d542f6', + }) + .del(); + + // Retry the migration down after removing the long URL await migrateDownOnce(knex); // Verify that the column type has reverted to varchar @@ -369,20 +381,14 @@ describe('migrations', () => { /^(varchar|character varying)$/, ); - // Verify that both the simple and long URLs exist after the rollback - await expect(knex('locations')).resolves.toEqual( + // Verify that the short URL still exists in the table + await expect(knex('locations').where({ type: 'url' })).resolves.toEqual( expect.arrayContaining([ { id: '1f99f7f6-1d87-4e8c-994a-8a2ba1d542f6', target: 'https://example.com', type: 'url', }, - { - id: '1f99f6f7-1d87-4e8c-994a-2a8ba1d542f6', - target: - 'https://example.com/foo-bar-test-group/very-long-group-name-that-exceeds-255-characters-just-to-test-the-limits-of-url-length-in-the-catalog-info-yaml-file-and-see-how-the-backstage-system-handles-it-making/test-this-alright-1/-/blob/main/catalog-info.yaml', - type: 'url', - }, ]), );