-
Notifications
You must be signed in to change notification settings - Fork 1
/
screens.go
78 lines (62 loc) · 1.98 KB
/
screens.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"path"
)
func (m *Migrator) MigrateScreens() error {
collection, err := m.pbDao.FindCollectionByNameOrId("screens")
if err != nil {
return err
}
hasOldRecords := m.isNotEmptyCollection(collection)
insertedIds := make([]string, 0, 1000)
limit := 1000
items := make([]*v2Screen, 0, limit)
for i := 0; ; i++ {
q := m.oldDB.Select("*").
From("Screen").
OrderBy("id asc").
Limit(int64(limit)).
Offset(int64(i * limit))
if err := q.All(&items); err != nil {
return err
}
filesToCopy := make(map[string]string, len(items))
for _, item := range items {
insertedIds = append(insertedIds, m.buildRecordId(item.baseModel))
record := m.initRecordToMigrate(collection, item.baseModel)
if record == nil {
continue // already migrated
}
record.Set("created", item.CreatedAt)
record.Set("updated", item.UpdatedAt)
record.Set("prototype", fmt.Sprintf("%s%d", v2Prefix, item.PrototypeId))
record.Set("title", item.Title)
record.Set("alignment", item.Alignment)
record.Set("background", item.Background)
record.Set("fixedHeader", item.FixedHeader)
record.Set("fixedFooter", item.FixedFooter)
record.Set("file", path.Base(item.FilePath))
if err := m.pbDao.SaveRecord(record); err != nil {
// try to copy batched files so that we can continue from where we left
if copyErr := m.batchCopyFiles(filesToCopy, 500, "screen_file"); copyErr != nil {
return fmt.Errorf("failed to save %q and to copy all screen files: %w; %w", record.Id, err, copyErr)
}
return fmt.Errorf("failed to save %q: %w", record.Id, err)
}
// copy later on batches
filesToCopy[item.FilePath] = record.BaseFilesPath() + "/" + record.GetString("file")
}
if err := m.batchCopyFiles(filesToCopy, 500, "screen_file"); err != nil {
return err
}
if len(items) < limit {
break // no more items
}
items = items[:0]
}
if hasOldRecords {
return m.deleteMissingRecords(collection, insertedIds)
}
return nil
}