Refactor the DB migration system slightly (#32344)
Introduce "idNumber" for each migration, and clarify the difference between the migration ID number and database version.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
28
models/migrations/migrations_test.go
Normal file
28
models/migrations/migrations_test.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
package migrations
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/modules/test"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMigrations(t *testing.T) {
|
||||||
|
defer test.MockVariableValue(&preparedMigrations)()
|
||||||
|
preparedMigrations = []*migration{
|
||||||
|
{idNumber: 70},
|
||||||
|
{idNumber: 71},
|
||||||
|
}
|
||||||
|
assert.EqualValues(t, 72, calcDBVersion(preparedMigrations))
|
||||||
|
assert.EqualValues(t, 72, ExpectedDBVersion())
|
||||||
|
|
||||||
|
assert.EqualValues(t, 71, migrationIDNumberToDBVersion(70))
|
||||||
|
|
||||||
|
assert.EqualValues(t, []*migration{{idNumber: 70}, {idNumber: 71}}, getPendingMigrations(70, preparedMigrations))
|
||||||
|
assert.EqualValues(t, []*migration{{idNumber: 71}}, getPendingMigrations(71, preparedMigrations))
|
||||||
|
assert.EqualValues(t, []*migration{}, getPendingMigrations(72, preparedMigrations))
|
||||||
|
}
|
@ -51,7 +51,7 @@ func migrateWithSetting(x *xorm.Engine) error {
|
|||||||
} else if current < 0 {
|
} else if current < 0 {
|
||||||
// execute migrations when the database isn't initialized even if AutoMigration is false
|
// execute migrations when the database isn't initialized even if AutoMigration is false
|
||||||
return migrations.Migrate(x)
|
return migrations.Migrate(x)
|
||||||
} else if expected := migrations.ExpectedVersion(); current != expected {
|
} else if expected := migrations.ExpectedDBVersion(); current != expected {
|
||||||
log.Fatal(`"database.AUTO_MIGRATION" is disabled, but current database version %d is not equal to the expected version %d.`+
|
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)
|
`You can set "database.AUTO_MIGRATION" to true or migrate manually by running "gitea [--config /path/to/app.ini] migrate"`, current, expected)
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func checkDBVersion(ctx context.Context, logger log.Logger, autofix bool) error {
|
func checkDBVersion(ctx context.Context, logger log.Logger, autofix bool) error {
|
||||||
logger.Info("Expected database version: %d", migrations.ExpectedVersion())
|
logger.Info("Expected database version: %d", migrations.ExpectedDBVersion())
|
||||||
if err := db.InitEngineWithMigration(ctx, migrations.EnsureUpToDate); err != nil {
|
if err := db.InitEngineWithMigration(ctx, migrations.EnsureUpToDate); err != nil {
|
||||||
if !autofix {
|
if !autofix {
|
||||||
logger.Critical("Error: %v during ensure up to date", err)
|
logger.Critical("Error: %v during ensure up to date", err)
|
||||||
|
Reference in New Issue
Block a user