Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Add support for Pregel API
- Add tests to check support for Enterprise Graphs
- Search View v2 (`search-alias`)
- Add Rename View support

## [1.3.3](https://github.com/arangodb/go-driver/tree/v1.3.3) (2022-07-27)
- Fix `lastValue` field type
Expand Down
49 changes: 30 additions & 19 deletions test/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,33 +394,44 @@ func TestGetArangoSearchViews(t *testing.T) {
}
}

// TestRemoveArangoSearchView creates an arangosearch view and then removes it.
func TestRemoveArangoSearchView(t *testing.T) {
// TestRenameAndRemoveArangoSearchView creates an arangosearch view and then removes it.
func TestRenameAndRemoveArangoSearchView(t *testing.T) {
ctx := context.Background()
c := createClientFromEnv(t, true)
skipBelowVersion(c, "3.4", t)
db := ensureDatabase(ctx, c, "view_test", nil, t)
name := "test_remove_asview"
name := "test_rename_view"
v, err := db.CreateArangoSearchView(ctx, name, nil)
if err != nil {
t.Fatalf("Failed to create collection '%s': %s", name, describe(err))
}
require.NoError(t, err)

// View must exist now
if found, err := db.ViewExists(ctx, name); err != nil {
t.Errorf("ViewExists('%s') failed: %s", name, describe(err))
} else if !found {
t.Errorf("ViewExists('%s') return false, expected true", name)
}
found, err := db.ViewExists(ctx, name)
require.NoError(t, err)
require.True(t, found)

// Rename view
newName := "test_rename_view_new"
err = v.Rename(ctx, newName)
require.NoError(t, err)
require.Equal(t, newName, v.Name())

// Renamed View must exist
found, err = db.ViewExists(ctx, newName)
require.NoError(t, err)
require.True(t, found)

// Now remove it
if err := v.Remove(ctx); err != nil {
t.Fatalf("Failed to remove view '%s': %s", name, describe(err))
}
err = v.Remove(ctx)
require.NoError(t, err)

// View must not exist now
if found, err := db.ViewExists(ctx, name); err != nil {
t.Errorf("ViewExists('%s') failed: %s", name, describe(err))
} else if found {
t.Errorf("ViewExists('%s') return true, expected false", name)
}
found, err = db.ViewExists(ctx, name)
require.NoError(t, err)
require.False(t, found)

found, err = db.ViewExists(ctx, newName)
require.NoError(t, err)
require.False(t, found)
}

// TestUseArangoSearchView tries to create a view and actually use it in
Expand Down
3 changes: 3 additions & 0 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type View interface {
// Database returns the database containing the view.
Database() Database

// Rename renames the view.
Rename(ctx context.Context, newName string) error

// Remove removes the entire view.
// If the view does not exist, a NotFoundError is returned.
Remove(ctx context.Context) error
Expand Down
2 changes: 1 addition & 1 deletion view_arangosearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
// ArangoSearchView provides access to the information of a view.
// Views are only available in ArangoDB 3.4 and higher.
type ArangoSearchView interface {
// Include generic View functions
// View Includes generic View functions
View

// Properties fetches extended information about the view.
Expand Down
2 changes: 1 addition & 1 deletion view_arangosearch_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
// ArangoSearchViewAlias provides access to the information of a view alias
// Views aliases are only available in ArangoDB 3.10 and higher.
type ArangoSearchViewAlias interface {
// View Include generic View functions
// View Includes generic View functions
View

// Properties fetches extended information about the view.
Expand Down
28 changes: 28 additions & 0 deletions view_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ func (v *view) Database() Database {
return v.db
}

func (v *view) Rename(ctx context.Context, newName string) error {
if newName == "" {
return WithStack(InvalidArgumentError{Message: "newName is empty"})
}
req, err := v.conn.NewRequest("PUT", path.Join(v.relPath(), "rename"))
if err != nil {
return WithStack(err)
}
input := struct {
Name string `json:"name"`
}{
Name: newName,
}
if _, err := req.SetBody(input); err != nil {
return WithStack(err)
}
applyContextSettings(ctx, req)
resp, err := v.conn.Do(ctx, req)
if err != nil {
return WithStack(err)
}
if err := resp.CheckStatus(200); err != nil {
return WithStack(err)
}
v.name = newName
return nil
}

// Remove removes the entire view.
// If the view does not exist, a NotFoundError is returned.
func (v *view) Remove(ctx context.Context) error {
Expand Down