Skip to content

Commit 9434bc7

Browse files
committed
fix restore
1 parent 22edc16 commit 9434bc7

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

cmd/restore.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func runRestore(ctx *cli.Context) error {
6767
srcPath := os.Args[2]
6868

6969
zip.Verbose = ctx.Bool("verbose")
70-
log.Printf("Extracting %s to tmp work dir", srcPath)
70+
log.Printf("Extracting %s to %s", srcPath, tmpWorkDir)
7171
err = zip.ExtractTo(srcPath, tmpWorkDir)
7272
if err != nil {
7373
log.Fatalf("Failed to extract %s to tmp work directory: %v", srcPath, err)
@@ -101,7 +101,12 @@ func runRestore(ctx *cli.Context) error {
101101
log.Fatalf("Failed to SetEngine: %v", err)
102102
}
103103

104-
log.Printf("Restoring repo dir %s ...", setting.RepoRootPath)
104+
err = models.SyncDBStructs()
105+
if err != nil {
106+
log.Fatalf("Failed to SyncDBStructs: %v", err)
107+
}
108+
109+
log.Printf("Restoring repo dir to %s ...", setting.RepoRootPath)
105110
repoPath := filepath.Join(tmpWorkDir, "repositories")
106111
err = os.RemoveAll(setting.RepoRootPath)
107112
if err != nil {
@@ -113,7 +118,7 @@ func runRestore(ctx *cli.Context) error {
113118
log.Fatalf("Failed to move %s to %s: %v", repoPath, setting.RepoRootPath, err)
114119
}
115120

116-
log.Printf("Restoring custom dir %s ...", setting.CustomPath)
121+
log.Printf("Restoring custom dir to %s ...", setting.CustomPath)
117122
customPath := filepath.Join(tmpWorkDir, "custom")
118123
err = os.RemoveAll(setting.CustomPath)
119124
if err != nil {
@@ -125,7 +130,7 @@ func runRestore(ctx *cli.Context) error {
125130
log.Fatalf("Failed to move %s to %s: %v", customPath, setting.CustomPath, err)
126131
}
127132

128-
log.Printf("Restoring data dir %s ...", setting.AppDataPath)
133+
log.Printf("Restoring data dir to %s ...", setting.AppDataPath)
129134
dataPath := filepath.Join(tmpWorkDir, "data")
130135
err = os.RemoveAll(setting.AppDataPath)
131136
if err != nil {
@@ -137,8 +142,8 @@ func runRestore(ctx *cli.Context) error {
137142
log.Fatalf("Failed to move %s to %s: %v", dataPath, setting.AppDataPath, err)
138143
}
139144

140-
log.Printf("Restoring database from ...")
141145
dbPath := filepath.Join(tmpWorkDir, "database")
146+
log.Printf("Restoring database from %s ...", dbPath)
142147
err = models.RestoreDatabaseFixtures(dbPath)
143148
if err != nil {
144149
log.Fatalf("Failed to restore database dir %s: %v", dbPath, err)

models/models.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,15 @@ func NewEngine(migrateFunc func(*xorm.Engine) error) (err error) {
298298
return nil
299299
}
300300

301+
// SyncDBStructs will sync database structs
302+
func SyncDBStructs() error {
303+
if err := x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
304+
return fmt.Errorf("sync database struct error: %v", err)
305+
}
306+
307+
return nil
308+
}
309+
301310
// Statistic contains the database statistics
302311
type Statistic struct {
303312
Counter struct {
@@ -421,7 +430,7 @@ func restoreTableFixtures(bean interface{}, dirPath string) error {
421430

422431
const bufferSize = 100
423432
var records = make([]map[string]interface{}, 0, bufferSize*10)
424-
err = yaml.Unmarshal(data, records)
433+
err = yaml.Unmarshal(data, &records)
425434
if err != nil {
426435
return err
427436
}
@@ -439,7 +448,12 @@ func restoreTableFixtures(bean interface{}, dirPath string) error {
439448
qm := strings.Repeat("?,", len(columns))
440449
qm = "(" + qm[:len(qm)-1] + ")"
441450

442-
var sql = "INSERT INTO " + table.Name + "(" + strings.Join(columns, ",") + ") VALUES "
451+
_, err = x.Exec("DELETE FROM `" + table.Name + "`")
452+
if err != nil {
453+
return err
454+
}
455+
456+
var sql = "INSERT INTO `" + table.Name + "` (`" + strings.Join(columns, "`,`") + "`) VALUES "
443457
var args = make([]interface{}, 0, bufferSize)
444458
var insertSQLs = make([]string, 0, bufferSize)
445459
for i, vals := range records {

0 commit comments

Comments
 (0)