-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (43 loc) · 1.32 KB
/
main.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
package main
import (
"context"
"fmt"
"cloud.google.com/go/datastore"
)
// ToDo: Specify your entity projection here:
type Entity struct {
}
func main() {
// ToDo: Fill in the data
projectID := ""
fromNamespace := ""
fromKind := ""
toNamespace := ""
toKind := ""
fmt.Printf(
"Starting data migration from %s/%s to %s/%s in project %s.",
fromNamespace, fromKind, toNamespace, toKind, projectID)
ctx := context.Background()
fmt.Println("Initialising client...")
client, err := datastore.NewClient(ctx, projectID)
if err != nil {
panic(fmt.Errorf("failed to create Google Cloud Datastore client: %w", err))
}
fmt.Println("Querying all entities...")
var entities []Entity
query := datastore.NewQuery(fromKind).Namespace(fromNamespace)
keys, err := client.GetAll(ctx, query, &entities)
if err != nil {
panic(fmt.Errorf("error reading from Google Cloud Datastore: %w", err))
}
fmt.Printf("\nTotal entities found: %d", len(keys))
fmt.Printf("\nWriting entities to new destination: %s/%s", toNamespace, toKind)
for i, e := range entities {
key := datastore.NameKey(toKind, keys[i].Name, nil)
key.Namespace = toNamespace
if _, err := client.Put(ctx, key, &e); err != nil {
panic(fmt.Errorf("error writing to Google Cloud Datastore: %w", err))
}
}
fmt.Println("Data migration completed successfully.")
}