diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-02-17 16:37:48 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-17 16:37:48 +0800 |
commit | 397d2ac303d7dd34295537a5776bb77903100715 (patch) | |
tree | 702edfc191527b78731022d24f1eb2d4b84622eb /models | |
parent | bd71245c53275d5c8ffcd231b9e0f6fcd7b5dc80 (diff) | |
download | gitea-397d2ac303d7dd34295537a5776bb77903100715.tar.gz gitea-397d2ac303d7dd34295537a5776bb77903100715.zip |
Move deletebeans into models/db (#18781)
Diffstat (limited to 'models')
-rw-r--r-- | models/db/context.go | 11 | ||||
-rw-r--r-- | models/org.go | 6 | ||||
-rw-r--r-- | models/repo.go | 2 | ||||
-rw-r--r-- | models/user.go | 12 |
4 files changed, 15 insertions, 16 deletions
diff --git a/models/db/context.go b/models/db/context.go index 833c26ff6c..1cd23d453c 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -148,6 +148,17 @@ func DeleteByBean(ctx context.Context, bean interface{}) (int64, error) { return GetEngine(ctx).Delete(bean) } +// DeleteBeans deletes all given beans, beans should contain delete conditions. +func DeleteBeans(ctx context.Context, beans ...interface{}) (err error) { + e := GetEngine(ctx) + for i := range beans { + if _, err = e.Delete(beans[i]); err != nil { + return err + } + } + return nil +} + // CountByBean counts the number of database records according non-empty fields of the bean as conditions. func CountByBean(ctx context.Context, bean interface{}) (int64, error) { return GetEngine(ctx).Count(bean) diff --git a/models/org.go b/models/org.go index 0ea2ce6886..70b3fe27b9 100644 --- a/models/org.go +++ b/models/org.go @@ -331,9 +331,7 @@ func DeleteOrganization(ctx context.Context, org *Organization) error { return fmt.Errorf("%s is a user not an organization", org.Name) } - e := db.GetEngine(ctx) - - if err := deleteBeans(e, + if err := db.DeleteBeans(ctx, &Team{OrgID: org.ID}, &OrgUser{OrgID: org.ID}, &TeamUser{OrgID: org.ID}, @@ -342,7 +340,7 @@ func DeleteOrganization(ctx context.Context, org *Organization) error { return fmt.Errorf("deleteBeans: %v", err) } - if _, err := e.ID(org.ID).Delete(new(user_model.User)); err != nil { + if _, err := db.GetEngine(ctx).ID(org.ID).Delete(new(user_model.User)); err != nil { return fmt.Errorf("Delete: %v", err) } diff --git a/models/repo.go b/models/repo.go index 2a857c43df..7b0635074c 100644 --- a/models/repo.go +++ b/models/repo.go @@ -765,7 +765,7 @@ func DeleteRepository(doer *user_model.User, uid, repoID int64) error { return err } - if err := deleteBeans(sess, + if err := db.DeleteBeans(ctx, &Access{RepoID: repo.ID}, &Action{RepoID: repo.ID}, &Collaboration{RepoID: repoID}, diff --git a/models/user.go b/models/user.go index 5f7bedd36d..443e0c0c8b 100644 --- a/models/user.go +++ b/models/user.go @@ -29,16 +29,6 @@ func GetOrganizationCount(ctx context.Context, u *user_model.User) (int64, error Count(new(OrgUser)) } -// deleteBeans deletes all given beans, beans should contain delete conditions. -func deleteBeans(e db.Engine, beans ...interface{}) (err error) { - for i := range beans { - if _, err = e.Delete(beans[i]); err != nil { - return err - } - } - return nil -} - // DeleteUser deletes models associated to an user. func DeleteUser(ctx context.Context, u *user_model.User) (err error) { e := db.GetEngine(ctx) @@ -82,7 +72,7 @@ func DeleteUser(ctx context.Context, u *user_model.User) (err error) { } // ***** END: Follow ***** - if err = deleteBeans(e, + if err = db.DeleteBeans(ctx, &AccessToken{UID: u.ID}, &Collaboration{UserID: u.ID}, &Access{UserID: u.ID}, |