diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 4caf60e0ed..bc5bf929e2 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -585,6 +585,12 @@ func EnsureUpToDate(x *xorm.Engine) error { return nil } +// EnsureUpToDate will check if the db is completely new +func IsFreshDB(x *xorm.Engine) (bool, error) { + exist, err := x.IsTableExist(&xormigrate.Migration{}) + return !exist, err +} + // Migrate database to current version func Migrate(x *xorm.Engine) error { // Set a new clean the default mapper to GonicMapper as that is the default for Gitea. diff --git a/routers/common/db.go b/routers/common/db.go index a67c9582fa..e81b1e22c8 100644 --- a/routers/common/db.go +++ b/routers/common/db.go @@ -46,14 +46,14 @@ func migrateWithSetting(x *xorm.Engine) error { return migrations.Migrate(x) } - if current, err := migrations.GetCurrentDBVersion(x); err != nil { + if fresh, err := migrations.IsFreshDB(x); err != nil { return err - } else if current < 0 { + } else if fresh { // 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) + } else if upToDate := migrations.EnsureUpToDate(x); upToDate != nil { + log.Fatal(`"database.AUTO_MIGRATION" is disabled, but current database misses migrations.` + + `You can set "database.AUTO_MIGRATION" to true or migrate manually by running "gitea [--config /path/to/app.ini] migrate"`) } return nil } diff --git a/services/doctor/dbversion.go b/services/doctor/dbversion.go index 2b20cb2340..3ddca92fb3 100644 --- a/services/doctor/dbversion.go +++ b/services/doctor/dbversion.go @@ -12,7 +12,6 @@ import ( ) func checkDBVersion(ctx context.Context, logger log.Logger, autofix bool) error { - logger.Info("Expected database version: %d", migrations.ExpectedVersion()) if err := db.InitEngineWithMigration(ctx, migrations.EnsureUpToDate); err != nil { if !autofix { logger.Critical("Error: %v during ensure up to date", err)