Skip to content

Commit

Permalink
Run Migrate in Install rather than just SyncTables
Browse files Browse the repository at this point in the history
The underlying problem in go-gitea#17328 appears to be that users are re-running the install
page during upgrades. The function that tests and creates the db did not intend for
this and thus instead the migration scripts being run - a simple sync tables occurs.

This then causes a weird partially migrated DB which causes, in this release cycle,
the duplicate column in task table error. It is likely the cause of some weird
partial migration errors in other cycles too.

This PR simply ensures that the migration scripts are also run at this point too.

Fix go-gitea#17328

Signed-off-by: Andrew Thornton <[email protected]>
  • Loading branch information
zeripath committed Oct 28, 2021
1 parent 157de0f commit bea87be
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
13 changes: 12 additions & 1 deletion models/db/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func syncTables() error {
}

// NewTestEngine sets a new test xorm.Engine
func NewTestEngine() (err error) {
func NewTestEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
x, err = GetNewEngine()
if err != nil {
return fmt.Errorf("Connect to database: %v", err)
Expand All @@ -138,6 +138,17 @@ func NewTestEngine() (err error) {
x.SetMapper(names.GonicMapper{})
x.SetLogger(NewXORMLogger(!setting.IsProd))
x.ShowSQL(!setting.IsProd)

x.SetDefaultContext(ctx)

if err = x.Ping(); err != nil {
return err
}

if err = migrateFunc(x); err != nil {
return fmt.Errorf("migrate: %v", err)
}

return syncTables()
}

Expand Down
3 changes: 2 additions & 1 deletion routers/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/migrations"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/generate"
Expand Down Expand Up @@ -208,7 +209,7 @@ func SubmitInstall(ctx *context.Context) {
}

// Set test engine.
if err = db.NewTestEngine(); err != nil {
if err = db.NewTestEngine(ctx, migrations.Migrate); err != nil {
if strings.Contains(err.Error(), `Unknown database type: sqlite3`) {
ctx.Data["Err_DbType"] = true
ctx.RenderWithErr(ctx.Tr("install.sqlite3_not_available", "https://docs.gitea.io/en-us/install-from-binary/"), tplInstall, &form)
Expand Down

0 comments on commit bea87be

Please sign in to comment.