diff options
author | Jason Song <i@wolfogre.com> | 2022-12-07 23:58:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-07 09:58:31 -0600 |
commit | 0a85537c79b65f3b10a4b66265a23a8de46a2e43 (patch) | |
tree | 882703646819e463b64fec8fc436b8087ad5fd1b /routers | |
parent | e2fa84fddc80368c0315616870b8c75592397422 (diff) | |
download | gitea-0a85537c79b65f3b10a4b66265a23a8de46a2e43.tar.gz gitea-0a85537c79b65f3b10a4b66265a23a8de46a2e43.zip |
Support disabling database auto migration (#22053)
Gitea will migrate the database model version automatically, but it
should be able to be disabled and keep Gitea shutdown if the version is
not matched.
Diffstat (limited to 'routers')
-rw-r--r-- | routers/common/db.go | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/routers/common/db.go b/routers/common/db.go index ac082ab36f..2e86fbd0fd 100644 --- a/routers/common/db.go +++ b/routers/common/db.go @@ -12,6 +12,8 @@ import ( "code.gitea.io/gitea/models/migrations" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + + "xorm.io/xorm" ) // InitDBEngine In case of problems connecting to DB, retry connection. Eg, PGSQL in Docker Container on Synology @@ -24,7 +26,7 @@ func InitDBEngine(ctx context.Context) (err error) { default: } log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.Database.DBConnectRetries) - if err = db.InitEngineWithMigration(ctx, migrations.Migrate); err == nil { + if err = db.InitEngineWithMigration(ctx, migrateWithSetting); err == nil { break } else if i == setting.Database.DBConnectRetries-1 { return err @@ -36,3 +38,20 @@ func InitDBEngine(ctx context.Context) (err error) { db.HasEngine = true return nil } + +func migrateWithSetting(x *xorm.Engine) error { + if setting.Database.AutoMigration { + return migrations.Migrate(x) + } + + if current, err := migrations.GetCurrentDBVersion(x); err != nil { + return err + } else if current < 0 { + // execute migrations when the database isn't initialized even if AutoMigration is false + return migrations.Migrate(x) + } else if expected := migrations.ExpectedVersion(); current != expected { + log.Fatal(`"database.AUTO_MIGRATION" is disabled, but current database version %d is not equal to the expected version %d.`+ + `You can set "database.AUTO_MIGRATION" to true or migrate manually by running "gitea [--config /path/to/app.ini] migrate"`, current, expected) + } + return nil +} |