From 81b71aec023ade34129498d383ed895d1c9d8373 Mon Sep 17 00:00:00 2001 From: ryjiang Date: Tue, 6 Aug 2024 18:46:26 +0800 Subject: [PATCH] support rename collection to another database Signed-off-by: ryjiang --- milvus/grpc/Collection.ts | 22 ++++++++++++++++++---- milvus/types/Collection.ts | 1 + test/grpc/Database.spec.ts | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/milvus/grpc/Collection.ts b/milvus/grpc/Collection.ts index 39cbd5b..bf7e1b5 100644 --- a/milvus/grpc/Collection.ts +++ b/milvus/grpc/Collection.ts @@ -522,6 +522,8 @@ export class Collection extends Database { * @param {RenameCollectionReq} data - The request parameters. * @param {string} data.collection_name - The current name of the collection. * @param {string} data.new_collection_name - The new name for the collection. + * @param {string} data.db_name - Optional, the name of the database where the collection is located. + * @param {string} data.new_db_name - Optional, the name of the database where the new collection will be located. * @param {number} [data.timeout] - An optional duration of time in milliseconds to allow for the RPC. If it is set to undefined, the client keeps waiting until the server responds or error occurs. Default is undefined. * * @returns {Promise} The response status of the operation. @@ -538,13 +540,25 @@ export class Collection extends Database { * ``` */ async renameCollection(data: RenameCollectionReq): Promise { + const req: any = { + oldName: data.collection_name, + newName: data.new_collection_name, + }; + + // if db_name is set, add it to the request + if (data.db_name) { + req.db_name = data.db_name; + } + + // if new_db_name is set, add it to the request + if (data.new_db_name) { + req.newDBName = data.new_db_name; + } + const promise = await promisify( this.channelPool, 'RenameCollection', - { - oldName: data.collection_name, - newName: data.new_collection_name, - }, + req, data.timeout || this.timeout ); return promise; diff --git a/milvus/types/Collection.ts b/milvus/types/Collection.ts index c5ac4be..a0d6df2 100644 --- a/milvus/types/Collection.ts +++ b/milvus/types/Collection.ts @@ -161,6 +161,7 @@ export interface GetReplicaReq extends GrpcTimeOut { export interface RenameCollectionReq extends collectionNameReq { new_collection_name: string; + new_db_name?: string; } export interface BoolResponse extends resStatusResponse { diff --git a/test/grpc/Database.spec.ts b/test/grpc/Database.spec.ts index e165ba5..0a8e0f4 100644 --- a/test/grpc/Database.spec.ts +++ b/test/grpc/Database.spec.ts @@ -1,7 +1,8 @@ import { MilvusClient, ErrorCode, DEFAULT_DB } from '../../milvus'; import { IP, genCollectionParams, GENERATE_NAME } from '../tools'; -let milvusClient = new MilvusClient({ address: IP, logLevel: 'info' }); +let milvusClient = new MilvusClient({ address: IP, logLevel: 'debug' }); +const DEFAULT = 'default'; const DB_NAME = GENERATE_NAME('database'); const DB_NAME2 = GENERATE_NAME('database'); const COLLECTION_NAME = GENERATE_NAME(); @@ -33,7 +34,7 @@ describe(`Database API`, () => { const useDB = await milvusClient.useDatabase({ db_name: DB_NAME }); expect(useDB!.error_code).toEqual(ErrorCode.SUCCESS); - // create collection on another db + // create collection in another db const create = await milvusClient.createCollection( genCollectionParams({ collectionName: COLLECTION_NAME, dim: [4] }) ); @@ -127,6 +128,37 @@ describe(`Database API`, () => { expect(dropCollections.error_code).toEqual(ErrorCode.SUCCESS); }); + it(`move one collection to aonther database should ok`, async () => { + // create collection in another db + const createCollection = await milvusClient.createCollection({ + ...genCollectionParams({ collectionName: COLLECTION_NAME2, dim: [4] }), + db_name: DB_NAME2, + }); + expect(createCollection.error_code).toEqual(ErrorCode.SUCCESS); + + // move colleciton to DEFAULT + const move = await milvusClient.renameCollection({ + collection_name: COLLECTION_NAME2, + new_collection_name: COLLECTION_NAME2, + db_name: DB_NAME2, + new_db_name: DEFAULT, + }); + + const has = await milvusClient.hasCollection({ + collection_name: COLLECTION_NAME2, + db_name: DEFAULT, + }); + + expect(has.value).toEqual(true); + + // drop collection + const dropCollections = await milvusClient.dropCollection({ + collection_name: COLLECTION_NAME2, + db_name: DEFAULT, + }); + expect(dropCollections.error_code).toEqual(ErrorCode.SUCCESS); + }); + // it(`drop database should be ok`, async () => { // const all = await milvusClient.listDatabases();