Skip to content

Commit

Permalink
Use Data Source ID only to check for rule type references (#5121)
Browse files Browse the repository at this point in the history
The project ID belongs to the rule type, so we only check for the data
source ID when checking for references before attempting to delete. This
should be fairly safe as we have already checked the project ID
beforehand.

Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX authored Dec 3, 2024
1 parent c7e8d6d commit fe30177
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 38 deletions.
8 changes: 4 additions & 4 deletions database/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion database/query/datasources.sql
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ WHERE data_source_id = $1 AND project_id = $2;
--
-- name: ListRuleTypesReferencesByDataSource :many
SELECT * FROM rule_type_data_sources
WHERE data_sources_id = $1 AND project_id = $2;
WHERE data_sources_id = $1;

-- AddRuleTypeDataSourceReference adds a link between one rule type
-- and one data source it uses.
Expand Down
5 changes: 1 addition & 4 deletions internal/datasources/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,7 @@ func (d *dataSourceService) Delete(
tx := stx.Q()

// List rule types referencing the data source
ret, err := tx.ListRuleTypesReferencesByDataSource(ctx, db.ListRuleTypesReferencesByDataSourceParams{
DataSourcesID: id,
ProjectID: project,
})
ret, err := tx.ListRuleTypesReferencesByDataSource(ctx, id)
if err != nil {
return fmt.Errorf("failed to list rule types referencing data source %s: %w", id, err)
}
Expand Down
25 changes: 5 additions & 20 deletions internal/datasources/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,10 +898,7 @@ func TestDelete(t *testing.T) {
setup: func(args args, mockDB *mockdb.MockStore) {
// Mock ListRuleTypesReferencesByDataSource to return empty list
mockDB.EXPECT().
ListRuleTypesReferencesByDataSource(gomock.Any(), gomock.Eq(db.ListRuleTypesReferencesByDataSourceParams{
DataSourcesID: args.id,
ProjectID: args.project,
})).
ListRuleTypesReferencesByDataSource(gomock.Any(), args.id).
Return([]db.RuleTypeDataSource{}, nil)

// Mock DeleteDataSource to succeed
Expand All @@ -924,10 +921,7 @@ func TestDelete(t *testing.T) {
setup: func(args args, mockDB *mockdb.MockStore) {
// Mock ListRuleTypesReferencesByDataSource to return empty list
mockDB.EXPECT().
ListRuleTypesReferencesByDataSource(gomock.Any(), gomock.Eq(db.ListRuleTypesReferencesByDataSourceParams{
DataSourcesID: args.id,
ProjectID: args.project,
})).
ListRuleTypesReferencesByDataSource(gomock.Any(), args.id).
Return([]db.RuleTypeDataSource{}, nil)

// Mock DeleteDataSource to return sql.ErrNoRows
Expand All @@ -950,10 +944,7 @@ func TestDelete(t *testing.T) {
setup: func(args args, mockDB *mockdb.MockStore) {
// Mock ListRuleTypesReferencesByDataSource to return non-empty list
mockDB.EXPECT().
ListRuleTypesReferencesByDataSource(gomock.Any(), gomock.Eq(db.ListRuleTypesReferencesByDataSourceParams{
DataSourcesID: args.id,
ProjectID: args.project,
})).
ListRuleTypesReferencesByDataSource(gomock.Any(), args.id).
Return([]db.RuleTypeDataSource{
{RuleTypeID: uuid.New()},
}, nil)
Expand All @@ -970,10 +961,7 @@ func TestDelete(t *testing.T) {
setup: func(args args, mockDB *mockdb.MockStore) {
// Mock ListRuleTypesReferencesByDataSource to return an error
mockDB.EXPECT().
ListRuleTypesReferencesByDataSource(gomock.Any(), gomock.Eq(db.ListRuleTypesReferencesByDataSourceParams{
DataSourcesID: args.id,
ProjectID: args.project,
})).
ListRuleTypesReferencesByDataSource(gomock.Any(), args.id).
Return(nil, fmt.Errorf("database error"))
},
wantErr: true,
Expand All @@ -988,10 +976,7 @@ func TestDelete(t *testing.T) {
setup: func(args args, mockDB *mockdb.MockStore) {
// Mock ListRuleTypesReferencesByDataSource to return empty list
mockDB.EXPECT().
ListRuleTypesReferencesByDataSource(gomock.Any(), gomock.Eq(db.ListRuleTypesReferencesByDataSourceParams{
DataSourcesID: args.id,
ProjectID: args.project,
})).
ListRuleTypesReferencesByDataSource(gomock.Any(), args.id).
Return([]db.RuleTypeDataSource{}, nil)

// Mock DeleteDataSource to return an error
Expand Down
11 changes: 3 additions & 8 deletions internal/db/datasources.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/db/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fe30177

Please sign in to comment.