@@ -48,39 +48,40 @@ func deleteOrganization(ctx context.Context, org *org_model.Organization) error
4848
4949// DeleteOrganization completely and permanently deletes everything of organization.
5050func DeleteOrganization (ctx context.Context , org * org_model.Organization , purge bool ) error {
51- ctx , committer , err := db .TxContext (ctx )
52- if err != nil {
53- return err
54- }
55- defer committer .Close ()
56-
51+ // The repositories deletion of the organization cannot be under a transaction,
52+ // because it cannot be rolled back because the content in disk will be deleted
53+ // in the DeleteOwnerRepositoriesDirectly function.
54+ // Even not all repositories deleted successfully, we still delete the organization again.
55+ // TODO: We should mark all the repositories as deleted and delete them in a background job.
5756 if purge {
5857 err := repo_service .DeleteOwnerRepositoriesDirectly (ctx , org .AsUser ())
5958 if err != nil {
6059 return err
6160 }
6261 }
6362
64- // Check ownership of repository.
65- count , err := repo_model .CountRepositories (ctx , repo_model.CountRepositoryOptions {OwnerID : org .ID })
66- if err != nil {
67- return fmt .Errorf ("GetRepositoryCount: %w" , err )
68- } else if count > 0 {
69- return repo_model.ErrUserOwnRepos {UID : org .ID }
70- }
71-
72- // Check ownership of packages.
73- if ownsPackages , err := packages_model .HasOwnerPackages (ctx , org .ID ); err != nil {
74- return fmt .Errorf ("HasOwnerPackages: %w" , err )
75- } else if ownsPackages {
76- return packages_model.ErrUserOwnPackages {UID : org .ID }
77- }
63+ err := db .WithTx (ctx , func (ctx context.Context ) error {
64+ // Check ownership of repository.
65+ count , err := repo_model .CountRepositories (ctx , repo_model.CountRepositoryOptions {OwnerID : org .ID })
66+ if err != nil {
67+ return fmt .Errorf ("GetRepositoryCount: %w" , err )
68+ } else if count > 0 {
69+ return repo_model.ErrUserOwnRepos {UID : org .ID }
70+ }
7871
79- if err := deleteOrganization (ctx , org ); err != nil {
80- return fmt .Errorf ("DeleteOrganization: %w" , err )
81- }
72+ // Check ownership of packages.
73+ if ownsPackages , err := packages_model .HasOwnerPackages (ctx , org .ID ); err != nil {
74+ return fmt .Errorf ("HasOwnerPackages: %w" , err )
75+ } else if ownsPackages {
76+ return packages_model.ErrUserOwnPackages {UID : org .ID }
77+ }
8278
83- if err := committer .Commit (); err != nil {
79+ if err := deleteOrganization (ctx , org ); err != nil {
80+ return fmt .Errorf ("DeleteOrganization: %w" , err )
81+ }
82+ return nil
83+ })
84+ if err != nil {
8485 return err
8586 }
8687
0 commit comments