Skip to content

Commit

Permalink
Move deletebeans into models/db (go-gitea#18781)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored and Stelios Malathouras committed Mar 28, 2022
1 parent 314c4ed commit 8ff1bb2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
11 changes: 11 additions & 0 deletions models/db/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
12 changes: 1 addition & 11 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit 8ff1bb2

Please sign in to comment.