Skip to content

Commit

Permalink
support rename collection to another database
Browse files Browse the repository at this point in the history
Signed-off-by: ryjiang <[email protected]>
  • Loading branch information
shanghaikid committed Aug 6, 2024
1 parent 4928e7f commit 81b71ae
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
22 changes: 18 additions & 4 deletions milvus/grpc/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResStatus>} The response status of the operation.
Expand All @@ -538,13 +540,25 @@ export class Collection extends Database {
* ```
*/
async renameCollection(data: RenameCollectionReq): Promise<ResStatus> {
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;
Expand Down
1 change: 1 addition & 0 deletions milvus/types/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 34 additions & 2 deletions test/grpc/Database.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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] })
);
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 81b71ae

Please sign in to comment.