Refactor deletion (#28610)
Introduce the new generic deletion methods - `func DeleteByID[T any](ctx context.Context, id int64) (int64, error)` - `func DeleteByIDs[T any](ctx context.Context, ids ...int64) error` - `func Delete[T any](ctx context.Context, opts FindOptions) (int64, error)` So, we no longer need any specific deletion method and can just use the generic ones instead. Replacement of #28450 Closes #28450 --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
parent
b41925cee3
commit
778ad795fd
31 changed files with 89 additions and 169 deletions
|
@ -68,14 +68,6 @@ func repoArchiverForRelativePath(relativePath string) (*RepoArchiver, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
var delRepoArchiver = new(RepoArchiver)
|
||||
|
||||
// DeleteRepoArchiver delete archiver
|
||||
func DeleteRepoArchiver(ctx context.Context, archiver *RepoArchiver) error {
|
||||
_, err := db.GetEngine(ctx).ID(archiver.ID).Delete(delRepoArchiver)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetRepoArchiver get an archiver
|
||||
func GetRepoArchiver(ctx context.Context, repoID int64, tp git.ArchiveType, commitID string) (*RepoArchiver, error) {
|
||||
var archiver RepoArchiver
|
||||
|
@ -100,12 +92,6 @@ func ExistsRepoArchiverWithStoragePath(ctx context.Context, storagePath string)
|
|||
return db.GetEngine(ctx).Exist(archiver)
|
||||
}
|
||||
|
||||
// AddRepoArchiver adds an archiver
|
||||
func AddRepoArchiver(ctx context.Context, archiver *RepoArchiver) error {
|
||||
_, err := db.GetEngine(ctx).Insert(archiver)
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateRepoArchiverStatus updates archiver's status
|
||||
func UpdateRepoArchiverStatus(ctx context.Context, archiver *RepoArchiver) error {
|
||||
_, err := db.GetEngine(ctx).ID(archiver.ID).Cols("status").Update(archiver)
|
||||
|
@ -114,6 +100,7 @@ func UpdateRepoArchiverStatus(ctx context.Context, archiver *RepoArchiver) error
|
|||
|
||||
// DeleteAllRepoArchives deletes all repo archives records
|
||||
func DeleteAllRepoArchives(ctx context.Context) error {
|
||||
// 1=1 to enforce delete all data, otherwise it will delete nothing
|
||||
_, err := db.GetEngine(ctx).Where("1=1").Delete(new(RepoArchiver))
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -34,12 +34,13 @@ type PushMirror struct {
|
|||
}
|
||||
|
||||
type PushMirrorOptions struct {
|
||||
db.ListOptions
|
||||
ID int64
|
||||
RepoID int64
|
||||
RemoteName string
|
||||
}
|
||||
|
||||
func (opts *PushMirrorOptions) toConds() builder.Cond {
|
||||
func (opts PushMirrorOptions) ToConds() builder.Cond {
|
||||
cond := builder.NewCond()
|
||||
if opts.RepoID > 0 {
|
||||
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
|
||||
|
@ -75,12 +76,6 @@ func (m *PushMirror) GetRemoteName() string {
|
|||
return m.RemoteName
|
||||
}
|
||||
|
||||
// InsertPushMirror inserts a push-mirror to database
|
||||
func InsertPushMirror(ctx context.Context, m *PushMirror) error {
|
||||
_, err := db.GetEngine(ctx).Insert(m)
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdatePushMirror updates the push-mirror
|
||||
func UpdatePushMirror(ctx context.Context, m *PushMirror) error {
|
||||
_, err := db.GetEngine(ctx).ID(m.ID).AllCols().Update(m)
|
||||
|
@ -95,23 +90,12 @@ func UpdatePushMirrorInterval(ctx context.Context, m *PushMirror) error {
|
|||
|
||||
func DeletePushMirrors(ctx context.Context, opts PushMirrorOptions) error {
|
||||
if opts.RepoID > 0 {
|
||||
_, err := db.GetEngine(ctx).Where(opts.toConds()).Delete(&PushMirror{})
|
||||
_, err := db.Delete[PushMirror](ctx, opts)
|
||||
return err
|
||||
}
|
||||
return util.NewInvalidArgumentErrorf("repoID required and must be set")
|
||||
}
|
||||
|
||||
func GetPushMirror(ctx context.Context, opts PushMirrorOptions) (*PushMirror, error) {
|
||||
mirror := &PushMirror{}
|
||||
exist, err := db.GetEngine(ctx).Where(opts.toConds()).Get(mirror)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !exist {
|
||||
return nil, ErrPushMirrorNotExist
|
||||
}
|
||||
return mirror, nil
|
||||
}
|
||||
|
||||
// GetPushMirrorsByRepoID returns push-mirror information of a repository.
|
||||
func GetPushMirrorsByRepoID(ctx context.Context, repoID int64, listOptions db.ListOptions) ([]*PushMirror, int64, error) {
|
||||
sess := db.GetEngine(ctx).Where("repo_id = ?", repoID)
|
||||
|
|
|
@ -20,20 +20,20 @@ func TestPushMirrorsIterate(t *testing.T) {
|
|||
|
||||
now := timeutil.TimeStampNow()
|
||||
|
||||
repo_model.InsertPushMirror(db.DefaultContext, &repo_model.PushMirror{
|
||||
db.Insert(db.DefaultContext, &repo_model.PushMirror{
|
||||
RemoteName: "test-1",
|
||||
LastUpdateUnix: now,
|
||||
Interval: 1,
|
||||
})
|
||||
|
||||
long, _ := time.ParseDuration("24h")
|
||||
repo_model.InsertPushMirror(db.DefaultContext, &repo_model.PushMirror{
|
||||
db.Insert(db.DefaultContext, &repo_model.PushMirror{
|
||||
RemoteName: "test-2",
|
||||
LastUpdateUnix: now,
|
||||
Interval: long,
|
||||
})
|
||||
|
||||
repo_model.InsertPushMirror(db.DefaultContext, &repo_model.PushMirror{
|
||||
db.Insert(db.DefaultContext, &repo_model.PushMirror{
|
||||
RemoteName: "test-3",
|
||||
LastUpdateUnix: now,
|
||||
Interval: 0,
|
||||
|
|
|
@ -450,12 +450,6 @@ func SortReleases(rels []*Release) {
|
|||
sort.Sort(sorter)
|
||||
}
|
||||
|
||||
// DeleteReleaseByID deletes a release from database by given ID.
|
||||
func DeleteReleaseByID(ctx context.Context, id int64) error {
|
||||
_, err := db.GetEngine(ctx).ID(id).Delete(new(Release))
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateReleasesMigrationsByType updates all migrated repositories' releases from gitServiceType to replace originalAuthorID to posterID
|
||||
func UpdateReleasesMigrationsByType(ctx context.Context, gitServiceType structs.GitServiceType, originalAuthorID string, posterID int64) error {
|
||||
_, err := db.GetEngine(ctx).Table("release").
|
||||
|
@ -509,7 +503,7 @@ func PushUpdateDeleteTag(ctx context.Context, repo *Repository, tagName string)
|
|||
return fmt.Errorf("GetRelease: %w", err)
|
||||
}
|
||||
if rel.IsTag {
|
||||
if _, err = db.GetEngine(ctx).ID(rel.ID).Delete(new(Release)); err != nil {
|
||||
if _, err = db.DeleteByID[Release](ctx, rel.ID); err != nil {
|
||||
return fmt.Errorf("Delete: %w", err)
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -131,9 +131,7 @@ func DeleteUploads(ctx context.Context, uploads ...*Upload) (err error) {
|
|||
for i := 0; i < len(uploads); i++ {
|
||||
ids[i] = uploads[i].ID
|
||||
}
|
||||
if _, err = db.GetEngine(ctx).
|
||||
In("id", ids).
|
||||
Delete(new(Upload)); err != nil {
|
||||
if err = db.DeleteByIDs[Upload](ctx, ids...); err != nil {
|
||||
return fmt.Errorf("delete uploads: %w", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -85,23 +85,21 @@ func watchRepoMode(ctx context.Context, watch Watch, mode WatchMode) (err error)
|
|||
|
||||
watch.Mode = mode
|
||||
|
||||
e := db.GetEngine(ctx)
|
||||
|
||||
if !hadrec && needsrec {
|
||||
watch.Mode = mode
|
||||
if _, err = e.Insert(watch); err != nil {
|
||||
if err = db.Insert(ctx, watch); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if needsrec {
|
||||
watch.Mode = mode
|
||||
if _, err := e.ID(watch.ID).AllCols().Update(watch); err != nil {
|
||||
if _, err := db.GetEngine(ctx).ID(watch.ID).AllCols().Update(watch); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if _, err = e.Delete(Watch{ID: watch.ID}); err != nil {
|
||||
} else if _, err = db.DeleteByID[Watch](ctx, watch.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
if repodiff != 0 {
|
||||
_, err = e.Exec("UPDATE `repository` SET num_watches = num_watches + ? WHERE id = ?", repodiff, watch.RepoID)
|
||||
_, err = db.GetEngine(ctx).Exec("UPDATE `repository` SET num_watches = num_watches + ? WHERE id = ?", repodiff, watch.RepoID)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue