Skip to content

Commit 0a85537

Browse files
authored
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.
1 parent e2fa84f commit 0a85537

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

custom/conf/app.example.ini

+3
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ LOG_SQL = false ; if unset defaults to true
403403
;;
404404
;; Database maximum number of open connections, default is 0 meaning no maximum
405405
;MAX_OPEN_CONNS = 0
406+
;;
407+
;; Whether execute database models migrations automatically
408+
;AUTO_MIGRATION = true
406409

407410
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
408411
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+1
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ The following configuration set `Content-Type: application/vnd.android.package-a
444444
- `MAX_OPEN_CONNS` **0**: Database maximum open connections - default is 0, meaning there is no limit.
445445
- `MAX_IDLE_CONNS` **2**: Max idle database connections on connection pool, default is 2 - this will be capped to `MAX_OPEN_CONNS`.
446446
- `CONN_MAX_LIFETIME` **0 or 3s**: Sets the maximum amount of time a DB connection may be reused - default is 0, meaning there is no limit (except on MySQL where it is 3s - see #6804 & #7071).
447+
- `AUTO_MIGRATION` **true**: Whether execute database models migrations automatically.
447448

448449
Please see #8540 & #8273 for further discussion of the appropriate values for `MAX_OPEN_CONNS`, `MAX_IDLE_CONNS` & `CONN_MAX_LIFETIME` and their
449450
relation to port exhaustion.

modules/doctor/dbversion.go

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
)
1313

1414
func checkDBVersion(ctx context.Context, logger log.Logger, autofix bool) error {
15+
logger.Info("Expected database version: %d", migrations.ExpectedVersion())
1516
if err := db.InitEngineWithMigration(ctx, migrations.EnsureUpToDate); err != nil {
1617
if !autofix {
1718
logger.Critical("Error: %v during ensure up to date", err)

modules/setting/database.go

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
MaxOpenConns int
5050
ConnMaxLifetime time.Duration
5151
IterateBufferSize int
52+
AutoMigration bool
5253
}{
5354
Timeout: 500,
5455
IterateBufferSize: 50,
@@ -105,6 +106,7 @@ func InitDBConfig() {
105106
Database.LogSQL = sec.Key("LOG_SQL").MustBool(true)
106107
Database.DBConnectRetries = sec.Key("DB_RETRIES").MustInt(10)
107108
Database.DBConnectBackoff = sec.Key("DB_RETRY_BACKOFF").MustDuration(3 * time.Second)
109+
Database.AutoMigration = sec.Key("AUTO_MIGRATION").MustBool(true)
108110
}
109111

110112
// DBConnStr returns database connection string

routers/common/db.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"code.gitea.io/gitea/models/migrations"
1313
"code.gitea.io/gitea/modules/log"
1414
"code.gitea.io/gitea/modules/setting"
15+
16+
"xorm.io/xorm"
1517
)
1618

1719
// 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) {
2426
default:
2527
}
2628
log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.Database.DBConnectRetries)
27-
if err = db.InitEngineWithMigration(ctx, migrations.Migrate); err == nil {
29+
if err = db.InitEngineWithMigration(ctx, migrateWithSetting); err == nil {
2830
break
2931
} else if i == setting.Database.DBConnectRetries-1 {
3032
return err
@@ -36,3 +38,20 @@ func InitDBEngine(ctx context.Context) (err error) {
3638
db.HasEngine = true
3739
return nil
3840
}
41+
42+
func migrateWithSetting(x *xorm.Engine) error {
43+
if setting.Database.AutoMigration {
44+
return migrations.Migrate(x)
45+
}
46+
47+
if current, err := migrations.GetCurrentDBVersion(x); err != nil {
48+
return err
49+
} else if current < 0 {
50+
// execute migrations when the database isn't initialized even if AutoMigration is false
51+
return migrations.Migrate(x)
52+
} else if expected := migrations.ExpectedVersion(); current != expected {
53+
log.Fatal(`"database.AUTO_MIGRATION" is disabled, but current database version %d is not equal to the expected version %d.`+
54+
`You can set "database.AUTO_MIGRATION" to true or migrate manually by running "gitea [--config /path/to/app.ini] migrate"`, current, expected)
55+
}
56+
return nil
57+
}

0 commit comments

Comments
 (0)