Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RenameCollection API #425

Merged
merged 1 commit into from
Mar 17, 2023
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
2 changes: 2 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type Client interface {
ReleaseCollection(ctx context.Context, collName string) error
// HasCollection check whether collection exists
HasCollection(ctx context.Context, collName string) (bool, error)
// RenameCollection performs renaming for provided collection.
RenameCollection(ctx context.Context, collName, newName string) error

// CreateAlias creates an alias for collection
CreateAlias(ctx context.Context, collName string, alias string) error
Expand Down
21 changes: 21 additions & 0 deletions client/client_grpc_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,27 @@ func (c *GrpcClient) ShowCollection(ctx context.Context, collName string) (*enti
}, nil
}

// RenameCollection performs renaming for provided collection.
func (c *GrpcClient) RenameCollection(ctx context.Context, collName, newName string) error {
if c.Service == nil {
return ErrClientNotReady
}

if err := c.checkCollectionExists(ctx, collName); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave this to RenameCollection

return err
}

req := &server.RenameCollectionRequest{
OldName: collName,
NewName: newName,
}
resp, err := c.Service.RenameCollection(ctx, req)
if err != nil {
return err
}
return handleRespStatus(resp)
}

// LoadCollection load collection into memory
func (c *GrpcClient) LoadCollection(ctx context.Context, collName string, async bool, opts ...LoadCollectionOption) error {
if c.Service == nil {
Expand Down
52 changes: 52 additions & 0 deletions client/client_grpc_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,58 @@ func (s *CollectionSuite) TestCreateCollection() {
})
}

func (s *CollectionSuite) TestRenameCollection() {
c := s.client
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.Run("normal_run", func() {
defer s.resetMock()

newCollName := fmt.Sprintf("new_%s", randStr(6))

s.mock.EXPECT().HasCollection(mock.Anything, &server.HasCollectionRequest{CollectionName: testCollectionName}).Return(&server.BoolResponse{Status: &common.Status{}, Value: true}, nil)
s.mock.EXPECT().RenameCollection(mock.Anything, &server.RenameCollectionRequest{OldName: testCollectionName, NewName: newCollName}).Return(&common.Status{}, nil)

err := c.RenameCollection(ctx, testCollectionName, newCollName)
s.NoError(err)
})

s.Run("coll_not_exist", func() {
defer s.resetMock()

newCollName := fmt.Sprintf("new_%s", randStr(6))

s.mock.EXPECT().HasCollection(mock.Anything, &server.HasCollectionRequest{CollectionName: testCollectionName}).Return(&server.BoolResponse{Status: &common.Status{}, Value: false}, nil)

err := c.RenameCollection(ctx, testCollectionName, newCollName)
s.Error(err)
})

s.Run("rename_failed", func() {
defer s.resetMock()

newCollName := fmt.Sprintf("new_%s", randStr(6))

s.mock.EXPECT().HasCollection(mock.Anything, &server.HasCollectionRequest{CollectionName: testCollectionName}).Return(&server.BoolResponse{Status: &common.Status{}, Value: true}, nil)
s.mock.EXPECT().RenameCollection(mock.Anything, &server.RenameCollectionRequest{OldName: testCollectionName, NewName: newCollName}).Return(&common.Status{ErrorCode: common.ErrorCode_UnexpectedError, Reason: "mocked failure"}, nil)

err := c.RenameCollection(ctx, testCollectionName, newCollName)
s.Error(err)
})

s.Run("rename_error", func() {
defer s.resetMock()

newCollName := fmt.Sprintf("new_%s", randStr(6))

s.mock.EXPECT().HasCollection(mock.Anything, &server.HasCollectionRequest{CollectionName: testCollectionName}).Return(&server.BoolResponse{Status: &common.Status{}, Value: true}, nil)
s.mock.EXPECT().RenameCollection(mock.Anything, &server.RenameCollectionRequest{OldName: testCollectionName, NewName: newCollName}).Return(nil, errors.New("mocked error"))

err := c.RenameCollection(ctx, testCollectionName, newCollName)
s.Error(err)
})
}

func TestCollectionSuite(t *testing.T) {
suite.Run(t, new(CollectionSuite))
}
Expand Down