-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_crud.go
124 lines (100 loc) · 2.81 KB
/
migration_crud.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
package firestoreadapter
import (
"errors"
"fmt"
"cloud.google.com/go/firestore"
"github.com/mhogar/amber/common"
"github.com/mhogar/amber/models"
"google.golang.org/api/iterator"
)
func (crud *FirestoreCRUD) Setup() error {
return nil
}
func (crud *FirestoreCRUD) CreateMigration(timestamp string) error {
//create and validate migration model
migration := models.CreateMigration(timestamp)
verr := migration.Validate()
if verr != models.ValidateMigrationValid {
return errors.New(fmt.Sprint("error validating migration model:", verr))
}
//create the migration
err := crud.DocWriter.Create(crud.getMigrationDocRef(timestamp), migration)
if err != nil {
return common.ChainError("error creating migration", err)
}
return nil
}
func (crud *FirestoreCRUD) GetMigrationByTimestamp(timestamp string) (*models.Migration, error) {
doc, err := crud.getMigration(timestamp)
if err != nil {
return nil, err
}
if doc == nil {
return nil, nil
}
return crud.readMigrationData(doc)
}
func (crud *FirestoreCRUD) GetLatestTimestamp() (string, bool, error) {
ctx, cancel := crud.ContextFactory.CreateStandardTimeoutContext()
itr := crud.Client.Collection("migrations").
OrderBy("timestamp", firestore.Desc).
Limit(1).
Documents(ctx)
defer cancel()
defer itr.Stop()
//get the result
doc, err := itr.Next()
if err == iterator.Done {
return "", false, nil
}
if err != nil {
return "", false, common.ChainError("error getting next doc", err)
}
//read the data
migration, err := crud.readMigrationData(doc)
if err != nil {
return "", false, err
}
return migration.Timestamp, true, nil
}
func (crud *FirestoreCRUD) DeleteMigrationByTimestamp(timestamp string) error {
//check migration already exists
doc, err := crud.getMigration(timestamp)
if err != nil {
return err
}
if doc == nil {
return nil
}
//delete migration
err = crud.DocWriter.Delete(doc.Ref)
if err != nil {
return common.ChainError("error deleting migration", err)
}
return nil
}
func (crud *FirestoreCRUD) getMigrationDocRef(timestamp string) *firestore.DocumentRef {
return crud.Client.Collection("migrations").Doc(timestamp)
}
func (crud *FirestoreCRUD) getMigration(timestamp string) (*firestore.DocumentSnapshot, error) {
ctx, cancel := crud.ContextFactory.CreateStandardTimeoutContext()
doc, err := crud.getMigrationDocRef(timestamp).Get(ctx)
cancel()
//check migration was found
if !doc.Exists() {
return nil, nil
}
//handle other errors
if err != nil {
return nil, common.ChainError("error getting migration", err)
}
return doc, nil
}
func (*FirestoreCRUD) readMigrationData(doc *firestore.DocumentSnapshot) (*models.Migration, error) {
migration := &models.Migration{}
err := doc.DataTo(&migration)
if err != nil {
return nil, common.ChainError("error reading migration data", err)
}
return migration, nil
}