diff --git a/server/store/datastore/org.go b/server/store/datastore/org.go index 5bd8f1c3c..5119fe650 100644 --- a/server/store/datastore/org.go +++ b/server/store/datastore/org.go @@ -48,7 +48,26 @@ func (s storage) OrgUpdate(org *model.Org) error { } func (s storage) OrgDelete(id int64) error { - return wrapDelete(s.engine.ID(id).Delete(new(model.Org))) + return s.orgDelete(s.engine.NewSession(), id) +} + +func (s storage) orgDelete(sess *xorm.Session, id int64) error { + if _, err := sess.Where("secret_org_id = ?", id).Delete(new(model.Secret)); err != nil { + return err + } + + var repos []*model.Repo + if err := sess.Where("repo_org_id = ?", id).Find(&repos); err != nil { + return err + } + + for _, repo := range repos { + if err := s.deleteRepo(sess, repo); err != nil { + return err + } + } + + return wrapDelete(sess.ID(id).Delete(new(model.Org))) } func (s storage) OrgFindByName(name string) (*model.Org, error) { diff --git a/server/store/datastore/org_test.go b/server/store/datastore/org_test.go index 3135a05ac..b4c5af691 100644 --- a/server/store/datastore/org_test.go +++ b/server/store/datastore/org_test.go @@ -23,7 +23,7 @@ import ( ) func TestOrgCRUD(t *testing.T) { - store, closer := newTestStore(t, new(model.Org), new(model.Repo)) + store, closer := newTestStore(t, new(model.Org), new(model.Repo), new(model.Secret), new(model.Config), new(model.Perm), new(model.Registry), new(model.Redirection), new(model.Pipeline)) defer closer() org1 := &model.Org{ diff --git a/server/store/datastore/repo.go b/server/store/datastore/repo.go index 6c4991ca8..89ff7a37c 100644 --- a/server/store/datastore/repo.go +++ b/server/store/datastore/repo.go @@ -91,13 +91,11 @@ func (s storage) UpdateRepo(repo *model.Repo) error { } func (s storage) DeleteRepo(repo *model.Repo) error { - const batchSize = perPage - sess := s.engine.NewSession() - defer sess.Close() - if err := sess.Begin(); err != nil { - return err - } + return s.deleteRepo(s.engine.NewSession(), repo) +} +func (s storage) deleteRepo(sess *xorm.Session, repo *model.Repo) error { + const batchSize = perPage if _, err := sess.Where("config_repo_id = ?", repo.ID).Delete(new(model.Config)); err != nil { return err } @@ -131,11 +129,7 @@ func (s storage) DeleteRepo(repo *model.Repo) error { } } - if err := wrapDelete(sess.ID(repo.ID).Delete(new(model.Repo))); err != nil { - return err - } - - return sess.Commit() + return wrapDelete(sess.ID(repo.ID).Delete(new(model.Repo))) } // RepoList list all repos where permissions for specific user are stored diff --git a/server/store/datastore/user.go b/server/store/datastore/user.go index 115ed5b0c..55c0f5bca 100644 --- a/server/store/datastore/user.go +++ b/server/store/datastore/user.go @@ -81,6 +81,10 @@ func (s storage) DeleteUser(user *model.User) error { return err } + if err := s.orgDelete(sess, user.OrgID); err != nil { + return err + } + if err := wrapDelete(sess.ID(user.ID).Delete(new(model.User))); err != nil { return err } diff --git a/server/store/datastore/users_test.go b/server/store/datastore/users_test.go index 6779b2398..fe443efa2 100644 --- a/server/store/datastore/users_test.go +++ b/server/store/datastore/users_test.go @@ -24,7 +24,7 @@ import ( ) func TestUsers(t *testing.T) { - store, closer := newTestStore(t, new(model.User), new(model.Repo), new(model.Pipeline), new(model.Step), new(model.Perm), new(model.Org)) + store, closer := newTestStore(t, new(model.User), new(model.Repo), new(model.Pipeline), new(model.Step), new(model.Perm), new(model.Org), new(model.Secret)) defer closer() g := goblin.Goblin(t) diff --git a/web/src/assets/locales/en.json b/web/src/assets/locales/en.json index da94a3de3..b13ef7790 100644 --- a/web/src/assets/locales/en.json +++ b/web/src/assets/locales/en.json @@ -412,7 +412,7 @@ "show": "Show users", "add": "Add user", "none": "There are no users yet.", - "delete_confirm": "Do you really want to delete this user?", + "delete_confirm": "Do you really want to delete this user? This will also delete all repositories owned by this user.", "deleted": "User deleted", "created": "User created", "saved": "User saved", @@ -430,7 +430,7 @@ "org_settings": "Organization settings", "delete_org": "Delete organization", "deleted": "Organization deleted", - "delete_confirm": "Do you really want to delete this organization?", + "delete_confirm": "Do you really want to delete this organization? This will also delete all repositories owned by this organization.", "view": "View organization" } }