aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-11-07 11:11:27 +0800
committerGitHub <noreply@github.com>2021-11-07 11:11:27 +0800
commit69b61d437357235700306f28d22d21acc39bb2b5 (patch)
tree33af1d4d797b428556a323837fcc3eda38a54479 /models
parentc9110eb5e4657e018695f084f37e1b423939e9e0 (diff)
downloadgitea-69b61d437357235700306f28d22d21acc39bb2b5.tar.gz
gitea-69b61d437357235700306f28d22d21acc39bb2b5.zip
Fix bug on admin subcommand (#17533)
* Fix bug on admin subcommand * Add signals for all initDB Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'models')
-rwxr-xr-xmodels/db/engine.go55
1 files changed, 14 insertions, 41 deletions
diff --git a/models/db/engine.go b/models/db/engine.go
index 411e39a13b..d5f2470a7a 100755
--- a/models/db/engine.go
+++ b/models/db/engine.go
@@ -128,40 +128,8 @@ func syncTables() error {
return x.StoreEngine("InnoDB").Sync2(tables...)
}
-// InitInstallEngineWithMigration creates a new xorm.Engine for testing during install
-//
-// This function will cause the basic database schema to be created
-func InitInstallEngineWithMigration(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
- x, err = NewEngine()
- if err != nil {
- 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()
-}
-
// InitEngine sets the xorm.Engine
-func InitEngine() (err error) {
+func InitEngine(ctx context.Context) (err error) {
x, err = NewEngine()
if err != nil {
return fmt.Errorf("Failed to connect to database: %v", err)
@@ -175,6 +143,12 @@ func InitEngine() (err error) {
x.SetMaxOpenConns(setting.Database.MaxOpenConns)
x.SetMaxIdleConns(setting.Database.MaxIdleConns)
x.SetConnMaxLifetime(setting.Database.ConnMaxLifetime)
+
+ DefaultContext = &Context{
+ Context: ctx,
+ e: x,
+ }
+ x.SetDefaultContext(ctx)
return nil
}
@@ -184,21 +158,20 @@ func InitEngine() (err error) {
// that prevents the doctor from fixing anything in the database if the migration level
// is different from the expected value.
func InitEngineWithMigration(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
- if err = InitEngine(); err != nil {
+ if err = InitEngine(ctx); err != nil {
return err
}
- DefaultContext = &Context{
- Context: ctx,
- e: x,
- }
-
- 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)
}