From 50ce07f8a34b4d67fb3b1feef0367425a9e9133d Mon Sep 17 00:00:00 2001 From: Michelangelo Mori Date: Fri, 29 Nov 2024 16:05:51 +0100 Subject: [PATCH] Add delete statement for `rule_type_data_sources`. This statement is necessary to allow updates on Rule Types. Since an update does not modify the ID, we can't expect cascading deletes to happen, and thus have to perform the cleanup manually. --- database/mock/store.go | 14 ++++++++++++++ database/query/datasources.sql | 5 +++++ internal/db/datasources.sql.go | 16 ++++++++++++++++ internal/db/querier.go | 1 + 4 files changed, 36 insertions(+) diff --git a/database/mock/store.go b/database/mock/store.go index 7dc333d0f5..87343649c6 100644 --- a/database/mock/store.go +++ b/database/mock/store.go @@ -749,6 +749,20 @@ func (mr *MockStoreMockRecorder) DeleteRuleType(ctx, id any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteRuleType", reflect.TypeOf((*MockStore)(nil).DeleteRuleType), ctx, id) } +// DeleteRuleTypeDataSource mocks base method. +func (m *MockStore) DeleteRuleTypeDataSource(ctx context.Context, arg db.DeleteRuleTypeDataSourceParams) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteRuleTypeDataSource", ctx, arg) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteRuleTypeDataSource indicates an expected call of DeleteRuleTypeDataSource. +func (mr *MockStoreMockRecorder) DeleteRuleTypeDataSource(ctx, arg any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteRuleTypeDataSource", reflect.TypeOf((*MockStore)(nil).DeleteRuleTypeDataSource), ctx, arg) +} + // DeleteSelector mocks base method. func (m *MockStore) DeleteSelector(ctx context.Context, id uuid.UUID) error { m.ctrl.T.Helper() diff --git a/database/query/datasources.sql b/database/query/datasources.sql index 59da7881de..19c4103f40 100644 --- a/database/query/datasources.sql +++ b/database/query/datasources.sql @@ -93,3 +93,8 @@ WHERE data_sources_id = $1 AND project_id = $2; INSERT INTO rule_type_data_sources (rule_type_id, data_sources_id, project_id) VALUES (sqlc.arg(ruleTypeID)::uuid, sqlc.arg(dataSourceID)::uuid, sqlc.arg(projectID)::uuid) RETURNING rule_type_id, data_sources_id, project_id; + +-- name: DeleteRuleTypeDataSource :exec +DELETE FROM rule_type_data_sources + WHERE rule_type_id = sqlc.arg(ruleid) + AND project_id = sqlc.arg(projectid); diff --git a/internal/db/datasources.sql.go b/internal/db/datasources.sql.go index eea0b8590a..e1bb145ac7 100644 --- a/internal/db/datasources.sql.go +++ b/internal/db/datasources.sql.go @@ -197,6 +197,22 @@ func (q *Queries) DeleteDataSourceFunctions(ctx context.Context, arg DeleteDataS return items, nil } +const deleteRuleTypeDataSource = `-- name: DeleteRuleTypeDataSource :exec +DELETE FROM rule_type_data_sources + WHERE rule_type_id = $1 + AND project_id = $2 +` + +type DeleteRuleTypeDataSourceParams struct { + Ruleid uuid.UUID `json:"ruleid"` + Projectid uuid.UUID `json:"projectid"` +} + +func (q *Queries) DeleteRuleTypeDataSource(ctx context.Context, arg DeleteRuleTypeDataSourceParams) error { + _, err := q.db.ExecContext(ctx, deleteRuleTypeDataSource, arg.Ruleid, arg.Projectid) + return err +} + const getDataSource = `-- name: GetDataSource :one SELECT id, name, display_name, project_id, created_at, updated_at FROM data_sources diff --git a/internal/db/querier.go b/internal/db/querier.go index 4d12dc68ca..aeb2fe8b58 100644 --- a/internal/db/querier.go +++ b/internal/db/querier.go @@ -78,6 +78,7 @@ type Querier interface { DeleteRepository(ctx context.Context, id uuid.UUID) error DeleteRuleInstanceOfProfileInProject(ctx context.Context, arg DeleteRuleInstanceOfProfileInProjectParams) error DeleteRuleType(ctx context.Context, id uuid.UUID) error + DeleteRuleTypeDataSource(ctx context.Context, arg DeleteRuleTypeDataSourceParams) error DeleteSelector(ctx context.Context, id uuid.UUID) error DeleteSelectorsByProfileID(ctx context.Context, profileID uuid.UUID) error DeleteSessionStateByProjectID(ctx context.Context, arg DeleteSessionStateByProjectIDParams) error