diff options
author | Jonas <cez81@users.noreply.github.com> | 2017-04-08 17:27:26 +0200 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2017-04-08 23:27:26 +0800 |
commit | 54f0293f0ab73f357f545f8e05d16f9b254aba5e (patch) | |
tree | b95f86dd93e7c74cada06a919d7f790c2ca1d083 /models | |
parent | edbb9eefd6c12d6c67bf864539a51f5a09c7d670 (diff) | |
download | gitea-54f0293f0ab73f357f545f8e05d16f9b254aba5e.tar.gz gitea-54f0293f0ab73f357f545f8e05d16f9b254aba5e.zip |
Mirror sync interval specified as duration string (#1407)
* Sync interval specifed as duration string
* Changed mirror interval text
* make fmt
* Add MinInterval for mirror sync
* Use duration internally
* Changed min default to 10m
* make fmt
* Incorrect default
* Removed defaults in MustDuration()
* Add Mirror interval migration
* Default values corrected
* Use transaction during migration
* Change http 500 to page with error message
* Cleanup session.commit()
Diffstat (limited to 'models')
-rw-r--r-- | models/migrations/migrations.go | 2 | ||||
-rw-r--r-- | models/migrations/v27.go | 56 | ||||
-rw-r--r-- | models/repo.go | 2 | ||||
-rw-r--r-- | models/repo_mirror.go | 6 |
4 files changed, 62 insertions, 4 deletions
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 07a56e879b..fee0fef79c 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -102,6 +102,8 @@ var migrations = []Migration{ NewMigration("add show field in user openid table", addUserOpenIDShow), // v26 -> v27 NewMigration("generate and migrate repo and wiki Git hooks", generateAndMigrateGitHookChains), + // v27 -> v28 + NewMigration("change mirror interval from hours to time.Duration", convertIntervalToDuration), } // Migrate database to current version diff --git a/models/migrations/v27.go b/models/migrations/v27.go new file mode 100644 index 0000000000..0ac9553725 --- /dev/null +++ b/models/migrations/v27.go @@ -0,0 +1,56 @@ +// 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" + "time" + + "github.com/go-xorm/xorm" +) + +func convertIntervalToDuration(x *xorm.Engine) (err error) { + type Repository struct { + ID int64 + OwnerID int64 + Name string + } + type Mirror struct { + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"INDEX"` + Repo *Repository `xorm:"-"` + Interval time.Duration + EnablePrune bool `xorm:"NOT NULL DEFAULT true"` + + Updated time.Time `xorm:"-"` + UpdatedUnix int64 `xorm:"INDEX"` + NextUpdate time.Time `xorm:"-"` + NextUpdateUnix int64 `xorm:"INDEX"` + + address string `xorm:"-"` + } + + sess := x.NewSession() + defer sess.Close() + + if err := sess.Begin(); err != nil { + return err + } + + var mirrors []Mirror + err = sess.Table("mirror").Select("*").Find(&mirrors) + if err != nil { + return fmt.Errorf("Query repositories: %v", err) + } + for _, mirror := range mirrors { + mirror.Interval = mirror.Interval * time.Hour + _, err := sess.Id(mirror.ID).Cols("interval").Update(mirror) + if err != nil { + return fmt.Errorf("update mirror interval failed: %v", err) + } + } + + return sess.Commit() +} diff --git a/models/repo.go b/models/repo.go index 8548accb5d..c654356916 100644 --- a/models/repo.go +++ b/models/repo.go @@ -815,7 +815,7 @@ func MigrateRepository(u *User, opts MigrateRepoOptions) (*Repository, error) { RepoID: repo.ID, Interval: setting.Mirror.DefaultInterval, EnablePrune: true, - NextUpdate: time.Now().Add(time.Duration(setting.Mirror.DefaultInterval) * time.Hour), + NextUpdate: time.Now().Add(setting.Mirror.DefaultInterval), }); err != nil { return repo, fmt.Errorf("InsertOne: %v", err) } diff --git a/models/repo_mirror.go b/models/repo_mirror.go index d8d4bc0bec..67ae449bde 100644 --- a/models/repo_mirror.go +++ b/models/repo_mirror.go @@ -27,8 +27,8 @@ type Mirror struct { ID int64 `xorm:"pk autoincr"` RepoID int64 `xorm:"INDEX"` Repo *Repository `xorm:"-"` - Interval int // Hour. - EnablePrune bool `xorm:"NOT NULL DEFAULT true"` + Interval time.Duration + EnablePrune bool `xorm:"NOT NULL DEFAULT true"` Updated time.Time `xorm:"-"` UpdatedUnix int64 `xorm:"INDEX"` @@ -68,7 +68,7 @@ func (m *Mirror) AfterSet(colName string, _ xorm.Cell) { // ScheduleNextUpdate calculates and sets next update time. func (m *Mirror) ScheduleNextUpdate() { - m.NextUpdate = time.Now().Add(time.Duration(m.Interval) * time.Hour) + m.NextUpdate = time.Now().Add(m.Interval) } func (m *Mirror) readAddress() { |