fix: [2.4] Check whether new collection name is alias (#36981) (#37208)

Cherry pick from master
pr: #36981

Related to #36963

---------

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
pull/37224/head
congqixia 2024-10-28 22:46:24 +08:00 committed by GitHub
parent 79e6ef2617
commit b44ef8207e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 0 deletions

View File

@ -780,6 +780,12 @@ func (mt *MetaTable) RenameCollection(ctx context.Context, dbName string, oldNam
return fmt.Errorf("unsupported use an alias to rename collection, alias:%s", oldName)
}
_, ok = mt.aliases.get(newDBName, newName)
if ok {
log.Warn("cannot rename collection to an existing alias")
return fmt.Errorf("cannot rename collection to an existing alias: %s", newName)
}
// check new collection already exists
newColl, err := mt.getCollectionByNameInternal(ctx, newDBName, newName, ts)
if newColl != nil {

View File

@ -1582,6 +1582,36 @@ func TestMetaTable_RenameCollection(t *testing.T) {
assert.Error(t, err)
})
t.Run("rename db name fails if aliases exists", func(t *testing.T) {
catalog := mocks.NewRootCoordCatalog(t)
catalog.On("GetCollectionByName",
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
).Return(nil, merr.WrapErrCollectionNotFound("error"))
meta := &MetaTable{
dbName2Meta: map[string]*model.Database{
util.DefaultDBName: model.NewDefaultDatabase(),
"db1": model.NewDatabase(2, "db1", pb.DatabaseState_DatabaseCreated, nil),
},
catalog: catalog,
names: newNameDb(),
aliases: newNameDb(),
collID2Meta: map[typeutil.UniqueID]*model.Collection{
1: {
CollectionID: 1,
Name: "old",
},
},
}
meta.names.insert(util.DefaultDBName, "old", 1)
meta.aliases.insert(util.DefaultDBName, "new", 1)
err := meta.RenameCollection(context.TODO(), util.DefaultDBName, "old", "db1", "new", 1000)
assert.Error(t, err)
})
t.Run("alter collection ok", func(t *testing.T) {
catalog := mocks.NewRootCoordCatalog(t)
catalog.On("AlterCollection",