mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db * Fix lint * Fix lint * Fix test * Fix lint * Fix lint * revert unnecessary change * Fix test * Fix wrong replace string * Use *Context * Correct committer spelling and fix wrong replaced words Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
@@ -46,7 +47,7 @@ func AdoptRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mode
|
||||
IsEmpty: !opts.AutoInit,
|
||||
}
|
||||
|
||||
if err := models.WithTx(func(ctx models.DBContext) error {
|
||||
if err := db.WithTx(func(ctx *db.Context) error {
|
||||
repoPath := models.RepoPath(u.Name, repo.Name)
|
||||
isExist, err := util.IsExist(repoPath)
|
||||
if err != nil {
|
||||
|
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
@@ -22,8 +23,8 @@ import (
|
||||
func GitFsck(ctx context.Context, timeout time.Duration, args []string) error {
|
||||
log.Trace("Doing: GitFsck")
|
||||
|
||||
if err := models.Iterate(
|
||||
models.DefaultDBContext(),
|
||||
if err := db.Iterate(
|
||||
db.DefaultContext(),
|
||||
new(models.Repository),
|
||||
builder.Expr("id>0 AND is_fsck_enabled=?", true),
|
||||
func(idx int, bean interface{}) error {
|
||||
@@ -57,8 +58,8 @@ func GitGcRepos(ctx context.Context, timeout time.Duration, args ...string) erro
|
||||
log.Trace("Doing: GitGcRepos")
|
||||
args = append([]string{"gc"}, args...)
|
||||
|
||||
if err := models.Iterate(
|
||||
models.DefaultDBContext(),
|
||||
if err := db.Iterate(
|
||||
db.DefaultContext(),
|
||||
new(models.Repository),
|
||||
builder.Gt{"id": 0},
|
||||
func(idx int, bean interface{}) error {
|
||||
@@ -93,7 +94,7 @@ func GitGcRepos(ctx context.Context, timeout time.Duration, args ...string) erro
|
||||
}
|
||||
|
||||
// Now update the size of the repository
|
||||
if err := repo.UpdateSize(models.DefaultDBContext()); err != nil {
|
||||
if err := repo.UpdateSize(db.DefaultContext()); err != nil {
|
||||
log.Error("Updating size as part of garbage collection failed for %v. Stdout: %s\nError: %v", repo, stdout, err)
|
||||
desc := fmt.Sprintf("Updating size as part of garbage collection failed for %s. Stdout: %s\nError: %v", repo.RepoPath(), stdout, err)
|
||||
if err = models.CreateRepositoryNotice(desc); err != nil {
|
||||
@@ -114,8 +115,8 @@ func GitGcRepos(ctx context.Context, timeout time.Duration, args ...string) erro
|
||||
|
||||
func gatherMissingRepoRecords(ctx context.Context) ([]*models.Repository, error) {
|
||||
repos := make([]*models.Repository, 0, 10)
|
||||
if err := models.Iterate(
|
||||
models.DefaultDBContext(),
|
||||
if err := db.Iterate(
|
||||
db.DefaultContext(),
|
||||
new(models.Repository),
|
||||
builder.Gt{"id": 0},
|
||||
func(idx int, bean interface{}) error {
|
||||
|
@@ -11,12 +11,13 @@ import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
|
||||
assert.NoError(t, models.PrepareTestDatabase())
|
||||
assert.NoError(t, db.PrepareTestDatabase())
|
||||
|
||||
pushCommits := NewPushCommits()
|
||||
pushCommits.Commits = []*PushCommit{
|
||||
@@ -47,7 +48,7 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
|
||||
}
|
||||
pushCommits.HeadCommit = &PushCommit{Sha1: "69554a6"}
|
||||
|
||||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository)
|
||||
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository)
|
||||
payloadCommits, headCommit, err := pushCommits.ToAPIPayloadCommits(repo.RepoPath(), "/user2/repo16")
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, payloadCommits, 3)
|
||||
@@ -99,7 +100,7 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPushCommits_AvatarLink(t *testing.T) {
|
||||
assert.NoError(t, models.PrepareTestDatabase())
|
||||
assert.NoError(t, db.PrepareTestDatabase())
|
||||
|
||||
pushCommits := NewPushCommits()
|
||||
pushCommits.Commits = []*PushCommit{
|
||||
|
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
@@ -54,7 +55,7 @@ func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mod
|
||||
|
||||
var rollbackRepo *models.Repository
|
||||
|
||||
if err := models.WithTx(func(ctx models.DBContext) error {
|
||||
if err := db.WithTx(func(ctx *db.Context) error {
|
||||
if err := models.CreateRepository(ctx, doer, u, repo, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -9,16 +9,17 @@ import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIncludesAllRepositoriesTeams(t *testing.T) {
|
||||
assert.NoError(t, models.PrepareTestDatabase())
|
||||
assert.NoError(t, db.PrepareTestDatabase())
|
||||
|
||||
testTeamRepositories := func(teamID int64, repoIds []int64) {
|
||||
team := models.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team)
|
||||
team := db.AssertExistsAndLoadBean(t, &models.Team{ID: teamID}).(*models.Team)
|
||||
assert.NoError(t, team.GetRepositories(&models.SearchTeamOptions{}), "%s: GetRepositories", team.Name)
|
||||
assert.Len(t, team.Repos, team.NumRepos, "%s: len repo", team.Name)
|
||||
assert.Len(t, team.Repos, len(repoIds), "%s: repo count", team.Name)
|
||||
|
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
@@ -78,7 +79,7 @@ func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *m
|
||||
panic(panicErr)
|
||||
}()
|
||||
|
||||
err = models.WithTx(func(ctx models.DBContext) error {
|
||||
err = db.WithTx(func(ctx *db.Context) error {
|
||||
if err = models.CreateRepository(ctx, doer, owner, repo, false); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -122,7 +123,7 @@ func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *m
|
||||
}
|
||||
|
||||
// even if below operations failed, it could be ignored. And they will be retried
|
||||
ctx := models.DefaultDBContext()
|
||||
ctx := db.DefaultContext()
|
||||
if err := repo.UpdateSize(ctx); err != nil {
|
||||
log.Error("Failed to update size for repository: %v", err)
|
||||
}
|
||||
@@ -135,7 +136,7 @@ func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *m
|
||||
|
||||
// ConvertForkToNormalRepository convert the provided repo from a forked repo to normal repo
|
||||
func ConvertForkToNormalRepository(repo *models.Repository) error {
|
||||
err := models.WithTx(func(ctx models.DBContext) error {
|
||||
err := db.WithTx(func(ctx *db.Context) error {
|
||||
repo, err := models.GetRepositoryByIDCtx(ctx, repo.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@@ -8,15 +8,16 @@ import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestForkRepository(t *testing.T) {
|
||||
assert.NoError(t, models.PrepareTestDatabase())
|
||||
assert.NoError(t, db.PrepareTestDatabase())
|
||||
|
||||
// user 13 has already forked repo10
|
||||
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 13}).(*models.User)
|
||||
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository)
|
||||
user := db.AssertExistsAndLoadBean(t, &models.User{ID: 13}).(*models.User)
|
||||
repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 10}).(*models.Repository)
|
||||
|
||||
fork, err := ForkRepository(user, user, models.ForkRepoOptions{
|
||||
BaseRepo: repo,
|
||||
|
@@ -14,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
@@ -185,7 +186,7 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp
|
||||
return initRepoCommit(tmpDir, repo, repo.Owner, templateRepo.DefaultBranch)
|
||||
}
|
||||
|
||||
func generateGitContent(ctx models.DBContext, repo, templateRepo, generateRepo *models.Repository) (err error) {
|
||||
func generateGitContent(ctx *db.Context, repo, templateRepo, generateRepo *models.Repository) (err error) {
|
||||
tmpDir, err := ioutil.TempDir(os.TempDir(), "gitea-"+repo.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create temp dir for repository %s: %v", repo.RepoPath(), err)
|
||||
@@ -223,7 +224,7 @@ func generateGitContent(ctx models.DBContext, repo, templateRepo, generateRepo *
|
||||
}
|
||||
|
||||
// GenerateGitContent generates git content from a template repository
|
||||
func GenerateGitContent(ctx models.DBContext, templateRepo, generateRepo *models.Repository) error {
|
||||
func GenerateGitContent(ctx *db.Context, templateRepo, generateRepo *models.Repository) error {
|
||||
if err := generateGitContent(ctx, generateRepo, templateRepo, generateRepo); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -239,7 +240,7 @@ func GenerateGitContent(ctx models.DBContext, templateRepo, generateRepo *models
|
||||
}
|
||||
|
||||
// GenerateRepository generates a repository from a template
|
||||
func GenerateRepository(ctx models.DBContext, doer, owner *models.User, templateRepo *models.Repository, opts models.GenerateRepoOptions) (_ *models.Repository, err error) {
|
||||
func GenerateRepository(ctx *db.Context, doer, owner *models.User, templateRepo *models.Repository, opts models.GenerateRepoOptions) (_ *models.Repository, err error) {
|
||||
generateRepo := &models.Repository{
|
||||
OwnerID: owner.ID,
|
||||
Owner: owner,
|
||||
|
@@ -12,6 +12,7 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
@@ -220,8 +221,8 @@ func CheckDelegateHooks(repoPath string) ([]string, error) {
|
||||
func SyncRepositoryHooks(ctx context.Context) error {
|
||||
log.Trace("Doing: SyncRepositoryHooks")
|
||||
|
||||
if err := models.Iterate(
|
||||
models.DefaultDBContext(),
|
||||
if err := db.Iterate(
|
||||
db.DefaultContext(),
|
||||
new(models.Repository),
|
||||
builder.Gt{"id": 0},
|
||||
func(idx int, bean interface{}) error {
|
||||
|
@@ -14,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
@@ -22,7 +23,7 @@ import (
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
|
||||
func prepareRepoCommit(ctx models.DBContext, repo *models.Repository, tmpDir, repoPath string, opts models.CreateRepoOptions) error {
|
||||
func prepareRepoCommit(ctx *db.Context, repo *models.Repository, tmpDir, repoPath string, opts models.CreateRepoOptions) error {
|
||||
commitTimeStr := time.Now().Format(time.RFC3339)
|
||||
authorSig := repo.Owner.NewGitSig()
|
||||
|
||||
@@ -196,7 +197,7 @@ func checkInitRepository(owner, name string) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func adoptRepository(ctx models.DBContext, repoPath string, u *models.User, repo *models.Repository, opts models.CreateRepoOptions) (err error) {
|
||||
func adoptRepository(ctx *db.Context, repoPath string, u *models.User, repo *models.Repository, opts models.CreateRepoOptions) (err error) {
|
||||
isExist, err := util.IsExist(repoPath)
|
||||
if err != nil {
|
||||
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
|
||||
@@ -283,7 +284,7 @@ func adoptRepository(ctx models.DBContext, repoPath string, u *models.User, repo
|
||||
}
|
||||
|
||||
// InitRepository initializes README and .gitignore if needed.
|
||||
func initRepository(ctx models.DBContext, repoPath string, u *models.User, repo *models.Repository, opts models.CreateRepoOptions) (err error) {
|
||||
func initRepository(ctx *db.Context, repoPath string, u *models.User, repo *models.Repository, opts models.CreateRepoOptions) (err error) {
|
||||
if err = checkInitRepository(repo.OwnerName, repo.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -8,9 +8,9 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
models.MainTest(m, filepath.Join("..", ".."))
|
||||
db.MainTest(m, filepath.Join("..", ".."))
|
||||
}
|
||||
|
@@ -14,6 +14,7 @@ import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/lfs"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
@@ -132,7 +133,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *models.User, repo *models.
|
||||
}
|
||||
}
|
||||
|
||||
if err = repo.UpdateSize(models.DefaultDBContext()); err != nil {
|
||||
if err = repo.UpdateSize(db.DefaultContext()); err != nil {
|
||||
log.Error("Failed to update size for repository: %v", err)
|
||||
}
|
||||
|
||||
|
@@ -10,13 +10,14 @@ import (
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
)
|
||||
|
||||
// PushUpdateAddDeleteTags updates a number of added and delete tags
|
||||
func PushUpdateAddDeleteTags(repo *models.Repository, gitRepo *git.Repository, addTags, delTags []string) error {
|
||||
return models.WithTx(func(ctx models.DBContext) error {
|
||||
return db.WithTx(func(ctx *db.Context) error {
|
||||
if err := models.PushUpdateDeleteTagsContext(ctx, repo, delTags); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -25,7 +26,7 @@ func PushUpdateAddDeleteTags(repo *models.Repository, gitRepo *git.Repository, a
|
||||
}
|
||||
|
||||
// pushUpdateAddTags updates a number of add tags
|
||||
func pushUpdateAddTags(ctx models.DBContext, repo *models.Repository, gitRepo *git.Repository, tags []string) error {
|
||||
func pushUpdateAddTags(ctx *db.Context, repo *models.Repository, gitRepo *git.Repository, tags []string) error {
|
||||
if len(tags) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user