fix restore

This commit is contained in:
Lunny Xiao 2018-05-08 18:08:26 +08:00
parent 22edc16336
commit 9434bc7a22
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 26 additions and 7 deletions

View File

@ -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)

View File

@ -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 {