Skip to content

Commit df8dcb5

Browse files
authored
Remove graph with all collections (#582)
1 parent 45f10b4 commit df8dcb5

File tree

5 files changed

+76
-8
lines changed

5 files changed

+76
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Switch to Go 1.20.11
55
- Switch to Go 1.21.5
66
- Disable AF mode in tests (not supported since 3.12)
7+
- Remove graph with all collections
78

89
## [1.6.1](https://github.com/arangodb/go-driver/tree/v1.6.1) (2023-10-31)
910
- Add support for getting license

graph.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2017-2024 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -17,8 +17,6 @@
1717
//
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
20-
// Author Ewout Prangsma
21-
//
2220

2321
package driver
2422

@@ -33,6 +31,9 @@ type Graph interface {
3331
// If the graph does not exist, a NotFoundError is returned.
3432
Remove(ctx context.Context) error
3533

34+
// RemoveWithOpts removes the entire graph with options.
35+
RemoveWithOpts(ctx context.Context, opts *RemoveGraphOptions) error
36+
3637
// IsSmart returns true of smart is smart. In case of Community Edition it is always false
3738
IsSmart() bool
3839

@@ -69,7 +70,7 @@ type Graph interface {
6970
// NumberOfShards returns the number of shards for the graph.
7071
NumberOfShards() int
7172

72-
// OrphanCollections returns the orphan collcetions of the graph.
73+
// OrphanCollections returns the orphan collections of the graph.
7374
OrphanCollections() []string
7475

7576
// ReplicationFactor returns the current replication factor.
@@ -78,3 +79,7 @@ type Graph interface {
7879
// WriteConcern returns the write concern setting of the graph.
7980
WriteConcern() int
8081
}
82+
83+
type RemoveGraphOptions struct {
84+
DropCollections bool `json:"dropCollections,omitempty"`
85+
}

graph_impl.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2023 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2023-2024 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -136,3 +136,26 @@ func (g *graph) Remove(ctx context.Context) error {
136136
}
137137
return nil
138138
}
139+
140+
func (g *graph) RemoveWithOpts(ctx context.Context, opts *RemoveGraphOptions) error {
141+
req, err := g.conn.NewRequest("DELETE", g.relPath())
142+
if err != nil {
143+
return WithStack(err)
144+
}
145+
146+
if opts != nil {
147+
if opts.DropCollections {
148+
req.SetQuery("dropCollections", "true")
149+
}
150+
}
151+
152+
applyContextSettings(ctx, req)
153+
resp, err := g.conn.Do(ctx, req)
154+
if err != nil {
155+
return WithStack(err)
156+
}
157+
if err := resp.CheckStatus(201, 202); err != nil {
158+
return WithStack(err)
159+
}
160+
return nil
161+
}

test/graph_test.go

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2017-2023 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2017-2024 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@ import (
2424
"context"
2525
"testing"
2626

27+
"github.com/stretchr/testify/require"
28+
2729
driver "github.com/arangodb/go-driver"
2830
)
2931

@@ -189,3 +191,37 @@ func TestRemoveGraph(t *testing.T) {
189191
t.Errorf("GraphExists('%s') return true, expected false", name)
190192
}
191193
}
194+
195+
// TestRemoveGraphWithOpts creates a graph with collection and then removes it.
196+
func TestRemoveGraphWithOpts(t *testing.T) {
197+
ctx := context.Background()
198+
c := createClient(t, nil)
199+
db := ensureDatabase(ctx, c, "graph_test_remove", nil, t)
200+
name := "test_remove_graph_opts"
201+
colName := "remove_graph_col"
202+
203+
g, err := db.CreateGraphV2(ctx, name, nil)
204+
require.NoError(t, err)
205+
206+
found, err := db.GraphExists(ctx, name)
207+
require.NoError(t, err)
208+
require.True(t, found)
209+
210+
// Now create a vertex collection
211+
vc, err := g.CreateVertexCollection(nil, colName)
212+
require.NoError(t, err)
213+
require.Equal(t, colName, vc.Name())
214+
215+
// Now remove the graph with collections
216+
err = g.RemoveWithOpts(ctx, &driver.RemoveGraphOptions{DropCollections: true})
217+
require.NoError(t, err)
218+
219+
// Collection must not exist in a database
220+
colExist, err := db.CollectionExists(ctx, name)
221+
require.NoError(t, err)
222+
require.False(t, colExist)
223+
224+
found, err = db.GraphExists(ctx, name)
225+
require.NoError(t, err)
226+
require.False(t, found)
227+
}

test/vertex_collection_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// DISCLAIMER
33
//
4-
// Copyright 2017-2023 ArangoDB GmbH, Cologne, Germany
4+
// Copyright 2017-2024 ArangoDB GmbH, Cologne, Germany
55
//
66
// Licensed under the Apache License, Version 2.0 (the "License");
77
// you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@ import (
2424
"context"
2525
"testing"
2626

27+
"github.com/stretchr/testify/require"
28+
2729
driver "github.com/arangodb/go-driver"
2830
)
2931

@@ -144,7 +146,8 @@ func TestCreateSatelliteVertexCollection(t *testing.T) {
144146
}
145147

146148
// revert
147-
g.Remove(ctx)
149+
err = g.Remove(ctx)
150+
require.NoError(t, err)
148151
}
149152

150153
// TestRemoveVertexCollection creates a graph and then adds an vertex collection in it and then removes the vertex collection.

0 commit comments

Comments
 (0)