diff --git a/.changeset/grumpy-bikes-begin.md b/.changeset/grumpy-bikes-begin.md new file mode 100644 index 0000000000..a98e6651f5 --- /dev/null +++ b/.changeset/grumpy-bikes-begin.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-tech-insights-backend': patch +--- + +Add DB index to improve latency of latest fact query diff --git a/plugins/tech-insights-backend/knexfile.js b/plugins/tech-insights-backend/knexfile.js new file mode 100644 index 0000000000..b771c202f5 --- /dev/null +++ b/plugins/tech-insights-backend/knexfile.js @@ -0,0 +1,26 @@ +/* + * Copyright 2023 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. + */ + +// This file makes it possible to run "yarn knex migrate:make some_file_name" +// to assist in making new migrations +module.exports = { + client: 'better-sqlite3', + connection: ':memory:', + useNullAsDefault: true, + migrations: { + directory: './migrations', + }, +}; diff --git a/plugins/tech-insights-backend/migrations/20230213170839_latest-facts-index.js b/plugins/tech-insights-backend/migrations/20230213170839_latest-facts-index.js new file mode 100644 index 0000000000..94a589f59d --- /dev/null +++ b/plugins/tech-insights-backend/migrations/20230213170839_latest-facts-index.js @@ -0,0 +1,36 @@ +/* + * Copyright 2023 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. + */ + +const indexName = 'fact_id_entity_timestamp_idx'; +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = async knex => { + await knex.schema.alterTable('facts', table => { + table.index(['id', 'entity', 'timestamp'], indexName); + }); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = async knex => { + await knex.schema.alterTable('facts', table => { + table.dropIndex(indexName); + }); +};