-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_helper_iterator.go
141 lines (114 loc) · 3.44 KB
/
migration_helper_iterator.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
package anser
import (
"context"
"sync"
"github.com/cdr/amboy/job"
"github.com/deciduosity/anser/client"
"github.com/deciduosity/anser/model"
"github.com/cdr/grip"
"github.com/cdr/grip/message"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
type errorMigrationIterator struct {
err error
}
func (e *errorMigrationIterator) Err() error { return e.err }
func (e *errorMigrationIterator) Close() error { return nil }
func (e *errorMigrationIterator) Next(_ context.Context) bool { return false }
func (e *errorMigrationIterator) Item() *model.MigrationMetadata { return nil }
type cursorMigrationMetadataIterator struct {
cursor client.Cursor
catcher grip.Catcher
ctx context.Context
}
func (c *cursorMigrationMetadataIterator) Err() error { return c.catcher.Resolve() }
func (c *cursorMigrationMetadataIterator) Close() error { return c.cursor.Close(context.TODO()) }
func (c *cursorMigrationMetadataIterator) Next(ctx context.Context) bool {
ret := c.cursor.Next(ctx)
c.catcher.AddWhen(!ret, c.cursor.Err())
return ret
}
func (c *cursorMigrationMetadataIterator) Item() *model.MigrationMetadata {
o := &model.MigrationMetadata{}
c.catcher.Add(c.cursor.Decode(o))
return o
}
func NewMigrationHelper(e Environment) MigrationHelper { return &migrationBase{env: e} }
type migrationBase struct {
env Environment
sync.Mutex
}
func (m *migrationBase) Env() Environment {
m.Lock()
defer m.Unlock()
if m.env == nil {
m.env = GetEnvironment()
}
return m.env
}
func (m *migrationBase) SaveMigrationEvent(ctx context.Context, mm *model.MigrationMetadata) error {
env := m.Env()
client, err := env.GetClient()
if err != nil {
return errors.WithStack(err)
}
ns := env.MetadataNamespace()
res, err := client.Database(ns.DB).Collection(ns.Collection).ReplaceOne(ctx,
bson.M{"_id": mm.ID},
mm, options.Replace().SetUpsert(true))
if err != nil {
return errors.WithStack(err)
}
if res.MatchedCount+res.UpsertedCount+res.ModifiedCount < 1 {
return errors.Errorf("migration event was not saved for '%s'", mm.ID)
}
return nil
}
func (m *migrationBase) FinishMigration(ctx context.Context, name string, j *job.Base) {
j.MarkComplete()
meta := model.MigrationMetadata{
ID: j.ID(),
Migration: name,
HasErrors: j.HasErrors(),
Completed: true,
}
err := m.SaveMigrationEvent(ctx, &meta)
if err != nil {
j.AddError(err)
grip.Warning(message.Fields{
"message": "encountered problem saving migration event",
"id": j.ID(),
"name": name,
"error": err.Error(),
"type": "client",
})
return
}
grip.Debug(message.Fields{
"message": "completed migration",
"id": j.ID(),
"name": name,
"metadata": meta,
})
}
func (m *migrationBase) PendingMigrationOperations(ctx context.Context, ns model.Namespace, q map[string]interface{}) int {
return 0
}
func (m *migrationBase) GetMigrationEvents(ctx context.Context, q map[string]interface{}) MigrationMetadataIterator {
env := m.Env()
client, err := env.GetClient()
if err != nil {
return &errorMigrationIterator{err: err}
}
ns := env.MetadataNamespace()
cursor, err := client.Database(ns.DB).Collection(ns.Collection).Find(ctx, q)
if err != nil {
return &errorMigrationIterator{err: err}
}
if cursor == nil {
return &errorMigrationIterator{}
}
return &cursorMigrationMetadataIterator{ctx: ctx, cursor: cursor, catcher: grip.NewBasicCatcher()}
}