From 9434bc7a22c88e50a7f488662879353ea6bb2433 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 8 May 2018 18:08:26 +0800 Subject: [PATCH] fix restore --- cmd/restore.go | 15 ++++++++++----- models/models.go | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/cmd/restore.go b/cmd/restore.go index cdba8b8bb..f6d26047a 100644 --- a/cmd/restore.go +++ b/cmd/restore.go @@ -67,7 +67,7 @@ func runRestore(ctx *cli.Context) error { srcPath := os.Args[2] zip.Verbose = ctx.Bool("verbose") - log.Printf("Extracting %s to tmp work dir", srcPath) + log.Printf("Extracting %s to %s", srcPath, tmpWorkDir) err = zip.ExtractTo(srcPath, tmpWorkDir) if err != nil { log.Fatalf("Failed to extract %s to tmp work directory: %v", srcPath, err) @@ -101,7 +101,12 @@ func runRestore(ctx *cli.Context) error { log.Fatalf("Failed to SetEngine: %v", err) } - log.Printf("Restoring repo dir %s ...", setting.RepoRootPath) + err = models.SyncDBStructs() + if err != nil { + log.Fatalf("Failed to SyncDBStructs: %v", err) + } + + log.Printf("Restoring repo dir to %s ...", setting.RepoRootPath) repoPath := filepath.Join(tmpWorkDir, "repositories") err = os.RemoveAll(setting.RepoRootPath) if err != nil { @@ -113,7 +118,7 @@ func runRestore(ctx *cli.Context) error { log.Fatalf("Failed to move %s to %s: %v", repoPath, setting.RepoRootPath, err) } - log.Printf("Restoring custom dir %s ...", setting.CustomPath) + log.Printf("Restoring custom dir to %s ...", setting.CustomPath) customPath := filepath.Join(tmpWorkDir, "custom") err = os.RemoveAll(setting.CustomPath) if err != nil { @@ -125,7 +130,7 @@ func runRestore(ctx *cli.Context) error { log.Fatalf("Failed to move %s to %s: %v", customPath, setting.CustomPath, err) } - log.Printf("Restoring data dir %s ...", setting.AppDataPath) + log.Printf("Restoring data dir to %s ...", setting.AppDataPath) dataPath := filepath.Join(tmpWorkDir, "data") err = os.RemoveAll(setting.AppDataPath) if err != nil { @@ -137,8 +142,8 @@ func runRestore(ctx *cli.Context) error { log.Fatalf("Failed to move %s to %s: %v", dataPath, setting.AppDataPath, err) } - log.Printf("Restoring database from ...") dbPath := filepath.Join(tmpWorkDir, "database") + log.Printf("Restoring database from %s ...", dbPath) err = models.RestoreDatabaseFixtures(dbPath) if err != nil { log.Fatalf("Failed to restore database dir %s: %v", dbPath, err) diff --git a/models/models.go b/models/models.go index 69bfe7664..3b89fd2a9 100644 --- a/models/models.go +++ b/models/models.go @@ -298,6 +298,15 @@ func NewEngine(migrateFunc func(*xorm.Engine) error) (err error) { return nil } +// SyncDBStructs will sync database structs +func SyncDBStructs() error { + if err := x.StoreEngine("InnoDB").Sync2(tables...); err != nil { + return fmt.Errorf("sync database struct error: %v", err) + } + + return nil +} + // Statistic contains the database statistics type Statistic struct { Counter struct { @@ -421,7 +430,7 @@ func restoreTableFixtures(bean interface{}, dirPath string) error { const bufferSize = 100 var records = make([]map[string]interface{}, 0, bufferSize*10) - err = yaml.Unmarshal(data, records) + err = yaml.Unmarshal(data, &records) if err != nil { return err } @@ -439,7 +448,12 @@ func restoreTableFixtures(bean interface{}, dirPath string) error { qm := strings.Repeat("?,", len(columns)) qm = "(" + qm[:len(qm)-1] + ")" - var sql = "INSERT INTO " + table.Name + "(" + strings.Join(columns, ",") + ") VALUES " + _, err = x.Exec("DELETE FROM `" + table.Name + "`") + if err != nil { + return err + } + + var sql = "INSERT INTO `" + table.Name + "` (`" + strings.Join(columns, "`,`") + "`) VALUES " var args = make([]interface{}, 0, bufferSize) var insertSQLs = make([]string, 0, bufferSize) for i, vals := range records {