mirror of
https://github.com/go-gitea/gitea.git
synced 2026-02-14 18:47:20 +01:00
Backport #36500 by @lunny Fix #36448 Removed unnecessary parameters from the LFS GC process and switched to an ORDER BY id ASC strategy with a last-ID cursor to avoid missing or duplicating meta object IDs. Signed-off-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
git_model "code.gitea.io/gitea/models/git"
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
"code.gitea.io/gitea/models/unittest"
|
|
"code.gitea.io/gitea/modules/lfs"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/test"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestIterateLFSMetaObjectsForRepoUpdatesDoNotSkip(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
ctx := t.Context()
|
|
repo, err := repo_model.GetRepositoryByOwnerAndName(ctx, "user2", "repo1")
|
|
assert.NoError(t, err)
|
|
|
|
defer test.MockVariableValue(&setting.Database.IterateBufferSize, 1)()
|
|
|
|
created := make([]*git_model.LFSMetaObject, 0, 3)
|
|
for i := range 3 {
|
|
content := []byte("gitea-lfs-" + strconv.Itoa(i))
|
|
pointer, err := lfs.GeneratePointer(bytes.NewReader(content))
|
|
assert.NoError(t, err)
|
|
|
|
meta, err := git_model.NewLFSMetaObject(ctx, repo.ID, pointer)
|
|
assert.NoError(t, err)
|
|
created = append(created, meta)
|
|
}
|
|
|
|
iterated := make([]int64, 0, len(created))
|
|
cutoff := time.Now().Add(24 * time.Hour)
|
|
iterErr := git_model.IterateLFSMetaObjectsForRepo(ctx, repo.ID, func(ctx context.Context, meta *git_model.LFSMetaObject, count int64) error {
|
|
iterated = append(iterated, meta.ID)
|
|
_, err := db.GetEngine(ctx).ID(meta.ID).Cols("updated_unix").Update(&git_model.LFSMetaObject{
|
|
UpdatedUnix: timeutil.TimeStamp(time.Now().Unix()),
|
|
})
|
|
return err
|
|
}, &git_model.IterateLFSMetaObjectsForRepoOptions{
|
|
OlderThan: timeutil.TimeStamp(cutoff.Unix()),
|
|
UpdatedLessRecentlyThan: timeutil.TimeStamp(cutoff.Unix()),
|
|
})
|
|
assert.NoError(t, iterErr)
|
|
|
|
expected := []int64{created[0].ID, created[1].ID, created[2].ID}
|
|
assert.Equal(t, expected, iterated)
|
|
}
|