Skip to content

Commit

Permalink
only sync if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed May 27, 2020
1 parent cba3390 commit a9dcb3e
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cmd/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ func runDoctorAuthorizedKeys(ctx *cli.Context) ([]string, error) {
}

func runDoctorCheckDBVersion(ctx *cli.Context) ([]string, error) {
if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil {
if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate, false); err != nil {
if ctx.Bool("fix") {
return []string{fmt.Sprintf("WARN: Got Error %v during ensure up to date", err), "Attempting to migrate to the latest DB version to fix this."}, models.NewEngine(context.Background(), migrations.Migrate)
return []string{fmt.Sprintf("WARN: Got Error %v during ensure up to date", err), "Attempting to migrate to the latest DB version to fix this."}, models.NewEngine(context.Background(), migrations.Migrate, true)
}
return nil, err
}
Expand Down Expand Up @@ -507,7 +507,7 @@ func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) {
var outdatedDB bool

// make sure DB version is uptodate
if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate); err != nil {
if err := models.NewEngine(context.Background(), migrations.EnsureUpToDate, false); err != nil {
results = append(results, "Warning: model version on the database does not match the current Gitea version. Model consistency can be checked but not fixed until the database is upgraded.")
outdatedDB = true
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func runMigrate(ctx *cli.Context) error {
log.Trace("Log path: %s", setting.LogRootPath)
setting.InitDBConfig()

if err := models.NewEngine(context.Background(), migrations.Migrate); err != nil {
if err := models.NewEngine(context.Background(), migrations.Migrate, true); err != nil {
log.Fatal("Failed to initialize ORM engine: %v", err)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/pr/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func runPR() {
var helper testfixtures.Helper = &testfixtures.SQLite{}
models.NewEngine(context.Background(), func(_ *xorm.Engine) error {
return nil
})
}, true)
models.HasEngine = true
//x.ShowSQL(true)
err = models.InitFixtures(
Expand Down
2 changes: 1 addition & 1 deletion integrations/migration-test/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func doMigrationTest(t *testing.T, version string) {
err := models.SetEngine()
assert.NoError(t, err)

err = models.NewEngine(context.Background(), wrappedMigrate)
err = models.NewEngine(context.Background(), wrappedMigrate, true)
assert.NoError(t, err)
currentEngine.Close()
}
Expand Down
8 changes: 5 additions & 3 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func SetEngine() (err error) {
}

// NewEngine initializes a new xorm.Engine
func NewEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err error) {
func NewEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error, sync bool) (err error) {
if err = SetEngine(); err != nil {
return err
}
Expand All @@ -197,8 +197,10 @@ func NewEngine(ctx context.Context, migrateFunc func(*xorm.Engine) error) (err e
return fmt.Errorf("migrate: %v", err)
}

if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
return fmt.Errorf("sync database struct error: %v", err)
if sync {
if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
return fmt.Errorf("sync database struct error: %v", err)
}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func initDBEngine(ctx context.Context) (err error) {
default:
}
log.Info("ORM engine initialization attempt #%d/%d...", i+1, setting.Database.DBConnectRetries)
if err = models.NewEngine(ctx, migrations.Migrate); err == nil {
if err = models.NewEngine(ctx, migrations.Migrate, true); err == nil {
break
} else if i == setting.Database.DBConnectRetries-1 {
return err
Expand Down

0 comments on commit a9dcb3e

Please sign in to comment.