Skip to content

Commit 718e0db

Browse files
authored
Run Migrate in Install rather than just SyncTables (#17475) (#17486)
Backport #17475 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 6110ddc commit 718e0db

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

models/models.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,35 @@ func syncTables() error {
179179
return x.StoreEngine("InnoDB").Sync2(tables...)
180180
}
181181

182-
// NewTestEngine sets a new test xorm.Engine
183-
func NewTestEngine() (err error) {
182+
// NewInstallTestEngine creates a new xorm.Engine for testing during install
183+
//
184+
// This function will cause the basic database schema to be created
185+
func NewInstallTestEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
184186
x, err = GetNewEngine()
185187
if err != nil {
186-
return fmt.Errorf("Connect to database: %v", err)
188+
return fmt.Errorf("failed to connect to database: %w", err)
187189
}
188190

189191
x.SetMapper(names.GonicMapper{})
190192
x.SetLogger(NewXORMLogger(!setting.IsProd()))
191193
x.ShowSQL(!setting.IsProd())
194+
195+
x.SetDefaultContext(ctx)
196+
197+
if err = x.Ping(); err != nil {
198+
return err
199+
}
200+
201+
// We have to run migrateFunc here in case the user is re-running installation on a previously created DB.
202+
// If we do not then table schemas will be changed and there will be conflicts when the migrations run properly.
203+
//
204+
// Installation should only be being re-run if users want to recover an old database.
205+
// However, we should think carefully about should we support re-install on an installed instance,
206+
// as there may be other problems due to secret reinitialization.
207+
if err = migrateFunc(x); err != nil {
208+
return fmt.Errorf("migrate: %v", err)
209+
}
210+
192211
return syncTables()
193212
}
194213

routers/install/install.go

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

1717
"code.gitea.io/gitea/models"
18+
"code.gitea.io/gitea/models/migrations"
1819
"code.gitea.io/gitea/modules/base"
1920
"code.gitea.io/gitea/modules/context"
2021
"code.gitea.io/gitea/modules/generate"
@@ -207,7 +208,7 @@ func SubmitInstall(ctx *context.Context) {
207208
}
208209

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

0 commit comments

Comments
 (0)