Skip to content

Commit 883006a

Browse files
committed
Rename NewTestEngine to NewInstallTestEngine
Signed-off-by: Andrew Thornton <[email protected]>
1 parent d09d9b5 commit 883006a

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

models/db/engine.go

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

131-
// NewTestEngine sets a new test xorm.Engine
132-
func NewTestEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (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{})
@@ -145,6 +147,12 @@ func NewTestEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (e
145147
return err
146148
}
147149

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.
148156
if err = migrateFunc(x); err != nil {
149157
return fmt.Errorf("migrate: %v", err)
150158
}
@@ -192,10 +200,6 @@ func NewEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err e
192200
}
193201

194202
if err = migrateFunc(x); err != nil {
195-
// The only case to re-run the installation on an installed instance is: users may want to recover an old database.
196-
// In such case, we should run the migrations first,
197-
// otherwise the table schemas may conflict in the normal startup migrations, because `syncTables` below already changed the schemas to the latest.
198-
// However, we should think carefully about should we support re-install on an installed instance, it may bring other problems, eg: secrets will be lost.
199203
return fmt.Errorf("migrate: %v", err)
200204
}
201205

routers/install/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func SubmitInstall(ctx *context.Context) {
209209
}
210210

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

0 commit comments

Comments
 (0)