Skip to content

Commit

Permalink
Support rename db for collection
Browse files Browse the repository at this point in the history
Signed-off-by: jaime <[email protected]>
  • Loading branch information
jaime0815 committed Jul 21, 2023
1 parent 76c6326 commit 191c192
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 45 deletions.
25 changes: 22 additions & 3 deletions examples/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from sklearn import preprocessing
import string

from pymilvus.orm import db

default_dim = 128
default_nb = 3000
default_float_vec_field_name = "float_vector"
Expand Down Expand Up @@ -305,17 +307,34 @@ def test_rename_collection():
FieldSchema("float_vector", DataType.FLOAT_VECTOR, is_primary=False, dim=128),
])

old_collection = "old_collection"
new_collection = "new_collection"
old_collection = "old_collection_rename_testing"
new_collection = "new_collection_rename_testing"
Collection(old_collection, schema=schema)

print("\nlist collections:")
print(utility.list_collections())
print("rename collection name start, db:default, collections:", utility.list_collections())
assert utility.has_collection(old_collection)

utility.rename_collection(old_collection, new_collection)
assert utility.has_collection(new_collection)
assert not utility.has_collection(old_collection)
print("rename collection name end, db:default, collections:", utility.list_collections())

# create db1
new_db = "new_db"
if new_db not in db.list_database():
print("\ncreate database: new_db")
db.create_database(db_name=new_db)
utility.rename_collection(new_collection, new_collection, new_db)
print("rename db name end, db:default, collections:", utility.list_collections())

db.using_database(db_name=new_db)
assert utility.has_collection(new_collection)
print("rename db name end, db:", new_db, "collections:", utility.list_collections())

db.using_database(db_name="default")
assert not utility.has_collection(new_collection)
print("db:default, collections:", utility.list_collections())


if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions pymilvus/client/grpc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,11 @@ def list_collections(self, timeout: Optional[float] = None):
raise MilvusException(status.error_code, status.reason)

@retry_on_rpc_failure()
def rename_collections(self, old_name: str, new_name: str, timeout: Optional[float] = None):
def rename_collections(self, old_name: str, new_db_name: str, new_name: str, timeout: Optional[float] = None):
check_pass_param(collection_name=new_name)
check_pass_param(collection_name=new_db_name)
check_pass_param(collection_name=old_name)
request = Prepare.rename_collections_request(old_name, new_name)
request = Prepare.rename_collections_request(old_name, new_db_name, new_name)
rf = self._stub.RenameCollection.future(request, timeout=timeout)
response = rf.result()

Expand Down
4 changes: 2 additions & 2 deletions pymilvus/client/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ def show_collections_request(cls, collection_names: Optional[List[str]] = None):
return req

@classmethod
def rename_collections_request(cls, old_name: str, new_name: str):
return milvus_types.RenameCollectionRequest(oldName=old_name, newName=new_name)
def rename_collections_request(cls, old_name: str, new_db_name: str, new_name: str):
return milvus_types.RenameCollectionRequest(oldName=old_name, newName=new_name, newDBName=new_db_name)

@classmethod
def create_partition_request(cls, collection_name: str, partition_name: str):
Expand Down
2 changes: 1 addition & 1 deletion pymilvus/grpc_gen/milvus-proto
68 changes: 34 additions & 34 deletions pymilvus/grpc_gen/milvus_pb2.py

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions pymilvus/grpc_gen/milvus_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1607,16 +1607,18 @@ class ResourceGroup(_message.Message):
def __init__(self, name: _Optional[str] = ..., capacity: _Optional[int] = ..., num_available_node: _Optional[int] = ..., num_loaded_replica: _Optional[_Mapping[str, int]] = ..., num_outgoing_node: _Optional[_Mapping[str, int]] = ..., num_incoming_node: _Optional[_Mapping[str, int]] = ...) -> None: ...

class RenameCollectionRequest(_message.Message):
__slots__ = ["base", "db_name", "oldName", "newName"]
__slots__ = ["base", "db_name", "oldName", "newName", "newDBName"]
BASE_FIELD_NUMBER: _ClassVar[int]
DB_NAME_FIELD_NUMBER: _ClassVar[int]
OLDNAME_FIELD_NUMBER: _ClassVar[int]
NEWNAME_FIELD_NUMBER: _ClassVar[int]
NEWDBNAME_FIELD_NUMBER: _ClassVar[int]
base: _common_pb2.MsgBase
db_name: str
oldName: str
newName: str
def __init__(self, base: _Optional[_Union[_common_pb2.MsgBase, _Mapping]] = ..., db_name: _Optional[str] = ..., oldName: _Optional[str] = ..., newName: _Optional[str] = ...) -> None: ...
newDBName: str
def __init__(self, base: _Optional[_Union[_common_pb2.MsgBase, _Mapping]] = ..., db_name: _Optional[str] = ..., oldName: _Optional[str] = ..., newName: _Optional[str] = ..., newDBName: _Optional[str] = ...) -> None: ...

class CreateDatabaseRequest(_message.Message):
__slots__ = ["base", "db_name"]
Expand Down
3 changes: 2 additions & 1 deletion pymilvus/orm/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ def drop_collection(collection_name: str, timeout: Optional[float] = None, using
def rename_collection(
old_collection_name: str,
new_collection_name: str,
new_db_name: str = "default",
timeout: Optional[float] = None,
using: str = "default",
):
Expand Down Expand Up @@ -522,7 +523,7 @@ def rename_collection(
>>> False
"""
return _get_connection(using).rename_collections(
old_collection_name, new_collection_name, timeout=timeout
old_collection_name, new_db_name, new_collection_name, timeout=timeout
)


Expand Down

0 comments on commit 191c192

Please sign in to comment.