-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdelete.go
140 lines (111 loc) · 2.93 KB
/
delete.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
package main
import (
"context"
"fmt"
"log"
firestore "cloud.google.com/go/firestore"
"github.com/urfave/cli"
"google.golang.org/api/iterator"
)
func deleteCommandAction(c *cli.Context) error {
argsLength := len(c.Args())
var err error
if argsLength < 1 || argsLength > 2 {
return cli.NewExitError("Wrong number of arguments", 82)
}
deleteRecursive := c.Bool("recursive")
deleteField := c.String("field")
if deleteRecursive && deleteField != "" {
return cli.NewExitError("recursive delete and field delete can't be combined!", 82)
}
client, err := createClient(credentials)
if err != nil {
return cliClientError(err)
}
var documentRef *firestore.DocumentRef
if argsLength == 2 {
collectionPath := c.Args().First()
id := c.Args().Get(1)
collectionRef := client.Collection(collectionPath)
documentRef = collectionRef.Doc(id)
} else {
documentPath := c.Args().First()
documentRef = client.Doc(documentPath)
}
defer client.Close()
if deleteRecursive {
deleteBatch := client.Batch()
err = deleteSubCollections(documentRef, client, deleteBatch)
if err != nil {
return err
}
_, err = deleteBatch.Commit(context.Background())
if err != nil {
return cli.NewExitError(fmt.Sprintf("failed to delete sub-collections batch. %v\n", err), 82)
}
}
if deleteField != "" {
_, err := documentRef.Update(
context.Background(),
[]firestore.Update{{
Path: deleteField,
Value: firestore.Delete,
},
})
if err != nil {
return cli.NewExitError(fmt.Sprintf("failed to delete field. \n%v", err), 82)
}
return nil
}
res, err := documentRef.Delete(context.Background())
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to delete data. \n%v", res), 82)
}
defer client.Close()
return nil
}
func deleteSubCollections(r *firestore.DocumentRef, c *firestore.Client, b *firestore.WriteBatch) error {
subCollectionIter := r.Collections(context.Background())
for {
subCol, err := subCollectionIter.Next()
if err == iterator.Done {
break
}
if err != nil {
return cli.NewExitError(fmt.Sprintf("failed to iterate over sub-collections (error at %v).", subCol.Path), 82)
}
err = deleteCollection(subCol, c, b)
if err != nil {
return err
}
}
return nil
}
func deleteCollection(r *firestore.CollectionRef, c *firestore.Client, b *firestore.WriteBatch) error {
iter := r.DocumentRefs(context.Background())
for {
numDeleted := 0
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return cli.NewExitError(fmt.Sprintf(
"could not iterate over sub-collection %s (error on document %s). error %s\n",
r.ID, doc.Path, err), 82)
}
err = deleteSubCollections(doc, c, b)
if err != nil {
return err
}
b.Delete(doc)
numDeleted++
}
if numDeleted == 0 {
break
}
log.Printf("added sub-collection %s to delete-batch! (number of deleted documents: %d)", r.Path, numDeleted)
}
return nil
}