Skip to content

Commit ae0d4cc

Browse files
zeripathStelios Malathouras
authored and
Stelios Malathouras
committed
Run Migrate in Install rather than just SyncTables (go-gitea#17475)
* Run Migrate in Install rather than just SyncTables 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]>
1 parent c1036d5 commit ae0d4cc

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

models/db/engine.go

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

131-
// NewTestEngine sets a new test xorm.Engine
132-
func NewTestEngine() (err error) {
131+
// NewInstallTestEngine creates a new xorm.Engine for testing during install
132+
//
133+
// This function will cause the basic database schema to be created
134+
func NewInstallTestEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
133135
x, err = GetNewEngine()
134136
if err != nil {
135-
return fmt.Errorf("Connect to database: %v", err)
137+
return fmt.Errorf("failed to connect to database: %w", err)
136138
}
137139

138140
x.SetMapper(names.GonicMapper{})
139141
x.SetLogger(NewXORMLogger(!setting.IsProd))
140142
x.ShowSQL(!setting.IsProd)
143+
144+
x.SetDefaultContext(ctx)
145+
146+
if err = x.Ping(); err != nil {
147+
return err
148+
}
149+
150+
// We have to run migrateFunc here in case the user is re-running installation on a previously created DB.
151+
// If we do not then table schemas will be changed and there will be conflicts when the migrations run properly.
152+
//
153+
// Installation should only be being re-run if users want to recover an old database.
154+
// However, we should think carefully about should we support re-install on an installed instance,
155+
// as there may be other problems due to secret reinitialization.
156+
if err = migrateFunc(x); err != nil {
157+
return fmt.Errorf("migrate: %v", err)
158+
}
159+
141160
return syncTables()
142161
}
143162

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.NewInstallTestEngine(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)