Skip to content

Commit

Permalink
db/migrations: adds constraint on firmware sets to restrict fimrmware…
Browse files Browse the repository at this point in the history
… deletion

When a firmware row is referenced by a firmware set, restrict deletion of the firmware row,
This ensures referential integrity in firmware sets.

Updates test for constraint check.
  • Loading branch information
joelrebel committed Aug 16, 2023
1 parent 5ee3b2d commit 6a5a33c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
16 changes: 16 additions & 0 deletions db/migrations/00020_firmware_set_update_constraint.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- +goose NO TRANSACTION
-- +goose Up
-- +goose StatementBegin

ALTER TABLE component_firmware_set_map DROP CONSTRAINT fk_firmware_id_ref_component_firmware_version;
ALTER TABLE component_firmware_set_map ADD CONSTRAINT fk_firmware_id_ref_component_firmware_version FOREIGN KEY (firmware_id) REFERENCES component_firmware_version(id) ON DELETE RESTRICT;

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin

ALTER TABLE component_firmware_set_map DROP CONSTRAINT fk_firmware_id_ref_component_firmware_version;
ALTER TABLE component_firmware_set_map ADD CONSTRAINT fk_firmware_id_ref_component_firmware_version FOREIGN KEY (firmware_id) REFERENCES component_firmware_version(id) ON DELETE CASCADE;

-- +goose StatementEnd
5 changes: 4 additions & 1 deletion pkg/api/v1/router_firmware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,13 @@ func TestIntegrationServerComponentFirmwareDelete(t *testing.T) {

realClientTests(t, func(ctx context.Context, authToken string, respCode int, expectError bool) error {
s.Client.SetToken(authToken)
_, err := s.Client.DeleteServerComponentFirmware(ctx, serverservice.ComponentFirmwareVersion{UUID: uuid.MustParse(dbtools.FixtureDellR640BMC.ID)})
_, err := s.Client.DeleteServerComponentFirmware(ctx, serverservice.ComponentFirmwareVersion{UUID: uuid.MustParse(dbtools.FixtureDellR640CPLD.ID)})

return err
})

_, err := s.Client.DeleteServerComponentFirmware(context.TODO(), serverservice.ComponentFirmwareVersion{UUID: uuid.MustParse(dbtools.FixtureDellR640BMC.ID)})
assert.Contains(t, err.Error(), "violates foreign key constraint \"fk_firmware_id_ref_component_firmware_version\"")
}

func TestIntegrationServerComponentFirmwareUpdate(t *testing.T) {
Expand Down

0 comments on commit 6a5a33c

Please sign in to comment.