diff options
Diffstat (limited to 'modules')
42 files changed, 198 insertions, 169 deletions
diff --git a/modules/convert/git_commit_test.go b/modules/convert/git_commit_test.go index aa35571706..298a006cb1 100644 --- a/modules/convert/git_commit_test.go +++ b/modules/convert/git_commit_test.go @@ -9,6 +9,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" @@ -17,8 +18,8 @@ import ( ) func TestToCommitMeta(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) - headRepo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + assert.NoError(t, db.PrepareTestDatabase()) + headRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) sha1, _ := git.NewIDFromString("0000000000000000000000000000000000000000") signature := &git.Signature{Name: "Test Signature", Email: "test@email.com", When: time.Unix(0, 0)} tag := &git.Tag{ diff --git a/modules/convert/issue_test.go b/modules/convert/issue_test.go index f3c5b50c8c..7c6b05e816 100644 --- a/modules/convert/issue_test.go +++ b/modules/convert/issue_test.go @@ -10,6 +10,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" @@ -18,9 +19,9 @@ import ( ) func TestLabel_ToLabel(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) - label := models.AssertExistsAndLoadBean(t, &models.Label{ID: 1}).(*models.Label) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: label.RepoID}).(*models.Repository) + assert.NoError(t, db.PrepareTestDatabase()) + label := db.AssertExistsAndLoadBean(t, &models.Label{ID: 1}).(*models.Label) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: label.RepoID}).(*models.Repository) assert.Equal(t, &api.Label{ ID: label.ID, Name: label.Name, diff --git a/modules/convert/main_test.go b/modules/convert/main_test.go index 8720ff0427..acb9802f97 100644 --- a/modules/convert/main_test.go +++ b/modules/convert/main_test.go @@ -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("..", "..")) } diff --git a/modules/convert/pull_test.go b/modules/convert/pull_test.go index adf42d8ca4..655fc76329 100644 --- a/modules/convert/pull_test.go +++ b/modules/convert/pull_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" @@ -15,9 +16,9 @@ import ( func TestPullRequest_APIFormat(t *testing.T) { //with HeadRepo - assert.NoError(t, models.PrepareTestDatabase()) - headRepo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) - pr := models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest) + assert.NoError(t, db.PrepareTestDatabase()) + headRepo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + pr := db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest) assert.NoError(t, pr.LoadAttributes()) assert.NoError(t, pr.LoadIssue()) apiPullRequest := ToAPIPullRequest(pr) @@ -31,7 +32,7 @@ func TestPullRequest_APIFormat(t *testing.T) { }, apiPullRequest.Head) //withOut HeadRepo - pr = models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest) + pr = db.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest) assert.NoError(t, pr.LoadIssue()) assert.NoError(t, pr.LoadAttributes()) // simulate fork deletion diff --git a/modules/convert/user_test.go b/modules/convert/user_test.go index 679c4f9894..404adc2029 100644 --- a/modules/convert/user_test.go +++ b/modules/convert/user_test.go @@ -8,20 +8,21 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" ) func TestUser_ToUser(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - user1 := models.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User) + user1 := db.AssertExistsAndLoadBean(t, &models.User{ID: 1, IsAdmin: true}).(*models.User) apiUser := toUser(user1, true, true) assert.True(t, apiUser.IsAdmin) - user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User) + user2 := db.AssertExistsAndLoadBean(t, &models.User{ID: 2, IsAdmin: false}).(*models.User) apiUser = toUser(user2, true, true) assert.False(t, apiUser.IsAdmin) @@ -30,7 +31,7 @@ func TestUser_ToUser(t *testing.T) { assert.False(t, apiUser.IsAdmin) assert.EqualValues(t, api.VisibleTypePublic.String(), apiUser.Visibility) - user31 := models.AssertExistsAndLoadBean(t, &models.User{ID: 31, IsAdmin: false, Visibility: api.VisibleTypePrivate}).(*models.User) + user31 := db.AssertExistsAndLoadBean(t, &models.User{ID: 31, IsAdmin: false, Visibility: api.VisibleTypePrivate}).(*models.User) apiUser = toUser(user31, true, true) assert.False(t, apiUser.IsAdmin) diff --git a/modules/doctor/dbconsistency.go b/modules/doctor/dbconsistency.go index 0d84c63976..fef3ae67a3 100644 --- a/modules/doctor/dbconsistency.go +++ b/modules/doctor/dbconsistency.go @@ -8,6 +8,7 @@ import ( "context" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/migrations" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -15,7 +16,7 @@ import ( func checkDBConsistency(logger log.Logger, autofix bool) error { // make sure DB version is uptodate - if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil { + if err := db.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil { logger.Critical("Model version on the database does not match the current Gitea version. Model consistency will not be checked until the database is upgraded") return err } @@ -225,14 +226,14 @@ func checkDBConsistency(logger log.Logger, autofix bool) error { // TODO: function to recalc all counters if setting.Database.UsePostgreSQL { - count, err = models.CountBadSequences() + count, err = db.CountBadSequences() if err != nil { logger.Critical("Error: %v whilst checking sequence values", err) return err } if count > 0 { if autofix { - err := models.FixBadSequences() + err := db.FixBadSequences() if err != nil { logger.Critical("Error: %v whilst attempting to fix sequences", err) return err diff --git a/modules/doctor/dbversion.go b/modules/doctor/dbversion.go index c5cac3bf91..acc564bdb0 100644 --- a/modules/doctor/dbversion.go +++ b/modules/doctor/dbversion.go @@ -7,13 +7,13 @@ package doctor import ( "context" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/migrations" "code.gitea.io/gitea/modules/log" ) func checkDBVersion(logger log.Logger, autofix bool) error { - if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil { + if err := db.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil { if !autofix { logger.Critical("Error: %v during ensure up to date", err) return err @@ -21,7 +21,7 @@ func checkDBVersion(logger log.Logger, autofix bool) error { logger.Warn("Got Error: %v during ensure up to date", err) logger.Warn("Attempting to migrate to the latest DB version to fix this.") - err = models.NewEngine(context.Background(), migrations.Migrate) + err = db.NewEngine(context.Background(), migrations.Migrate) if err != nil { logger.Critical("Error: %v during migration", err) } diff --git a/modules/doctor/doctor.go b/modules/doctor/doctor.go index 8c0d467304..d5cc4a1850 100644 --- a/modules/doctor/doctor.go +++ b/modules/doctor/doctor.go @@ -9,7 +9,7 @@ import ( "sort" "strings" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) @@ -47,7 +47,7 @@ func initDBDisableConsole(disableConsole bool) error { setting.InitDBConfig() setting.NewXORMLogService(disableConsole) - if err := models.SetEngine(); err != nil { + if err := db.SetEngine(); err != nil { return fmt.Errorf("models.SetEngine: %v", err) } return nil diff --git a/modules/doctor/mergebase.go b/modules/doctor/mergebase.go index 7e9a065b77..e792c2b2fd 100644 --- a/modules/doctor/mergebase.go +++ b/modules/doctor/mergebase.go @@ -9,14 +9,16 @@ 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" + "xorm.io/builder" ) func iteratePRs(repo *models.Repository, each func(*models.Repository, *models.PullRequest) error) error { - return models.Iterate( - models.DefaultDBContext(), + return db.Iterate( + db.DefaultContext(), new(models.PullRequest), builder.Eq{"base_repo_id": repo.ID}, func(idx int, bean interface{}) error { diff --git a/modules/doctor/misc.go b/modules/doctor/misc.go index 47fee8f7fd..25bc3c3a72 100644 --- a/modules/doctor/misc.go +++ b/modules/doctor/misc.go @@ -12,19 +12,21 @@ 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/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" + lru "github.com/hashicorp/golang-lru" "xorm.io/builder" ) func iterateRepositories(each func(*models.Repository) error) error { - err := models.Iterate( - models.DefaultDBContext(), + err := db.Iterate( + db.DefaultContext(), new(models.Repository), builder.Gt{"id": 0}, func(idx int, bean interface{}) error { diff --git a/modules/indexer/code/bleve_test.go b/modules/indexer/code/bleve_test.go index f79957220f..1199dc98a0 100644 --- a/modules/indexer/code/bleve_test.go +++ b/modules/indexer/code/bleve_test.go @@ -8,14 +8,14 @@ import ( "io/ioutil" "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/util" "github.com/stretchr/testify/assert" ) func TestBleveIndexAndSearch(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) dir, err := ioutil.TempDir("", "bleve.index") assert.NoError(t, err) diff --git a/modules/indexer/code/elastic_search_test.go b/modules/indexer/code/elastic_search_test.go index 7cf62e0c5f..c9d2c297bb 100644 --- a/modules/indexer/code/elastic_search_test.go +++ b/modules/indexer/code/elastic_search_test.go @@ -8,13 +8,12 @@ import ( "os" "testing" - "code.gitea.io/gitea/models" - + "code.gitea.io/gitea/models/db" "github.com/stretchr/testify/assert" ) func TestESIndexAndSearch(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) u := os.Getenv("TEST_INDEXER_CODE_ES_URL") if u == "" { diff --git a/modules/indexer/code/indexer.go b/modules/indexer/code/indexer.go index 67fa43eda8..46b5905059 100644 --- a/modules/indexer/code/indexer.go +++ b/modules/indexer/code/indexer.go @@ -12,6 +12,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" @@ -285,7 +286,7 @@ func UpdateRepoIndexer(repo *models.Repository) { func populateRepoIndexer(ctx context.Context) { log.Info("Populating the repo indexer with existing repositories") - exist, err := models.IsTableNotEmpty("repository") + exist, err := db.IsTableNotEmpty("repository") if err != nil { log.Fatal("System error: %v", err) } else if !exist { @@ -295,12 +296,12 @@ func populateRepoIndexer(ctx context.Context) { // if there is any existing repo indexer metadata in the DB, delete it // since we are starting afresh. Also, xorm requires deletes to have a // condition, and we want to delete everything, thus 1=1. - if err := models.DeleteAllRecords("repo_indexer_status"); err != nil { + if err := db.DeleteAllRecords("repo_indexer_status"); err != nil { log.Fatal("System error: %v", err) } var maxRepoID int64 - if maxRepoID, err = models.GetMaxID("repository"); err != nil { + if maxRepoID, err = db.GetMaxID("repository"); err != nil { log.Fatal("System error: %v", err) } diff --git a/modules/indexer/code/indexer_test.go b/modules/indexer/code/indexer_test.go index 01717bd288..34930a84ca 100644 --- a/modules/indexer/code/indexer_test.go +++ b/modules/indexer/code/indexer_test.go @@ -8,13 +8,12 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models" - + "code.gitea.io/gitea/models/db" "github.com/stretchr/testify/assert" ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..", "..")) + db.MainTest(m, filepath.Join("..", "..", "..")) } func testIndexer(name string, t *testing.T, indexer Indexer) { diff --git a/modules/indexer/issues/indexer_test.go b/modules/indexer/issues/indexer_test.go index 8c163f78d1..1431115b8e 100644 --- a/modules/indexer/issues/indexer_test.go +++ b/modules/indexer/issues/indexer_test.go @@ -11,7 +11,7 @@ import ( "testing" "time" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -21,11 +21,11 @@ import ( ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..", "..")) + db.MainTest(m, filepath.Join("..", "..", "..")) } func TestBleveSearchIssues(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) setting.Cfg = ini.Empty() tmpIndexerDir, err := ioutil.TempDir("", "issues-indexer") @@ -74,7 +74,7 @@ func TestBleveSearchIssues(t *testing.T) { } func TestDBSearchIssues(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) setting.Indexer.IssueType = "db" InitIssueIndexer(true) diff --git a/modules/indexer/stats/indexer.go b/modules/indexer/stats/indexer.go index 4d8a174ff9..fe87a2268b 100644 --- a/modules/indexer/stats/indexer.go +++ b/modules/indexer/stats/indexer.go @@ -6,6 +6,7 @@ package stats import ( "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" ) @@ -39,7 +40,7 @@ func populateRepoIndexer() { isShutdown := graceful.GetManager().IsShutdown() - exist, err := models.IsTableNotEmpty("repository") + exist, err := db.IsTableNotEmpty("repository") if err != nil { log.Fatal("System error: %v", err) } else if !exist { @@ -47,7 +48,7 @@ func populateRepoIndexer() { } var maxRepoID int64 - if maxRepoID, err = models.GetMaxID("repository"); err != nil { + if maxRepoID, err = db.GetMaxID("repository"); err != nil { log.Fatal("System error: %v", err) } diff --git a/modules/indexer/stats/indexer_test.go b/modules/indexer/stats/indexer_test.go index 4bcbaa9423..a4645b2083 100644 --- a/modules/indexer/stats/indexer_test.go +++ b/modules/indexer/stats/indexer_test.go @@ -10,6 +10,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/setting" "gopkg.in/ini.v1" @@ -18,11 +19,11 @@ import ( ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..", "..")) + db.MainTest(m, filepath.Join("..", "..", "..")) } func TestRepoStatsIndex(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) setting.Cfg = ini.Empty() setting.NewQueueService() diff --git a/modules/migrations/gitea_uploader.go b/modules/migrations/gitea_uploader.go index f8fb52fa02..3e8d396ed1 100644 --- a/modules/migrations/gitea_uploader.go +++ b/modules/migrations/gitea_uploader.go @@ -16,6 +16,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/migrations/base" @@ -69,17 +70,17 @@ func NewGiteaLocalUploader(ctx context.Context, doer *models.User, repoOwner, re func (g *GiteaLocalUploader) MaxBatchInsertSize(tp string) int { switch tp { case "issue": - return models.MaxBatchInsertSize(new(models.Issue)) + return db.MaxBatchInsertSize(new(models.Issue)) case "comment": - return models.MaxBatchInsertSize(new(models.Comment)) + return db.MaxBatchInsertSize(new(models.Comment)) case "milestone": - return models.MaxBatchInsertSize(new(models.Milestone)) + return db.MaxBatchInsertSize(new(models.Milestone)) case "label": - return models.MaxBatchInsertSize(new(models.Label)) + return db.MaxBatchInsertSize(new(models.Label)) case "release": - return models.MaxBatchInsertSize(new(models.Release)) + return db.MaxBatchInsertSize(new(models.Release)) case "pullrequest": - return models.MaxBatchInsertSize(new(models.PullRequest)) + return db.MaxBatchInsertSize(new(models.PullRequest)) } return 10 } diff --git a/modules/migrations/gitea_uploader_test.go b/modules/migrations/gitea_uploader_test.go index 032393032b..73293f1f85 100644 --- a/modules/migrations/gitea_uploader_test.go +++ b/modules/migrations/gitea_uploader_test.go @@ -11,6 +11,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/migrations/base" "code.gitea.io/gitea/modules/structs" @@ -23,9 +24,9 @@ func TestGiteaUploadRepo(t *testing.T) { // FIXME: Since no accesskey or user/password will trigger rate limit of github, just skip t.Skip() - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User) var ( downloader = NewGithubDownloaderV3(context.Background(), "https://github.com", "", "", "", "go-xorm", "builder") @@ -50,7 +51,7 @@ func TestGiteaUploadRepo(t *testing.T) { }, nil) assert.NoError(t, err) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{OwnerID: user.ID, Name: repoName}).(*models.Repository) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{OwnerID: user.ID, Name: repoName}).(*models.Repository) assert.True(t, repo.HasWiki()) assert.EqualValues(t, models.RepositoryReady, repo.Status) diff --git a/modules/migrations/main_test.go b/modules/migrations/main_test.go index 86aee4e86b..5b29230659 100644 --- a/modules/migrations/main_test.go +++ b/modules/migrations/main_test.go @@ -10,14 +10,14 @@ import ( "testing" "time" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/migrations/base" "github.com/stretchr/testify/assert" ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } func timePtr(t time.Time) *time.Time { diff --git a/modules/migrations/migrate_test.go b/modules/migrations/migrate_test.go index 98ee2dfc4a..c050a9abc0 100644 --- a/modules/migrations/migrate_test.go +++ b/modules/migrations/migrate_test.go @@ -9,16 +9,17 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/setting" "github.com/stretchr/testify/assert" ) func TestMigrateWhiteBlocklist(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - adminUser := models.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User) - nonAdminUser := models.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) + adminUser := db.AssertExistsAndLoadBean(t, &models.User{Name: "user1"}).(*models.User) + nonAdminUser := db.AssertExistsAndLoadBean(t, &models.User{Name: "user2"}).(*models.User) setting.Migrations.AllowedDomains = []string{"github.com"} assert.NoError(t, Init()) diff --git a/modules/notification/action/action_test.go b/modules/notification/action/action_test.go index cdfe4bd663..705a4fb2f4 100644 --- a/modules/notification/action/action_test.go +++ b/modules/notification/action/action_test.go @@ -10,18 +10,19 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "github.com/stretchr/testify/assert" ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..", "..")) + db.MainTest(m, filepath.Join("..", "..", "..")) } func TestRenameRepoAction(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{OwnerID: user.ID}).(*models.Repository) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{OwnerID: user.ID}).(*models.Repository) repo.Owner = user oldRepoName := repo.Name @@ -38,10 +39,10 @@ func TestRenameRepoAction(t *testing.T) { IsPrivate: repo.IsPrivate, Content: oldRepoName, } - models.AssertNotExistsBean(t, actionBean) + db.AssertNotExistsBean(t, actionBean) NewNotifier().NotifyRenameRepository(user, repo, oldRepoName) - models.AssertExistsAndLoadBean(t, actionBean) + db.AssertExistsAndLoadBean(t, actionBean) models.CheckConsistencyFor(t, &models.Action{}) } diff --git a/modules/repofiles/action_test.go b/modules/repofiles/action_test.go index 97632df68f..c29cfd686d 100644 --- a/modules/repofiles/action_test.go +++ b/modules/repofiles/action_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" @@ -15,7 +16,7 @@ import ( ) func TestUpdateIssuesCommit(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) pushCommits := []*repository.PushCommit{ { Sha1: "abcdef1", @@ -43,8 +44,8 @@ func TestUpdateIssuesCommit(t *testing.T) { }, } - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repo.Owner = user commentBean := &models.Comment{ @@ -55,11 +56,11 @@ func TestUpdateIssuesCommit(t *testing.T) { } issueBean := &models.Issue{RepoID: repo.ID, Index: 4} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - models.AssertExistsAndLoadBean(t, commentBean) - models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, commentBean) + db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) // Test that push to a non-default branch closes no issue. @@ -73,7 +74,7 @@ func TestUpdateIssuesCommit(t *testing.T) { Message: "close #1", }, } - repo = models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + repo = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) commentBean = &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef1", @@ -82,11 +83,11 @@ func TestUpdateIssuesCommit(t *testing.T) { } issueBean = &models.Issue{RepoID: repo.ID, Index: 1} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch")) - models.AssertExistsAndLoadBean(t, commentBean) - models.AssertNotExistsBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, commentBean) + db.AssertNotExistsBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) pushCommits = []*repository.PushCommit{ @@ -99,7 +100,7 @@ func TestUpdateIssuesCommit(t *testing.T) { Message: "close " + setting.AppURL + repo.FullName() + "/pulls/1", }, } - repo = models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) + repo = db.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) commentBean = &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef3", @@ -108,16 +109,16 @@ func TestUpdateIssuesCommit(t *testing.T) { } issueBean = &models.Issue{RepoID: repo.ID, Index: 1} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 1}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - models.AssertExistsAndLoadBean(t, commentBean) - models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, commentBean) + db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_Colon(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) + assert.NoError(t, db.PrepareTestDatabase()) pushCommits := []*repository.PushCommit{ { Sha1: "abcdef2", @@ -129,21 +130,21 @@ func TestUpdateIssuesCommit_Colon(t *testing.T) { }, } - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) repo.Owner = user issueBean := &models.Issue{RepoID: repo.ID, Index: 4} - models.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") + db.AssertNotExistsBean(t, &models.Issue{RepoID: repo.ID, Index: 2}, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_Issue5957(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + assert.NoError(t, db.PrepareTestDatabase()) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Test that push to a non-default branch closes an issue. pushCommits := []*repository.PushCommit{ @@ -157,7 +158,7 @@ func TestUpdateIssuesCommit_Issue5957(t *testing.T) { }, } - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) commentBean := &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef1", @@ -167,17 +168,17 @@ func TestUpdateIssuesCommit_Issue5957(t *testing.T) { issueBean := &models.Issue{RepoID: repo.ID, Index: 2, ID: 7} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, issueBean, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, issueBean, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch")) - models.AssertExistsAndLoadBean(t, commentBean) - models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, commentBean) + db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + assert.NoError(t, db.PrepareTestDatabase()) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Test that a push to default branch closes issue in another repo // If the user also has push permissions to that repo @@ -192,7 +193,7 @@ func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) { }, } - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) commentBean := &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef1", @@ -202,17 +203,17 @@ func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) { issueBean := &models.Issue{RepoID: 1, Index: 1, ID: 1} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, issueBean, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, issueBean, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - models.AssertExistsAndLoadBean(t, commentBean) - models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, commentBean) + db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + assert.NoError(t, db.PrepareTestDatabase()) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // Test that a push to default branch closes issue in another repo // If the user also has push permissions to that repo @@ -227,7 +228,7 @@ func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) { }, } - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) commentBean := &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef1", @@ -237,17 +238,17 @@ func TestUpdateIssuesCommit_AnotherRepo_FullAddress(t *testing.T) { issueBean := &models.Issue{RepoID: 1, Index: 1, ID: 1} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, issueBean, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, issueBean, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - models.AssertExistsAndLoadBean(t, commentBean) - models.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") + db.AssertExistsAndLoadBean(t, commentBean) + db.AssertExistsAndLoadBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) } func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) { - assert.NoError(t, models.PrepareTestDatabase()) - user := models.AssertExistsAndLoadBean(t, &models.User{ID: 10}).(*models.User) + assert.NoError(t, db.PrepareTestDatabase()) + user := db.AssertExistsAndLoadBean(t, &models.User{ID: 10}).(*models.User) // Test that a push with close reference *can not* close issue // If the committer doesn't have push rights in that repo @@ -270,7 +271,7 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) { }, } - repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 6}).(*models.Repository) + repo := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 6}).(*models.Repository) commentBean := &models.Comment{ Type: models.CommentTypeCommitRef, CommitSHA: "abcdef3", @@ -286,12 +287,12 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) { issueBean := &models.Issue{RepoID: 3, Index: 1, ID: 6} - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, commentBean2) - models.AssertNotExistsBean(t, issueBean, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, commentBean2) + db.AssertNotExistsBean(t, issueBean, "is_closed=1") assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch)) - models.AssertNotExistsBean(t, commentBean) - models.AssertNotExistsBean(t, commentBean2) - models.AssertNotExistsBean(t, issueBean, "is_closed=1") + db.AssertNotExistsBean(t, commentBean) + db.AssertNotExistsBean(t, commentBean2) + db.AssertNotExistsBean(t, issueBean, "is_closed=1") models.CheckConsistencyFor(t, &models.Action{}) } diff --git a/modules/repofiles/blob_test.go b/modules/repofiles/blob_test.go index 8ba80347bf..5238dd6e74 100644 --- a/modules/repofiles/blob_test.go +++ b/modules/repofiles/blob_test.go @@ -7,7 +7,7 @@ package repofiles import ( "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -15,7 +15,7 @@ import ( ) func TestGetBlobBySHA(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") test.LoadRepo(t, ctx, 1) test.LoadRepoCommit(t, ctx) diff --git a/modules/repofiles/content_test.go b/modules/repofiles/content_test.go index 253d434459..f68460d7ec 100644 --- a/modules/repofiles/content_test.go +++ b/modules/repofiles/content_test.go @@ -8,7 +8,7 @@ import ( "path/filepath" "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -16,7 +16,7 @@ import ( ) func TestMain(m *testing.M) { - models.MainTest(m, filepath.Join("..", "..")) + db.MainTest(m, filepath.Join("..", "..")) } func getExpectedReadmeContentsResponse() *api.ContentsResponse { @@ -49,7 +49,7 @@ func getExpectedReadmeContentsResponse() *api.ContentsResponse { } func TestGetContents(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -77,7 +77,7 @@ func TestGetContents(t *testing.T) { } func TestGetContentsOrListForDir(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -112,7 +112,7 @@ func TestGetContentsOrListForDir(t *testing.T) { } func TestGetContentsOrListForFile(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -140,7 +140,7 @@ func TestGetContentsOrListForFile(t *testing.T) { } func TestGetContentsErrors(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -171,7 +171,7 @@ func TestGetContentsErrors(t *testing.T) { } func TestGetContentsOrListErrors(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -202,7 +202,7 @@ func TestGetContentsOrListErrors(t *testing.T) { } func TestGetContentsOrListOfEmptyRepos(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo15") ctx.SetParams(":id", "15") test.LoadRepo(t, ctx, 15) diff --git a/modules/repofiles/diff_test.go b/modules/repofiles/diff_test.go index da50284150..aeb4f76a45 100644 --- a/modules/repofiles/diff_test.go +++ b/modules/repofiles/diff_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/services/gitdiff" @@ -15,7 +16,7 @@ import ( ) func TestGetDiffPreview(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) @@ -128,7 +129,7 @@ func TestGetDiffPreview(t *testing.T) { } func TestGetDiffPreviewErrors(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) diff --git a/modules/repofiles/file_test.go b/modules/repofiles/file_test.go index aafe816a16..2974056549 100644 --- a/modules/repofiles/file_test.go +++ b/modules/repofiles/file_test.go @@ -7,7 +7,7 @@ package repofiles import ( "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -81,7 +81,7 @@ func getExpectedFileResponse() *api.FileResponse { } func TestGetFileResponseFromCommit(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") ctx.SetParams(":id", "1") test.LoadRepo(t, ctx, 1) diff --git a/modules/repofiles/tree_test.go b/modules/repofiles/tree_test.go index 274e89e969..9e2efa4216 100644 --- a/modules/repofiles/tree_test.go +++ b/modules/repofiles/tree_test.go @@ -7,7 +7,7 @@ package repofiles import ( "testing" - "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" @@ -15,7 +15,7 @@ import ( ) func TestGetTreeBySHA(t *testing.T) { - models.PrepareTestEnv(t) + db.PrepareTestEnv(t) ctx := test.MockContext(t, "user2/repo1") test.LoadRepo(t, ctx, 1) test.LoadRepoCommit(t, ctx) diff --git a/modules/repository/adopt.go b/modules/repository/adopt.go index 321e6ab767..9371822fbc 100644 --- a/modules/repository/adopt.go +++ b/modules/repository/adopt.go @@ -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 { diff --git a/modules/repository/check.go b/modules/repository/check.go index 0485e9e83a..1b550ad4f0 100644 --- a/modules/repository/check.go +++ b/modules/repository/check.go @@ -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 { diff --git a/modules/repository/commits_test.go b/modules/repository/commits_test.go index 9f08525258..c62ad58449 100644 --- a/modules/repository/commits_test.go +++ b/modules/repository/commits_test.go @@ -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{ diff --git a/modules/repository/create.go b/modules/repository/create.go index 5eac03836e..80f446e83f 100644 --- a/modules/repository/create.go +++ b/modules/repository/create.go @@ -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 } diff --git a/modules/repository/create_test.go b/modules/repository/create_test.go index 65ed7806a9..040c061d92 100644 --- a/modules/repository/create_test.go +++ b/modules/repository/create_test.go @@ -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) diff --git a/modules/repository/fork.go b/modules/repository/fork.go index b154cc5ccd..ff69f75b32 100644 --- a/modules/repository/fork.go +++ b/modules/repository/fork.go @@ -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 diff --git a/modules/repository/fork_test.go b/modules/repository/fork_test.go index fc4a724660..7a05f9dd97 100644 --- a/modules/repository/fork_test.go +++ b/modules/repository/fork_test.go @@ -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, diff --git a/modules/repository/generate.go b/modules/repository/generate.go index 1ba457fb3a..6ac2a3051c 100644 --- a/modules/repository/generate.go +++ b/modules/repository/generate.go @@ -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, diff --git a/modules/repository/hooks.go b/modules/repository/hooks.go index 8b4e7d6302..de5df591f0 100644 --- a/modules/repository/hooks.go +++ b/modules/repository/hooks.go @@ -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 { diff --git a/modules/repository/init.go b/modules/repository/init.go index 50cde4c0b9..e42d35a971 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -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 } diff --git a/modules/repository/main_test.go b/modules/repository/main_test.go index f13f358635..91d0d36ca0 100644 --- a/modules/repository/main_test.go +++ b/modules/repository/main_test.go @@ -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("..", "..")) } diff --git a/modules/repository/repo.go b/modules/repository/repo.go index 00413549ee..b59a80ee2f 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -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) } diff --git a/modules/repository/update.go b/modules/repository/update.go index 975f577da8..d9ff12e1ad 100644 --- a/modules/repository/update.go +++ b/modules/repository/update.go @@ -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 } diff --git a/modules/test/context_tests.go b/modules/test/context_tests.go index a15c74d828..8a4d2d8bce 100644 --- a/modules/test/context_tests.go +++ b/modules/test/context_tests.go @@ -14,6 +14,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/web/middleware" @@ -52,7 +53,7 @@ func MockContext(t *testing.T, path string) *context.Context { // LoadRepo load a repo into a test context. func LoadRepo(t *testing.T, ctx *context.Context, repoID int64) { ctx.Repo = &context.Repository{} - ctx.Repo.Repository = models.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository) + ctx.Repo.Repository = db.AssertExistsAndLoadBean(t, &models.Repository{ID: repoID}).(*models.Repository) var err error ctx.Repo.Owner, err = models.GetUserByID(ctx.Repo.Repository.OwnerID) assert.NoError(t, err) @@ -77,7 +78,7 @@ func LoadRepoCommit(t *testing.T, ctx *context.Context) { // LoadUser load a user into a test context. func LoadUser(t *testing.T, ctx *context.Context, userID int64) { - ctx.User = models.AssertExistsAndLoadBean(t, &models.User{ID: userID}).(*models.User) + ctx.User = db.AssertExistsAndLoadBean(t, &models.User{ID: userID}).(*models.User) } // LoadGitRepo load a git repo into a test context. Requires that ctx.Repo has |