-
Notifications
You must be signed in to change notification settings - Fork 111
/
migration_item.go
271 lines (240 loc) · 8.05 KB
/
migration_item.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package catalog
import (
"context"
"errors"
"strings"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/pkg/fileutil"
"github.com/rilldata/rill/runtime/services/catalog/artifacts"
"github.com/rilldata/rill/runtime/services/catalog/migrator"
)
type MigrationItem struct {
Name string
NormalizedName string
Path string
Type MigrationType
CatalogInFile *drivers.CatalogEntry
CatalogInStore *drivers.CatalogEntry
NewCatalog *drivers.CatalogEntry
HasChanged bool
FromName string
FromNormalizedName string
FromPath string
NormalizedDependencies []string
Error *runtimev1.ReconcileError
}
func (i *MigrationItem) renameFrom(name, path string) {
i.Type = MigrationRename
i.FromName = name
i.FromNormalizedName = normalizeName(name)
i.FromPath = path
}
type MigrationType int
const (
MigrationNoChange MigrationType = 0
MigrationCreate MigrationType = 1
MigrationRename MigrationType = 2
MigrationUpdate MigrationType = 3
MigrationReportUpdate MigrationType = 4
MigrationDelete MigrationType = 5
)
func (s *Service) getMigrationItems(
ctx context.Context,
repoPath string,
storeObjectsMap map[string]*drivers.CatalogEntry,
forcedPathMap map[string]bool,
) []*MigrationItem {
var items []*MigrationItem
// primary item for repoPath
var item *MigrationItem
hasFileObject := false
catalog, err := artifacts.Read(ctx, s.Repo, s.RegistryStore, s.InstID, repoPath)
if err != nil {
item = s.newMigrationItemFromError(repoPath, err)
items = []*MigrationItem{item}
} else {
item, items = s.newMigrationItemFromFile(ctx, repoPath, catalog, storeObjectsMap)
items = append(items, item)
hasFileObject = true
}
// mark the item as changed if it was forced to update
item.HasChanged = forcedPathMap[repoPath]
if hasFileObject {
s.checkFileChange(ctx, item)
}
catalogInStore, hasStoreObject := storeObjectsMap[item.NormalizedName]
if hasStoreObject {
item.CatalogInStore = catalogInStore
if !hasFileObject {
item.NewCatalog = catalogInStore
}
}
if item.Type == MigrationNoChange && (item.HasChanged || !hasStoreObject) {
if hasStoreObject {
item.Type = MigrationUpdate
} else {
item.Type = MigrationCreate
}
}
if !hasFileObject && !hasStoreObject {
// invalid file created/updated
// do not run any migration on this, changes are already saved to the file
item.Type = MigrationNoChange
if errors.Is(err, artifacts.ErrFileRead) {
// the item is possibly for a file that doesn't exist but was passed in ChangedPaths
return nil
}
}
if hasFileObject && hasStoreObject {
if item.Name != catalogInStore.Name {
// rename with same name different case
item.renameFrom(catalogInStore.Name, item.Path)
}
switch item.Type {
case MigrationCreate:
if migrator.IsEqual(ctx, item.CatalogInFile, item.CatalogInStore) && !item.HasChanged {
// if the actual content has not changed, mark as MigrationNoChange
item.Type = MigrationNoChange
} else {
// else mark as MigrationUpdate
item.Type = MigrationUpdate
}
case MigrationNoChange:
// if item doesn't exist in olap, mark as create
// happens when the catalog table is modified directly in some way
ok, _ := migrator.ExistsInOlap(ctx, s.Olap, item.CatalogInFile)
if !ok {
item.Type = MigrationCreate
}
}
}
return items
}
func (s *Service) newMigrationItemFromError(repoPath string, err error) *MigrationItem {
item := &MigrationItem{
Type: MigrationNoChange,
Path: repoPath,
}
if !errors.Is(err, artifacts.ErrFileRead) {
item.Error = &runtimev1.ReconcileError{
Code: runtimev1.ReconcileError_CODE_SYNTAX,
Message: err.Error(),
FilePath: repoPath,
}
}
item.Type = MigrationDelete
item.Name = fileutil.Stem(repoPath)
item.NormalizedName = normalizeName(item.Name)
return item
}
func (s *Service) newMigrationItemFromFile(
ctx context.Context,
repoPath string,
catalogInFile *drivers.CatalogEntry,
storeObjectsMap map[string]*drivers.CatalogEntry,
) (*MigrationItem, []*MigrationItem) {
item := &MigrationItem{
Type: MigrationNoChange,
Path: repoPath,
Name: catalogInFile.Name,
NormalizedName: normalizeName(catalogInFile.Name),
CatalogInFile: catalogInFile,
NewCatalog: catalogInFile,
}
item.Name = catalogInFile.Name
item.NormalizedName = normalizeName(item.Name)
item.CatalogInFile = catalogInFile
normalizedDependencies, embeddedEntries := migrator.GetDependencies(ctx, s.Olap, item.CatalogInFile)
// convert dependencies to lower case
for i, dep := range normalizedDependencies {
normalizedDependencies[i] = normalizeName(dep)
}
item.NormalizedDependencies = normalizedDependencies
return item, s.resolveDependencies(item, storeObjectsMap, embeddedEntries)
}
func (s *Service) checkFileChange(ctx context.Context, item *MigrationItem) {
repoStat, _ := s.Repo.Stat(ctx, s.InstID, item.Path)
catalogLastUpdated, _ := migrator.LastUpdated(ctx, s.InstID, s.Repo, item.CatalogInFile)
if repoStat.LastUpdated.After(catalogLastUpdated) {
item.CatalogInFile.UpdatedOn = repoStat.LastUpdated
} else {
item.CatalogInFile.UpdatedOn = catalogLastUpdated
// if catalog has changed in any way then always re-create/update
item.HasChanged = true
}
if item.CatalogInFile.UpdatedOn.After(s.Meta.LastMigration) {
// assume creation until we see a catalog object
item.Type = MigrationCreate
}
}
func (s *Service) resolveDependencies(
item *MigrationItem,
storeObjectsMap map[string]*drivers.CatalogEntry,
embeddedEntries []*drivers.CatalogEntry,
) []*MigrationItem {
items := make([]*MigrationItem, 0)
prevEmbeddedEntries := make(map[string]bool)
prevDependencies := s.Meta.dag.GetParents(item.NormalizedName)
for _, prevDependency := range prevDependencies {
if _, ok := storeObjectsMap[prevDependency]; !ok {
// embedded entries not created because ingestion failed
// it gets added to DAG but not to the catalog store
// treat it as a new dependency in this case
continue
}
prevEmbeddedEntries[prevDependency] = true
}
for _, embeddedEntry := range embeddedEntries {
normalizedEmbeddedName := normalizeName(embeddedEntry.Name)
if prevEmbeddedEntries[normalizedEmbeddedName] {
// delete from map for unchanged embedded entry.
// this map will later be used to remove link from previously embedded entry
delete(prevEmbeddedEntries, normalizedEmbeddedName)
continue
}
embeddedItem := s.newEmbeddedMigrationItem(embeddedEntry, MigrationCreate)
if existingEntry, ok := storeObjectsMap[embeddedItem.NormalizedName]; ok {
// update the catalog for embedded entry to the one from store
embeddedItem.CatalogInFile = existingEntry
embeddedItem.CatalogInStore = existingEntry
embeddedItem.Type = MigrationReportUpdate
}
items = append(items, embeddedItem)
}
// go through previous embedded entries not embedded anymore
for prevEmbeddedEntry := range prevEmbeddedEntries {
existingEntry, ok := storeObjectsMap[prevEmbeddedEntry]
if !ok || !existingEntry.Embedded {
// should not happen
continue
}
embeddedItem := s.newEmbeddedMigrationItem(existingEntry, MigrationReportUpdate)
items = append(items, embeddedItem)
}
return items
}
func (s *Service) newEmbeddedMigrationItem(newEntry *drivers.CatalogEntry, migrationType MigrationType) *MigrationItem {
return &MigrationItem{
Name: newEntry.Name,
NormalizedName: strings.ToLower(newEntry.Name),
Path: newEntry.Path,
CatalogInFile: newEntry,
CatalogInStore: newEntry,
NewCatalog: newEntry,
Type: migrationType,
}
}
func (s *Service) newDeleteMigrationItem(entry *drivers.CatalogEntry) *MigrationItem {
return &MigrationItem{
Name: entry.Name,
NormalizedName: normalizeName(entry.Name),
Type: MigrationDelete,
Path: entry.Path,
CatalogInStore: entry,
NewCatalog: entry,
}
}
func normalizeName(name string) string {
return strings.ToLower(name)
}