-
Notifications
You must be signed in to change notification settings - Fork 28
/
deleteall.go
71 lines (58 loc) · 1.66 KB
/
deleteall.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
package main
import (
firestore "cloud.google.com/go/firestore"
"context"
"fmt"
"github.com/urfave/cli"
)
func deleteAllCommandAction(c *cli.Context) error {
argsLength := len(c.Args())
if argsLength < 2 {
return cli.NewExitError("Wrong number of arguments", 82)
}
var collectionPath string
var ids []string
collectionPath = c.Args().First()
ids = c.Args()[1:]
client, err := createClient(credentials)
if err != nil {
return cliClientError(err)
}
deletedCount := 0
for _, part := range partition(ids, maxWritesCount) {
d, err := deleteAllDocuments(client, collectionPath, part)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to remove all documents. \n%v", err), 82)
}
deletedCount += len(d)
fmt.Printf("Deleted %v out of %v\n", deletedCount, len(ids))
}
defer client.Close()
return nil
}
func deleteAllDocuments(client *firestore.Client, collectionPath string, ids []string) ([]*firestore.WriteResult, error) {
batch := client.Batch()
collectionRef := client.Collection(collectionPath)
for _, id := range ids {
batch.Delete(collectionRef.Doc(id))
}
commit, err := batch.Commit(context.Background())
return commit, err
}
func partition(bigList []string, partitionSize int) [][]string {
collectionLen := len(bigList)
numFullPartitions := collectionLen / partitionSize
capacity := numFullPartitions
if collectionLen%partitionSize != 0 {
capacity++
}
result := make([][]string, capacity)
var i int
for ; i < numFullPartitions; i++ {
result[i] = bigList[i*partitionSize : (i+1)*partitionSize]
}
if collectionLen%partitionSize != 0 { // left over
result[i] = bigList[i*partitionSize : collectionLen]
}
return result
}