Skip to content

Commit f425578

Browse files
authored
[Refactor] CreateGraphV2 (#357)
1 parent 969f7da commit f425578

File tree

7 files changed

+372
-12
lines changed

7 files changed

+372
-12
lines changed

database_graphs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ type DatabaseGraphs interface {
4242

4343
// CreateGraph creates a new graph with given name and options, and opens a connection to it.
4444
// If a graph with given name already exists within the database, a DuplicateError is returned.
45+
// @deprecated since ArangoDB 3.9 - please use CreateGraphV2 instead
4546
CreateGraph(ctx context.Context, name string, options *CreateGraphOptions) (Graph, error)
47+
48+
// CreateGraphV2 creates a new graph with given name and options, and opens a connection to it.
49+
// If a graph with given name already exists within the database, a DuplicateError is returned.
50+
CreateGraphV2(ctx context.Context, name string, options *CreateGraphOptions) (Graph, error)
4651
}
4752

4853
// CreateGraphOptions contains options that customize the creating of a graph.

database_graphs_impl.go

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,60 @@ type createGraphAdditionalOptions struct {
172172

173173
// CreateGraph creates a new graph with given name and options, and opens a connection to it.
174174
// If a graph with given name already exists within the database, a DuplicateError is returned.
175-
// todo test
176175
func (d *database) CreateGraph(ctx context.Context, name string, options *CreateGraphOptions) (Graph, error) {
176+
input := createGraphOptions{
177+
Name: name,
178+
}
179+
if options != nil {
180+
input.OrphanVertexCollections = options.OrphanVertexCollections
181+
input.EdgeDefinitions = options.EdgeDefinitions
182+
input.IsSmart = options.IsSmart
183+
if options.ReplicationFactor == SatelliteGraph {
184+
input.Options = &createGraphAdditionalOptions{
185+
SmartGraphAttribute: options.SmartGraphAttribute,
186+
ReplicationFactor: graphReplicationFactor(options.ReplicationFactor),
187+
IsDisjoint: options.IsDisjoint,
188+
Satellites: options.Satellites,
189+
}
190+
} else if options.SmartGraphAttribute != "" || options.NumberOfShards != 0 {
191+
input.Options = &createGraphAdditionalOptions{
192+
SmartGraphAttribute: options.SmartGraphAttribute,
193+
NumberOfShards: options.NumberOfShards,
194+
ReplicationFactor: graphReplicationFactor(options.ReplicationFactor),
195+
WriteConcern: options.WriteConcern,
196+
IsDisjoint: options.IsDisjoint,
197+
Satellites: options.Satellites,
198+
}
199+
}
200+
}
201+
req, err := d.conn.NewRequest("POST", path.Join(d.relPath(), "_api/gharial"))
202+
if err != nil {
203+
return nil, WithStack(err)
204+
}
205+
if _, err := req.SetBody(input); err != nil {
206+
return nil, WithStack(err)
207+
}
208+
resp, err := d.conn.Do(ctx, req)
209+
if err != nil {
210+
return nil, WithStack(err)
211+
}
212+
if err := resp.CheckStatus(201, 202); err != nil {
213+
return nil, WithStack(err)
214+
}
215+
var data getGraphResponse
216+
if err := resp.ParseBody("", &data); err != nil {
217+
return nil, WithStack(err)
218+
}
219+
g, err := newGraph(data.Graph, d)
220+
if err != nil {
221+
return nil, WithStack(err)
222+
}
223+
return g, nil
224+
}
225+
226+
// CreateGraphV2 creates a new graph with given name and options, and opens a connection to it.
227+
// If a graph with given name already exists within the database, a DuplicateError is returned.
228+
func (d *database) CreateGraphV2(ctx context.Context, name string, options *CreateGraphOptions) (Graph, error) {
177229
input := createGraphOptions{
178230
Name: name,
179231
}

example_graph_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func Example_createGraph() {
7777
options.EdgeDefinitions = []driver.EdgeDefinition{edgeDefinition}
7878

7979
// now it's possible to create a graph
80-
graph, err := db.CreateGraph(nil, "myGraph", &options)
80+
graph, err := db.CreateGraphV2(nil, "myGraph", &options)
8181
if err != nil {
8282
log.Fatalf("Failed to create graph: %v", err)
8383
}

test/edge_collection_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestCreateEdgeCollection(t *testing.T) {
5050
c := createClientFromEnv(t, true)
5151
db := ensureDatabase(nil, c, "edge_collection_test", nil, t)
5252
name := "test_create_edge_collection"
53-
g, err := db.CreateGraph(nil, name, nil)
53+
g, err := db.CreateGraphV2(nil, name, nil)
5454
if err != nil {
5555
t.Fatalf("Failed to create graph '%s': %s", name, describe(err))
5656
}
@@ -123,7 +123,7 @@ func TestCreateSatelliteEdgeCollection(t *testing.T) {
123123
IsSmart: true,
124124
SmartGraphAttribute: "test",
125125
}
126-
g, err := db.CreateGraph(ctx, name, &options)
126+
g, err := db.CreateGraphV2(ctx, name, &options)
127127
if err != nil {
128128
t.Fatalf("Failed to create graph '%s': %s", name, describe(err))
129129
}
@@ -188,7 +188,7 @@ func TestRemoveEdgeCollection(t *testing.T) {
188188
c := createClientFromEnv(t, true)
189189
db := ensureDatabase(nil, c, "edge_collection_test", nil, t)
190190
name := "test_remove_edge_collection"
191-
g, err := db.CreateGraph(nil, name, nil)
191+
g, err := db.CreateGraphV2(nil, name, nil)
192192
if err != nil {
193193
t.Fatalf("Failed to create graph '%s': %s", name, describe(err))
194194
}
@@ -230,7 +230,7 @@ func TestSetVertexConstraints(t *testing.T) {
230230
c := createClientFromEnv(t, true)
231231
db := ensureDatabase(nil, c, "edge_collection_test", nil, t)
232232
name := "set_vertex_constraints"
233-
g, err := db.CreateGraph(nil, name, nil)
233+
g, err := db.CreateGraphV2(nil, name, nil)
234234
if err != nil {
235235
t.Fatalf("Failed to create graph '%s': %s", name, describe(err))
236236
}

0 commit comments

Comments
 (0)