]> source.dussan.org Git - gitea.git/commitdiff
Run Migrate in Install rather than just SyncTables (#17475) (#17486)
authorzeripath <art27@cantab.net>
Sat, 30 Oct 2021 09:28:11 +0000 (10:28 +0100)
committerGitHub <noreply@github.com>
Sat, 30 Oct 2021 09:28:11 +0000 (10:28 +0100)
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 <art27@cantab.net>
models/models.go
routers/install/install.go

index 4b157b2a04d27792045e481c4702da59870b5362..3b1fe91633f8ed1fcdf8b744290406d0b33e7958 100644 (file)
@@ -179,16 +179,35 @@ func syncTables() error {
        return x.StoreEngine("InnoDB").Sync2(tables...)
 }
 
-// NewTestEngine sets a new test xorm.Engine
-func NewTestEngine() (err error) {
+// NewInstallTestEngine creates a new xorm.Engine for testing during install
+//
+// This function will cause the basic database schema to be created
+func NewInstallTestEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
        x, err = GetNewEngine()
        if err != nil {
-               return fmt.Errorf("Connect to database: %v", err)
+               return fmt.Errorf("failed to connect to database: %w", err)
        }
 
        x.SetMapper(names.GonicMapper{})
        x.SetLogger(NewXORMLogger(!setting.IsProd()))
        x.ShowSQL(!setting.IsProd())
+
+       x.SetDefaultContext(ctx)
+
+       if err = x.Ping(); err != nil {
+               return err
+       }
+
+       // We have to run migrateFunc here in case the user is re-running installation on a previously created DB.
+       // If we do not then table schemas will be changed and there will be conflicts when the migrations run properly.
+       //
+       // Installation should only be being re-run if users want to recover an old database.
+       // However, we should think carefully about should we support re-install on an installed instance,
+       // as there may be other problems due to secret reinitialization.
+       if err = migrateFunc(x); err != nil {
+               return fmt.Errorf("migrate: %v", err)
+       }
+
        return syncTables()
 }
 
index ad985cf1848822ce9f9cde1644d0ff0ffbd0d659..3059278c46a3481ab9c241fa8954d048381aacc9 100644 (file)
@@ -15,6 +15,7 @@ import (
        "time"
 
        "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/models/migrations"
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/generate"
@@ -207,7 +208,7 @@ func SubmitInstall(ctx *context.Context) {
        }
 
        // Set test engine.
-       if err = models.NewTestEngine(); err != nil {
+       if err = models.NewInstallTestEngine(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)