diff --git a/pkg/services/org/orgimpl/store.go b/pkg/services/org/orgimpl/store.go index d56f941e34c..5d247ffeb9b 100644 --- a/pkg/services/org/orgimpl/store.go +++ b/pkg/services/org/orgimpl/store.go @@ -221,6 +221,8 @@ func (ss *sqlStore) Delete(ctx context.Context, cmd *org.DeleteOrgCommand) error deletes := []string{ "DELETE FROM star WHERE org_id = ?", + "DELETE FROM playlist_item WHERE playlist_id IN (SELECT id FROM playlist WHERE org_id = ?)", + "DELETE FROM playlist WHERE org_id = ?", "DELETE FROM dashboard_tag WHERE org_id = ?", "DELETE FROM api_key WHERE org_id = ?", "DELETE FROM data_source WHERE org_id = ?", diff --git a/pkg/services/org/orgimpl/store_test.go b/pkg/services/org/orgimpl/store_test.go index a4f839d6842..abc4e217418 100644 --- a/pkg/services/org/orgimpl/store_test.go +++ b/pkg/services/org/orgimpl/store_test.go @@ -15,6 +15,8 @@ import ( "github.com/grafana/grafana/pkg/infra/tracing" "github.com/grafana/grafana/pkg/services/accesscontrol" "github.com/grafana/grafana/pkg/services/org" + "github.com/grafana/grafana/pkg/services/playlist" + "github.com/grafana/grafana/pkg/services/playlist/playlistimpl" "github.com/grafana/grafana/pkg/services/quota/quotaimpl" "github.com/grafana/grafana/pkg/services/searchusers/sortopts" "github.com/grafana/grafana/pkg/services/sqlstore" @@ -115,11 +117,32 @@ func TestIntegrationOrgDataAccess(t *testing.T) { t.Run("Removing org", func(t *testing.T) { // make sure ac2 has no org ac2 := &org.Org{ID: 22, Name: "ac2", Version: 1, Created: time.Now(), Updated: time.Now()} - _, err := orgStore.Insert(context.Background(), ac2) + orgId, err := orgStore.Insert(context.Background(), ac2) + require.NoError(t, err) + + // Create some org-scoped items like playlists, so we can assert that they + // are cleaned up on delete. + plItems := []playlist.PlaylistItem{ + { + Type: "foo", + }, + } + plStore := playlistimpl.ProvideService(ss, tracing.InitializeTracerForTest()) + plCreateCommand := playlist.CreatePlaylistCommand{ + OrgId: orgId, + Name: "test", + Items: plItems, + } + pl, err := plStore.Create(context.Background(), &plCreateCommand) require.NoError(t, err) + err = orgStore.Delete(context.Background(), &org.DeleteOrgCommand{ID: ac2.ID}) require.NoError(t, err) + plDTO, err := plStore.Get(context.Background(), &playlist.GetPlaylistByUidQuery{OrgId: pl.OrgId, UID: pl.UID}) + require.Error(t, err) + require.Nil(t, plDTO) + // TODO: this part of the test will be added when we move RemoveOrgUser to org store // "Removing user from org should delete user completely if in no other org" // // remove ac2 user from ac1 org