Skip to content

Commit f48a893

Browse files
shiva-istariShiva
andauthored
fix(import): add sanity checks for connection string and bulk output directory in … (#9541)
…import client Co-authored-by: Shiva <shiva@Shivajis-MacBook-Pro.local>
1 parent d28f8fa commit f48a893

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

dgraph/cmd/dgraphimport/import_client.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import (
2525

2626
// newClient creates a new import client with the specified endpoint and gRPC options.
2727
func newClient(connectionString string) (api.DgraphClient, error) {
28+
if connectionString == "" {
29+
return nil, fmt.Errorf("connection string cannot be empty")
30+
}
31+
2832
dg, err := dgo.Open(connectionString)
2933
if err != nil {
3034
return nil, fmt.Errorf("failed to connect to endpoint [%s]: %w", connectionString, err)
@@ -35,6 +39,10 @@ func newClient(connectionString string) (api.DgraphClient, error) {
3539
}
3640

3741
func Import(ctx context.Context, connectionString string, bulkOutDir string) error {
42+
if bulkOutDir == "" {
43+
return fmt.Errorf("bulk output directory cannot be empty")
44+
}
45+
3846
dg, err := newClient(connectionString)
3947
if err != nil {
4048
return err

dgraph/cmd/dgraphimport/import_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ const expectedSchema = `{
6262
]
6363
}`
6464

65+
func TestEmptyConnStr(t *testing.T) {
66+
err := Import(context.Background(), "", "/tmp")
67+
require.Error(t, err)
68+
require.ErrorContains(t, err, "connection string cannot be empty")
69+
}
70+
71+
func TestEmptyBulkOutDir(t *testing.T) {
72+
err := Import(context.Background(), "dgraph://localhost:9080", "")
73+
require.Error(t, err)
74+
require.ErrorContains(t, err, "bulk output directory cannot be empty")
75+
}
76+
6577
func TestDrainModeAfterStartSnapshotStream(t *testing.T) {
6678
t.Skip("Skipping... sometimes the query for schema succeeds even when the server is in draining mode")
6779

dgraph/cmd/dgraphimport/run.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ func init() {
3636
flag.StringP("files", "f", "", "Location of *.rdf(.gz) or *.json(.gz) file(s) to load.")
3737
flag.StringP("snapshot-dir", "p", "", "Location of p directory")
3838
flag.StringP("schema", "s", "", "Location of DQL schema file.")
39-
flag.StringP("graphql_schema", "g", "", "Location of the GraphQL schema file.")
40-
flag.StringP("graphql-schema", "", "", "Location of the GraphQL schema file.")
39+
flag.StringP("graphql-schema", "g", "", "Location of the GraphQL schema file.")
4140
flag.String("format", "", "Specify file format (rdf or json)")
4241
flag.Bool("drop-all", false, "Drops all the existing data in the cluster before importing data into Dgraph.")
4342
flag.Bool("drop-all-confirm", false, "Confirm drop-all operation.")
44-
flag.StringP("conn-str", "c", "", "Dgraph connection string.")
43+
flag.StringP("conn-str", "c", "dgraph://localhost:9080", "Dgraph connection string.")
4544
}
4645

4746
func run() {

0 commit comments

Comments
 (0)