diff options
author | zeripath <art27@cantab.net> | 2021-10-29 09:23:10 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-29 09:23:10 +0100 |
commit | e6e3b212b3ed01705ac6fb746f87df8204c8102c (patch) | |
tree | 4203cf44220fc5fcfdcb04320deae5b3adacd492 /models | |
parent | 9733b60430d5abf7de6f7137129d604d78df9e74 (diff) | |
download | gitea-e6e3b212b3ed01705ac6fb746f87df8204c8102c.tar.gz gitea-e6e3b212b3ed01705ac6fb746f87df8204c8102c.zip |
Run Migrate in Install rather than just SyncTables (#17475)
* 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 <art27@cantab.net>
Diffstat (limited to 'models')
-rwxr-xr-x | models/db/engine.go | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/models/db/engine.go b/models/db/engine.go index 78b4ac22dd..bd55aa7a03 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -128,16 +128,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() } |