From c2e2d92000a32396cf835cf3148609933e189d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Tue, 6 Oct 2020 20:53:44 +0200 Subject: [PATCH] feat(catalog-backend): remove redundant columns in entities --- ...6203131_entity_remove_redundant_columns.js | 60 +++++++++++++++++++ .../src/database/CommonDatabase.ts | 17 ++---- plugins/catalog-backend/src/database/types.ts | 4 -- 3 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 plugins/catalog-backend/migrations/20201006203131_entity_remove_redundant_columns.js diff --git a/plugins/catalog-backend/migrations/20201006203131_entity_remove_redundant_columns.js b/plugins/catalog-backend/migrations/20201006203131_entity_remove_redundant_columns.js new file mode 100644 index 0000000000..7bacc67338 --- /dev/null +++ b/plugins/catalog-backend/migrations/20201006203131_entity_remove_redundant_columns.js @@ -0,0 +1,60 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 + */ +exports.up = async function up(knex) { + await knex.schema.alterTable('entities', table => { + table.dropColumn('api_version'); + table.dropColumn('kind'); + table.dropColumn('name'); + table.dropColumn('namespace'); + }); + + // NOTE(freben): For some reason, specifically sqlite3 in-mem just drops some + // subset of constraints sometimes, when a table column is dropped - even if + // the column had no relation at all to the constraint. We therefore recreate + // the constraint here as a stupid fix. + if (knex.client.config.client === 'sqlite3') { + await knex.schema.alterTable('entities', table => { + table.unique(['full_name'], 'entities_unique_full_name'); + }); + } +}; + +/** + * @param {import('knex')} knex + */ +exports.down = async function down(knex) { + await knex.schema.alterTable('entities', table => { + table + .string('api_version') + .notNullable() + .comment('The apiVersion field of the entity'); + table.string('kind').notNullable().comment('The kind field of the entity'); + table + .string('name') + .nullable() + .comment('The metadata.name field of the entity'); + table + .string('namespace') + .nullable() + .comment('The metadata.namespace field of the entity'); + }); +}; diff --git a/plugins/catalog-backend/src/database/CommonDatabase.ts b/plugins/catalog-backend/src/database/CommonDatabase.ts index 628b9dfb97..890a64781a 100644 --- a/plugins/catalog-backend/src/database/CommonDatabase.ts +++ b/plugins/catalog-backend/src/database/CommonDatabase.ts @@ -207,9 +207,7 @@ export class CommonDatabase implements Database { const rows = await builder .select('entities.*') - .orderBy('kind', 'asc') - .orderBy('namespace', 'asc') - .orderBy('name', 'asc') + .orderBy('full_name', 'asc') .groupBy('id'); return rows.map(row => this.toEntityResponse(row)); @@ -222,12 +220,9 @@ export class CommonDatabase implements Database { const tx = txOpaque as Knex.Transaction; const rows = await tx('entities') - .whereRaw( - tx.raw( - 'LOWER(kind) = LOWER(?) AND LOWER(namespace) = LOWER(?) AND LOWER(name) = LOWER(?)', - [name.kind, name.namespace, name.name], - ), - ) + .where({ + full_name: `${name.kind}:${name.namespace}/${name.name}`.toLowerCase(), + }) .select(); if (rows.length !== 1) { @@ -399,10 +394,6 @@ export class CommonDatabase implements Database { etag: entity.metadata.etag!, generation: entity.metadata.generation!, full_name: `${lowerKind}:${lowerNamespace}/${lowerName}`, - api_version: entity.apiVersion, - kind: entity.kind, - name: entity.metadata.name, - namespace: entity.metadata.namespace || ENTITY_DEFAULT_NAMESPACE, data: JSON.stringify(data), }; } diff --git a/plugins/catalog-backend/src/database/types.ts b/plugins/catalog-backend/src/database/types.ts index 162bc3507f..5a577f2223 100644 --- a/plugins/catalog-backend/src/database/types.ts +++ b/plugins/catalog-backend/src/database/types.ts @@ -19,10 +19,6 @@ import type { Entity, EntityName, Location } from '@backstage/catalog-model'; export type DbEntitiesRow = { id: string; location_id: string | null; - api_version: string; - kind: string; - name: string | null; - namespace: string | null; etag: string; generation: number; full_name: string;