diff options
author | zeripath <art27@cantab.net> | 2020-04-06 11:44:47 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-06 11:44:47 +0100 |
commit | d26885e2bf0a53af1c5c97c9d062f329250d8d20 (patch) | |
tree | 7dfe7646b121c0c77bed52ebe3bb9c41a1af5250 /models/migrations | |
parent | 1648bf2b60126f7959d92d176bf7f3cf3ae1cfe6 (diff) | |
download | gitea-d26885e2bf0a53af1c5c97c9d062f329250d8d20.tar.gz gitea-d26885e2bf0a53af1c5c97c9d062f329250d8d20.zip |
Mulitple Gitea Doctor improvements (#10943)
* Add `gitea doctor --list` flag to list the checks that will be run, including those by default
* Add `gitea doctor --run` to run specific checks
* Add `gitea doctor --all` to run all checks
* Add db version checker
* Add non-default recalculate merge bases check/fixer to doctor
* Add hook checker (Fix #9878) and ensure hooks are executable (Fix #6319)
* Fix authorized_keys checker - slight change of functionality here because parsing the command is fragile and we should just check if the authorized_keys file is essentially the same as what gitea would produce. (This is still not perfect as order matters - we should probably just md5sum the two files.)
* Add SCRIPT_TYPE check (Fix #10977)
* Add `gitea doctor --fix` to attempt to fix what is possible to easily fix
* Add `gitea doctor --log-file` to set the log-file, be it a file, stdout or to switch off completely. (Fixes previously undetected bug with certain xorm logging configurations - see @6543 comment.)
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'models/migrations')
-rw-r--r-- | models/migrations/migrations.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 847cd75d52..cad7f05f15 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -206,6 +206,52 @@ var migrations = []Migration{ NewMigration("Add OrgID column to Labels table", addOrgIDLabelColumn), } +// GetCurrentDBVersion returns the current db version +func GetCurrentDBVersion(x *xorm.Engine) (int64, error) { + if err := x.Sync(new(Version)); err != nil { + return -1, fmt.Errorf("sync: %v", err) + } + + currentVersion := &Version{ID: 1} + has, err := x.Get(currentVersion) + if err != nil { + return -1, fmt.Errorf("get: %v", err) + } + if !has { + return -1, nil + } + return currentVersion.Version, nil +} + +// ExpectedVersion returns the expected db version +func ExpectedVersion() int64 { + return int64(minDBVersion + len(migrations)) +} + +// EnsureUpToDate will check if the db is at the correct version +func EnsureUpToDate(x *xorm.Engine) error { + currentDB, err := GetCurrentDBVersion(x) + if err != nil { + return err + } + + if currentDB < 0 { + return fmt.Errorf("Database has not been initialised") + } + + if minDBVersion > currentDB { + return fmt.Errorf("DB version %d (<= %d) is too old for auto-migration. Upgrade to Gitea 1.6.4 first then upgrade to this version", currentDB, minDBVersion) + } + + expected := ExpectedVersion() + + if currentDB != expected { + return fmt.Errorf(`Current database version %d is not equal to the expected version %d. Please run "gitea [--config /path/to/app.ini] migrate" to update the database version`, currentDB, expected) + } + + return nil +} + // Migrate database to current version func Migrate(x *xorm.Engine) error { if err := x.Sync(new(Version)); err != nil { |