Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the Delete handler for the datasource service #5062

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions internal/datasources/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,61 @@ func (d *dataSourceService) Update(
panic("implement me")
}

// nolint:revive // there is a TODO
// Delete deletes a data source in the given project.
func (d *dataSourceService) Delete(
ctx context.Context, id uuid.UUID, project uuid.UUID, opts *Options) error {
//TODO implement me
panic("implement me")
stx, err := d.txBuilder(d, opts)
if err != nil {
return fmt.Errorf("failed to start transaction: %w", err)
}
defer func(stx serviceTX) {
err := stx.Rollback()
if err != nil {
fmt.Printf("failed to rollback transaction: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not use fmt.Printf, you can build a zerologger from the context.Context instance.

... Aaaaaand I just realized I missed this in another review. @teodor-yanev can you submit a fix for the Create function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, hahaha, copy pasta! I can fix both here, thanks!

}
}(stx)

// Get the transaction querier
tx := stx.Q()

// List rule types referencing the data source
ret, err := tx.ListRuleTypesReferencesByDataSource(ctx, db.ListRuleTypesReferencesByDataSourceParams{
DataSourcesID: id,
ProjectID: project,
})
if err != nil {
return fmt.Errorf("failed to list rule types referencing data source %s: %w", id, err)
}

// Check if the data source is in use by any rule types
if len(ret) > 0 {
// Return an error with the rule types that are using the data source
var existingRefs []string
for _, r := range ret {
existingRefs = append(existingRefs, r.RuleTypeID.String())
}
return util.UserVisibleError(codes.FailedPrecondition,
"data source %s is in use by the following rule types: %v", id, existingRefs)
}

// Delete the data source record
_, err = tx.DeleteDataSource(ctx, db.DeleteDataSourceParams{
ID: id,
ProjectID: project,
})
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return util.UserVisibleError(codes.NotFound,
"data source with id %s not found in project %s", id, project)
}
return fmt.Errorf("failed to delete data source with id %s: %w", id, err)
}

// Commit the transaction
if err := stx.Commit(); err != nil {
return fmt.Errorf("failed to commit transaction: %w", err)
}
return nil
}

// nolint:revive // there is a TODO
Expand Down