catalog-backend: Refuse to remove the bootstrap location

This commit is contained in:
Fredrik Adelöw
2021-01-15 12:03:50 +01:00
parent 057cb8e84f
commit ac7be581a1
3 changed files with 33 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
Refuse to remove the bootstrap location
@@ -137,6 +137,22 @@ describe('CommonDatabase', () => {
await expect(db.location(location.id)).rejects.toThrow(/Found no location/);
});
it('refuses to remove the bootstrap location', async () => {
const input: Location = {
id: 'dd12620d-0436-422f-93bd-929aa0788123',
type: 'bootstrap',
target: 'bootstrap',
};
const output = await db.transaction(
async tx => await db.addLocation(tx, input),
);
await expect(
db.transaction(async tx => await db.removeLocation(tx, output.id)),
).rejects.toThrow(ConflictError);
});
describe('addEntities', () => {
it('happy path: adds entities to empty database', async () => {
const result = await db.transaction(tx =>
@@ -330,15 +330,21 @@ export class CommonDatabase implements Database {
async removeLocation(txOpaque: Transaction, id: string): Promise<void> {
const tx = txOpaque as Knex.Transaction<any, any>;
const locations = await tx<DbLocationsRow>('locations')
.where({ id })
.select();
if (!locations.length) {
throw new NotFoundError(`Found no location with ID ${id}`);
}
if (locations[0].type === 'bootstrap') {
throw new ConflictError('You may not delete the bootstrap location.');
}
await tx<DbEntitiesRow>('entities')
.where({ location_id: id })
.update({ location_id: null });
const result = await tx<DbLocationsRow>('locations').where({ id }).del();
if (!result) {
throw new NotFoundError(`Found no location with ID ${id}`);
}
await tx<DbLocationsRow>('locations').where({ id }).del();
}
async location(id: string): Promise<DbLocationsRowWithStatus> {