feat(catalog-backend): remove redundant columns in entities

This commit is contained in:
Fredrik Adelöw
2020-10-06 20:53:44 +02:00
parent a9e7685a48
commit c2e2d92000
3 changed files with 64 additions and 17 deletions
@@ -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');
});
};
@@ -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<any, any>;
const rows = await tx<DbEntitiesRow>('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),
};
}
@@ -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;