summaryrefslogtreecommitdiffstats
path: root/models/migrations
diff options
context:
space:
mode:
authorJonas <cez81@users.noreply.github.com>2017-04-11 15:30:15 +0200
committerBo-Yi Wu <appleboy.tw@gmail.com>2017-04-11 21:30:15 +0800
commitbe6edaddcba1e607884539a250bd19debabba751 (patch)
tree352d09f7eb4643fb36bae9625fbeafd77643689e /models/migrations
parent54f0293f0ab73f357f545f8e05d16f9b254aba5e (diff)
downloadgitea-be6edaddcba1e607884539a250bd19debabba751.tar.gz
gitea-be6edaddcba1e607884539a250bd19debabba751.zip
Repo size in admin panel (#1482)
* Implementation of the feature to view repository size in admin panel * Move GetRepoSize to git module * Repository.RepoSize -> Repository.Size * RepoSize -> Size in template * Redo a few bits and pieces * Update size when syncing mirror or forking * Remove GetRepoSize * Changed fatal errors to error message * Copy migration code from Gogs * make fmt
Diffstat (limited to 'models/migrations')
-rw-r--r--models/migrations/migrations.go2
-rw-r--r--models/migrations/v28.go77
2 files changed, 79 insertions, 0 deletions
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go
index fee0fef79c..fb7b66cd00 100644
--- a/models/migrations/migrations.go
+++ b/models/migrations/migrations.go
@@ -104,6 +104,8 @@ var migrations = []Migration{
NewMigration("generate and migrate repo and wiki Git hooks", generateAndMigrateGitHookChains),
// v27 -> v28
NewMigration("change mirror interval from hours to time.Duration", convertIntervalToDuration),
+ // v28 -> v29
+ NewMigration("add field for repo size", addRepoSize),
}
// Migrate database to current version
diff --git a/models/migrations/v28.go b/models/migrations/v28.go
new file mode 100644
index 0000000000..f7b4cd6e30
--- /dev/null
+++ b/models/migrations/v28.go
@@ -0,0 +1,77 @@
+// Copyright 2017 The Gogs Authors. All rights reserved.
+// Copyright 2017 Gitea. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package migrations
+
+import (
+ "fmt"
+ "path/filepath"
+ "strings"
+
+ "code.gitea.io/git"
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/setting"
+
+ "github.com/go-xorm/xorm"
+)
+
+func addRepoSize(x *xorm.Engine) (err error) {
+ log.Info("This migration could take up to minutes, please be patient.")
+ type Repository struct {
+ ID int64
+ OwnerID int64
+ Name string
+ Size int64
+ }
+ type User struct {
+ ID int64
+ Name string
+ }
+ if err = x.Sync2(new(Repository)); err != nil {
+ return fmt.Errorf("Sync2: %v", err)
+ }
+
+ // For the sake of SQLite3, we can't use x.Iterate here.
+ offset := 0
+ for {
+ repos := make([]*Repository, 0, 10)
+ if err = x.Sql(fmt.Sprintf("SELECT * FROM `repository` ORDER BY id ASC LIMIT 10 OFFSET %d", offset)).
+ Find(&repos); err != nil {
+ return fmt.Errorf("select repos [offset: %d]: %v", offset, err)
+ }
+ log.Trace("Select [offset: %d, repos: %d]", offset, len(repos))
+ if len(repos) == 0 {
+ break
+ }
+ offset += 10
+
+ for _, repo := range repos {
+ if repo.Name == "." || repo.Name == ".." {
+ continue
+ }
+
+ user := new(User)
+ has, err := x.Where("id = ?", repo.OwnerID).Get(user)
+ if err != nil {
+ return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err)
+ } else if !has {
+ continue
+ }
+
+ repoPath := filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".git"
+ countObject, err := git.GetRepoSize(repoPath)
+ if err != nil {
+ log.Warn("GetRepoSize: %v", err)
+ continue
+ }
+
+ repo.Size = countObject.Size + countObject.SizePack
+ if _, err = x.Id(repo.ID).Cols("size").Update(repo); err != nil {
+ return fmt.Errorf("update size: %v", err)
+ }
+ }
+ }
+ return nil
+}