Move deletebeans into models/db (#18781)

This commit is contained in:
Lunny Xiao 2022-02-17 16:37:48 +08:00 committed by GitHub
parent bd71245c53
commit 397d2ac303
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 16 deletions

View File

@ -148,6 +148,17 @@ func DeleteByBean(ctx context.Context, bean interface{}) (int64, error) {
return GetEngine(ctx).Delete(bean) 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. // 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) { func CountByBean(ctx context.Context, bean interface{}) (int64, error) {
return GetEngine(ctx).Count(bean) return GetEngine(ctx).Count(bean)

View File

@ -331,9 +331,7 @@ func DeleteOrganization(ctx context.Context, org *Organization) error {
return fmt.Errorf("%s is a user not an organization", org.Name) return fmt.Errorf("%s is a user not an organization", org.Name)
} }
e := db.GetEngine(ctx) if err := db.DeleteBeans(ctx,
if err := deleteBeans(e,
&Team{OrgID: org.ID}, &Team{OrgID: org.ID},
&OrgUser{OrgID: org.ID}, &OrgUser{OrgID: org.ID},
&TeamUser{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) 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) return fmt.Errorf("Delete: %v", err)
} }

View File

@ -765,7 +765,7 @@ func DeleteRepository(doer *user_model.User, uid, repoID int64) error {
return err return err
} }
if err := deleteBeans(sess, if err := db.DeleteBeans(ctx,
&Access{RepoID: repo.ID}, &Access{RepoID: repo.ID},
&Action{RepoID: repo.ID}, &Action{RepoID: repo.ID},
&Collaboration{RepoID: repoID}, &Collaboration{RepoID: repoID},

View File

@ -29,16 +29,6 @@ func GetOrganizationCount(ctx context.Context, u *user_model.User) (int64, error
Count(new(OrgUser)) 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. // DeleteUser deletes models associated to an user.
func DeleteUser(ctx context.Context, u *user_model.User) (err error) { func DeleteUser(ctx context.Context, u *user_model.User) (err error) {
e := db.GetEngine(ctx) e := db.GetEngine(ctx)
@ -82,7 +72,7 @@ func DeleteUser(ctx context.Context, u *user_model.User) (err error) {
} }
// ***** END: Follow ***** // ***** END: Follow *****
if err = deleteBeans(e, if err = db.DeleteBeans(ctx,
&AccessToken{UID: u.ID}, &AccessToken{UID: u.ID},
&Collaboration{UserID: u.ID}, &Collaboration{UserID: u.ID},
&Access{UserID: u.ID}, &Access{UserID: u.ID},