From 34d4360e6efa4f7cce800b2a85d3b68c95140c3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Mon, 11 Nov 2024 17:09:21 +0100 Subject: [PATCH] remove redundant indices MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/twelve-masks-call.md | 14 +++++ .../20241111000000_drop_redundant_indices.js | 57 +++++++++++++++++++ .../src/tests/migrations.test.ts | 19 +++++++ 3 files changed, 90 insertions(+) create mode 100644 .changeset/twelve-masks-call.md create mode 100644 plugins/catalog-backend/migrations/20241111000000_drop_redundant_indices.js diff --git a/.changeset/twelve-masks-call.md b/.changeset/twelve-masks-call.md new file mode 100644 index 0000000000..7fc79db655 --- /dev/null +++ b/.changeset/twelve-masks-call.md @@ -0,0 +1,14 @@ +--- +'@backstage/plugin-catalog-backend': minor +--- + +Drop redundant indices from the database. + +The following redundant indices are removed in this version: + +- `final_entities_entity_id_idx` - overlaps with `final_entities_pkey` +- `refresh_state_entity_id_idx` - overlaps with `refresh_state_pkey` +- `refresh_state_entity_ref_idx` - overlaps with `refresh_state_entity_ref_uniq` +- `search_key_idx` and `search_value_idx` - these were replaced by the composite index `search_key_value_idx` in #22594 + +No negative end user impact is expected, but rather that performance should increase due to less index churn. diff --git a/plugins/catalog-backend/migrations/20241111000000_drop_redundant_indices.js b/plugins/catalog-backend/migrations/20241111000000_drop_redundant_indices.js new file mode 100644 index 0000000000..142e372cb9 --- /dev/null +++ b/plugins/catalog-backend/migrations/20241111000000_drop_redundant_indices.js @@ -0,0 +1,57 @@ +/* + * 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('final_entities', table => { + table.dropIndex([], 'final_entities_entity_id_idx'); // overlaps with final_entities_pkey + }); + + await knex.schema.alterTable('refresh_state', table => { + table.dropIndex([], 'refresh_state_entity_id_idx'); // overlaps with refresh_state_pkey + table.dropIndex([], 'refresh_state_entity_ref_idx'); // overlaps with refresh_state_entity_ref_uniq + }); + + await knex.schema.alterTable('search', table => { + table.dropIndex([], 'search_key_idx'); // was replaced by search_key_value_idx in 20240130092632_search_index + table.dropIndex([], 'search_value_idx'); // was replaced by search_key_value_idx in 20240130092632_search_index + }); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = async function down(knex) { + await knex.schema.alterTable('final_entities', table => { + table.index('entity_id', 'final_entities_entity_id_idx'); + }); + + await knex.schema.alterTable('refresh_state', table => { + table.index('entity_id', 'refresh_state_entity_id_idx'); + table.index('entity_ref', 'refresh_state_entity_ref_idx'); + }); + + await knex.schema.alterTable('search', table => { + table.index(['key'], 'search_key_idx'); + table.index(['value'], 'search_value_idx'); + }); +}; diff --git a/plugins/catalog-backend/src/tests/migrations.test.ts b/plugins/catalog-backend/src/tests/migrations.test.ts index 8ee228dd5c..bb840f7a21 100644 --- a/plugins/catalog-backend/src/tests/migrations.test.ts +++ b/plugins/catalog-backend/src/tests/migrations.test.ts @@ -502,4 +502,23 @@ describe('migrations', () => { await knex.destroy(); }, ); + + it.each(databases.eachSupportedId())( + '20241111000000_drop_redundant_indices.js, %p', + async databaseId => { + const knex = await databases.init(databaseId); + + await migrateUntilBefore( + knex, + '20241111000000_drop_redundant_indices.js', + ); + + await migrateUpOnce(knex); + + await migrateDownOnce(knex); + + expect(true).toBe(true); + await knex.destroy(); + }, + ); });