/
import_dgraph_sample_data.go
76 lines (60 loc) · 1.45 KB
/
import_dgraph_sample_data.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
package main
import (
"context"
"fmt"
"strconv"
"time"
"flag"
retry "github.com/avast/retry-go"
"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
"google.golang.org/grpc"
"os"
"github.com/infinimesh/infinimesh/pkg/node/dgraph"
)
var (
dgraphURL = "localhost:9080"
drop bool
)
func init() {
flag.BoolVar(&drop, "drop", false, "Drop all data in dgraph before import")
envURL := os.Getenv("DGRAPH_URL")
if envURL != "" {
dgraphURL = envURL
}
}
func main() {
flag.Parse()
counter := 0
err := retry.Do(func() error {
conn, _ := grpc.Dial(dgraphURL, grpc.WithInsecure())
defer conn.Close()
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
repo := dgraph.NewDGraphRepo(dg)
counter++
fmt.Println("----------- Attempt " + strconv.Itoa(counter) + " -----------")
if drop {
err := dg.Alter(context.Background(), &api.Operation{DropAll: true})
if err != nil {
return err
}
fmt.Println("Dgraph Data Drop Successful")
}
err := dgraph.ImportSchema(dg, true)
if err != nil {
fmt.Println("Import failed with error: " + err.Error())
return err
}
fmt.Println("Dgraph Schema Import Successful")
_, _, err = dgraph.ImportStandardSet(repo)
if err != nil {
fmt.Println("Import Standard set failed with error: " + err.Error())
return err
}
fmt.Println("Dgraph Test Data Import Successful")
return nil
}, retry.Delay(time.Second*4), retry.Attempts(5))
if err != nil {
panic(err)
}
}