Skip to content

Commit bea87be

Browse files
committed
Run Migrate in Install rather than just SyncTables
The underlying problem in #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 #17328 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 157de0f commit bea87be

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

models/db/engine.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func syncTables() error {
129129
}
130130

131131
// NewTestEngine sets a new test xorm.Engine
132-
func NewTestEngine() (err error) {
132+
func NewTestEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
133133
x, err = GetNewEngine()
134134
if err != nil {
135135
return fmt.Errorf("Connect to database: %v", err)
@@ -138,6 +138,17 @@ func NewTestEngine() (err error) {
138138
x.SetMapper(names.GonicMapper{})
139139
x.SetLogger(NewXORMLogger(!setting.IsProd))
140140
x.ShowSQL(!setting.IsProd)
141+
142+
x.SetDefaultContext(ctx)
143+
144+
if err = x.Ping(); err != nil {
145+
return err
146+
}
147+
148+
if err = migrateFunc(x); err != nil {
149+
return fmt.Errorf("migrate: %v", err)
150+
}
151+
141152
return syncTables()
142153
}
143154

routers/install/install.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"code.gitea.io/gitea/models"
1818
"code.gitea.io/gitea/models/db"
19+
"code.gitea.io/gitea/models/migrations"
1920
"code.gitea.io/gitea/modules/base"
2021
"code.gitea.io/gitea/modules/context"
2122
"code.gitea.io/gitea/modules/generate"
@@ -208,7 +209,7 @@ func SubmitInstall(ctx *context.Context) {
208209
}
209210

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

0 commit comments

Comments
 (0)