Skip to content

Commit 2ee041d

Browse files
authored
GT-201 Add Rename View support (#436)
1 parent 5a25ec9 commit 2ee041d

File tree

6 files changed

+64
-21
lines changed

6 files changed

+64
-21
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- Add support for Pregel API
1717
- Add tests to check support for Enterprise Graphs
1818
- Search View v2 (`search-alias`)
19+
- Add Rename View support
1920

2021
## [1.3.3](https://github.com/arangodb/go-driver/tree/v1.3.3) (2022-07-27)
2122
- Fix `lastValue` field type

test/view_test.go

+30-19
Original file line numberDiff line numberDiff line change
@@ -394,33 +394,44 @@ func TestGetArangoSearchViews(t *testing.T) {
394394
}
395395
}
396396

397-
// TestRemoveArangoSearchView creates an arangosearch view and then removes it.
398-
func TestRemoveArangoSearchView(t *testing.T) {
397+
// TestRenameAndRemoveArangoSearchView creates an arangosearch view and then removes it.
398+
func TestRenameAndRemoveArangoSearchView(t *testing.T) {
399399
ctx := context.Background()
400400
c := createClientFromEnv(t, true)
401401
skipBelowVersion(c, "3.4", t)
402402
db := ensureDatabase(ctx, c, "view_test", nil, t)
403-
name := "test_remove_asview"
403+
name := "test_rename_view"
404404
v, err := db.CreateArangoSearchView(ctx, name, nil)
405-
if err != nil {
406-
t.Fatalf("Failed to create collection '%s': %s", name, describe(err))
407-
}
405+
require.NoError(t, err)
406+
408407
// View must exist now
409-
if found, err := db.ViewExists(ctx, name); err != nil {
410-
t.Errorf("ViewExists('%s') failed: %s", name, describe(err))
411-
} else if !found {
412-
t.Errorf("ViewExists('%s') return false, expected true", name)
413-
}
408+
found, err := db.ViewExists(ctx, name)
409+
require.NoError(t, err)
410+
require.True(t, found)
411+
412+
// Rename view
413+
newName := "test_rename_view_new"
414+
err = v.Rename(ctx, newName)
415+
require.NoError(t, err)
416+
require.Equal(t, newName, v.Name())
417+
418+
// Renamed View must exist
419+
found, err = db.ViewExists(ctx, newName)
420+
require.NoError(t, err)
421+
require.True(t, found)
422+
414423
// Now remove it
415-
if err := v.Remove(ctx); err != nil {
416-
t.Fatalf("Failed to remove view '%s': %s", name, describe(err))
417-
}
424+
err = v.Remove(ctx)
425+
require.NoError(t, err)
426+
418427
// View must not exist now
419-
if found, err := db.ViewExists(ctx, name); err != nil {
420-
t.Errorf("ViewExists('%s') failed: %s", name, describe(err))
421-
} else if found {
422-
t.Errorf("ViewExists('%s') return true, expected false", name)
423-
}
428+
found, err = db.ViewExists(ctx, name)
429+
require.NoError(t, err)
430+
require.False(t, found)
431+
432+
found, err = db.ViewExists(ctx, newName)
433+
require.NoError(t, err)
434+
require.False(t, found)
424435
}
425436

426437
// TestUseArangoSearchView tries to create a view and actually use it in

view.go

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ type View interface {
4646
// Database returns the database containing the view.
4747
Database() Database
4848

49+
// Rename renames the view.
50+
Rename(ctx context.Context, newName string) error
51+
4952
// Remove removes the entire view.
5053
// If the view does not exist, a NotFoundError is returned.
5154
Remove(ctx context.Context) error

view_arangosearch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
// ArangoSearchView provides access to the information of a view.
3030
// Views are only available in ArangoDB 3.4 and higher.
3131
type ArangoSearchView interface {
32-
// Include generic View functions
32+
// View Includes generic View functions
3333
View
3434

3535
// Properties fetches extended information about the view.

view_arangosearch_alias.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
// ArangoSearchViewAlias provides access to the information of a view alias
3030
// Views aliases are only available in ArangoDB 3.10 and higher.
3131
type ArangoSearchViewAlias interface {
32-
// View Include generic View functions
32+
// View Includes generic View functions
3333
View
3434

3535
// Properties fetches extended information about the view.

view_impl.go

+28
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,34 @@ func (v *view) Database() Database {
9292
return v.db
9393
}
9494

95+
func (v *view) Rename(ctx context.Context, newName string) error {
96+
if newName == "" {
97+
return WithStack(InvalidArgumentError{Message: "newName is empty"})
98+
}
99+
req, err := v.conn.NewRequest("PUT", path.Join(v.relPath(), "rename"))
100+
if err != nil {
101+
return WithStack(err)
102+
}
103+
input := struct {
104+
Name string `json:"name"`
105+
}{
106+
Name: newName,
107+
}
108+
if _, err := req.SetBody(input); err != nil {
109+
return WithStack(err)
110+
}
111+
applyContextSettings(ctx, req)
112+
resp, err := v.conn.Do(ctx, req)
113+
if err != nil {
114+
return WithStack(err)
115+
}
116+
if err := resp.CheckStatus(200); err != nil {
117+
return WithStack(err)
118+
}
119+
v.name = newName
120+
return nil
121+
}
122+
95123
// Remove removes the entire view.
96124
// If the view does not exist, a NotFoundError is returned.
97125
func (v *view) Remove(ctx context.Context) error {

0 commit comments

Comments
 (0)