From 08e10bc37639ebd07cf12575706db47068bb135f Mon Sep 17 00:00:00 2001 From: bd-xiaowang Date: Thu, 10 Oct 2024 15:44:19 +0800 Subject: [PATCH] =?UTF-8?q?3.9=E4=BF=AE=E5=A4=8D=E4=B8=BB=E6=9C=BA?= =?UTF-8?q?=E4=B8=8E=E5=AE=9E=E4=BE=8B=E8=A1=A8=E5=94=AF=E4=B8=80=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E7=BC=BA=E5=A4=B1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scene_server/admin_server/imports.go | 1 + .../y3.9.202410101500/create_table_index.go | 92 +++++++++++++++++++ .../upgrader/y3.9.202410101500/pkg.go | 42 +++++++++ 3 files changed, 135 insertions(+) create mode 100644 src/scene_server/admin_server/upgrader/y3.9.202410101500/create_table_index.go create mode 100644 src/scene_server/admin_server/upgrader/y3.9.202410101500/pkg.go diff --git a/src/scene_server/admin_server/imports.go b/src/scene_server/admin_server/imports.go index ab5a9e0453..743f6aae37 100644 --- a/src/scene_server/admin_server/imports.go +++ b/src/scene_server/admin_server/imports.go @@ -135,4 +135,5 @@ import ( _ "configcenter/src/scene_server/admin_server/upgrader/y3.9.202112061431" _ "configcenter/src/scene_server/admin_server/upgrader/y3.9.202112071130" _ "configcenter/src/scene_server/admin_server/upgrader/y3.9.202112071431" + _ "configcenter/src/scene_server/admin_server/upgrader/y3.9.202410101500" ) diff --git a/src/scene_server/admin_server/upgrader/y3.9.202410101500/create_table_index.go b/src/scene_server/admin_server/upgrader/y3.9.202410101500/create_table_index.go new file mode 100644 index 0000000000..781c5b6d7b --- /dev/null +++ b/src/scene_server/admin_server/upgrader/y3.9.202410101500/create_table_index.go @@ -0,0 +1,92 @@ +/* + * Tencent is pleased to support the open source community by making + * 蓝鲸智云 - 配置平台 (BlueKing - Configuration System) available. + * Copyright (C) 2017 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * We undertake not to change the open source license (MIT license) applicable + * to the current version of the project delivered to anyone in the future. + */ + +package y3_9_202410101500 + +import ( + "context" + "reflect" + + "configcenter/src/common" + "configcenter/src/common/blog" + "configcenter/src/storage/dal" + "configcenter/src/storage/dal/types" +) + +var ( + newIndexMap = map[string]types.Index{ + common.BKTableNameBaseHost: { + Name: "bkcc_unique_bkHostInnerIP_bkCloudID", + Keys: map[string]int32{ + common.BKCloudIDField: 1, common.BKHostInnerIPField: 1, + }, + Background: true, + Unique: true, + }, + common.BKTableNameBaseInst: { + Name: "bkcc_unique_bkObjectID_bkInstName", + Keys: map[string]int32{ + common.BKObjIDField: 1, common.BKInstNameField: 1, + }, + Background: true, + Unique: true, + }, + } +) + +// updateTableIndex update cc_HostBase and cc_ObjectBase table index +func updateTableIndex(ctx context.Context, db dal.RDB) error { + for table, newIndex := range newIndexMap { + existIndexes, err := db.Table(table).Indexes(ctx) + if err != nil { + return err + } + + shouldCreate := true + + for _, idx := range existIndexes { + if idx.Name == newIndex.Name { + shouldCreate = false + break + } + // 判断索引是否与目标索引键相同 + if !reflect.DeepEqual(idx.Keys, newIndex.Keys) { + continue + } + // 判断索引是否是唯一索引 + if idx.Unique { + shouldCreate = false + break + } + // 如果索引键匹配但非唯一,删除该索引 + if err := db.Table(table).DropIndex(ctx, idx.Name); err != nil { + blog.Errorf("drop table index failed, table: %s, index: %s, err: %v", table, idx.Name, err) + return err + } + } + + if !shouldCreate { + continue + } + if err := db.Table(table).CreateIndex(ctx, newIndex); err != nil { + blog.Errorf("create table index failed, table: %s, index: %s, err: %v", table, newIndex.Name, err) + return err + } + } + + return nil +} diff --git a/src/scene_server/admin_server/upgrader/y3.9.202410101500/pkg.go b/src/scene_server/admin_server/upgrader/y3.9.202410101500/pkg.go new file mode 100644 index 0000000000..cae7941d1f --- /dev/null +++ b/src/scene_server/admin_server/upgrader/y3.9.202410101500/pkg.go @@ -0,0 +1,42 @@ +/* + * Tencent is pleased to support the open source community by making + * 蓝鲸智云 - 配置平台 (BlueKing - Configuration System) available. + * Copyright (C) 2017 THL A29 Limited, + * a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + * We undertake not to change the open source license (MIT license) applicable + * to the current version of the project delivered to anyone in the future. + */ + +package y3_9_202410101500 + +import ( + "context" + + "configcenter/src/common/blog" + "configcenter/src/scene_server/admin_server/upgrader" + "configcenter/src/storage/dal" +) + +func init() { + upgrader.RegistUpgrader("y3.9.202410101500", upgrade) +} + +func upgrade(ctx context.Context, db dal.RDB, conf *upgrader.Config) (err error) { + blog.Infof("start execute y3.9.202410101500") + + if err = updateTableIndex(ctx, db); err != nil { + blog.Errorf("upgrade y3.9.202410101500 update table index failed, err: %v", err) + return err + } + + blog.Infof("upgrade y3.9.202410101500 update table index success") + return nil +}