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

Delete profiles before deleting project #3561

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions database/mock/store.go

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

5 changes: 4 additions & 1 deletion database/query/profiles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ JOIN entity_profile_rules ON entity_profiles.id = entity_profile_rules.entity_pr
WHERE entity_profile_rules.rule_type_id = $1
GROUP BY profiles.id;


-- name: CountProfilesByEntityType :many
SELECT COUNT(p.id) AS num_profiles, ep.entity AS profile_entity
FROM profiles AS p
Expand All @@ -115,3 +114,7 @@ GROUP BY ep.entity;

-- name: CountProfilesByName :one
SELECT COUNT(*) AS num_named_profiles FROM profiles WHERE lower(name) = lower(sqlc.arg(name));

-- used when cleaning up a project to avoid FK dependency issues between rule_types and rule_instances
-- name: DeleteProfilesInProject :exec
DELETE FROM profiles WHERE project_id = $1;
10 changes: 10 additions & 0 deletions internal/db/profiles.sql.go

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

2 changes: 2 additions & 0 deletions internal/db/querier.go

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

7 changes: 7 additions & 0 deletions internal/projects/deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ func (p *projectDeleter) DeleteProject(
}
}

// Delete the project's profiles. This avoids foreign key issues with
// rule_instances when deleting rows from rule_type.
err = querier.DeleteProfilesInProject(ctx, proj)
if err != nil {
return fmt.Errorf("unable to delete profiles: %w", err)
}

// no role assignments for this project
// we can safely delete it.
l.Debug().Msg("deleting project from database")
Expand Down
6 changes: 6 additions & 0 deletions internal/projects/deleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestDeleteProjectOneProjectWithNoParents(t *testing.T) {
mockStore := mockdb.NewMockStore(ctrl)
mockStore.EXPECT().GetProjectByID(gomock.Any(), proj).Return(
db.Project{ID: proj}, nil)
mockStore.EXPECT().DeleteProfilesInProject(gomock.Any(), proj)
mockStore.EXPECT().DeleteProject(gomock.Any(), proj).
Return([]db.DeleteProjectRow{
{ID: proj},
Expand Down Expand Up @@ -90,6 +91,7 @@ func TestDeleteProjectWithOneParent(t *testing.T) {
}, nil)
mockStore.EXPECT().ListProvidersByProjectID(gomock.Any(), []uuid.UUID{proj}).
Return([]db.Provider{}, nil)
mockStore.EXPECT().DeleteProfilesInProject(gomock.Any(), proj)
mockStore.EXPECT().DeleteProject(gomock.Any(), proj).
Return([]db.DeleteProjectRow{
{
Expand Down Expand Up @@ -141,6 +143,7 @@ func TestDeleteProjectProjectInThreeNodeHierarchy(t *testing.T) {
}, nil)
mockStore.EXPECT().ListProvidersByProjectID(gomock.Any(), []uuid.UUID{proj}).
Return([]db.Provider{}, nil)
mockStore.EXPECT().DeleteProfilesInProject(gomock.Any(), proj)
mockStore.EXPECT().DeleteProject(gomock.Any(), proj).
Return([]db.DeleteProjectRow{
{
Expand Down Expand Up @@ -198,6 +201,7 @@ func TestDeleteMiddleProjectInThreeNodeHierarchy(t *testing.T) {
}, nil)
mockStore.EXPECT().ListProvidersByProjectID(gomock.Any(), []uuid.UUID{proj}).
Return([]db.Provider{}, nil)
mockStore.EXPECT().DeleteProfilesInProject(gomock.Any(), proj).Return(nil)
mockStore.EXPECT().DeleteProject(gomock.Any(), proj).
Return([]db.DeleteProjectRow{
{
Expand Down Expand Up @@ -247,6 +251,7 @@ func TestDeleteProjectWithProvider(t *testing.T) {
mockStore := mockdb.NewMockStore(ctrl)
mockStore.EXPECT().GetProjectByID(gomock.Any(), proj).Return(
db.Project{ID: proj}, nil)
mockStore.EXPECT().DeleteProfilesInProject(gomock.Any(), proj)
mockStore.EXPECT().DeleteProject(gomock.Any(), proj).
Return([]db.DeleteProjectRow{
{ID: proj},
Expand Down Expand Up @@ -302,6 +307,7 @@ func TestCleanupUnmanaged(t *testing.T) {
mockStore.EXPECT().GetProjectByID(gomock.Any(), projThree).Return(
db.Project{ID: projThree}, nil).Times(2)
// Project 3 has no other admins, so it will be deleted.
mockStore.EXPECT().DeleteProfilesInProject(gomock.Any(), projThree).Return(nil)
mockStore.EXPECT().DeleteProject(gomock.Any(), projThree).Return(
[]db.DeleteProjectRow{
{ID: projThree},
Expand Down