From b44ef8207efc2811d1bef35bf238da37f00141bf Mon Sep 17 00:00:00 2001 From: congqixia Date: Mon, 28 Oct 2024 22:46:24 +0800 Subject: [PATCH] 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 --- internal/rootcoord/meta_table.go | 6 ++++++ internal/rootcoord/meta_table_test.go | 30 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/internal/rootcoord/meta_table.go b/internal/rootcoord/meta_table.go index 6cd10eb54cfb3..f86eb82297c66 100644 --- a/internal/rootcoord/meta_table.go +++ b/internal/rootcoord/meta_table.go @@ -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 { diff --git a/internal/rootcoord/meta_table_test.go b/internal/rootcoord/meta_table_test.go index 1aed09cc80970..7bd666f7ca3c4 100644 --- a/internal/rootcoord/meta_table_test.go +++ b/internal/rootcoord/meta_table_test.go @@ -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",