From f1fe0cfe88c3e8850554b49a6e319f78b1a0fdfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 30 Nov 2021 18:09:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=B7=B2=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E7=A9=BA=E9=97=B4,=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E6=80=A7=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- transform/migrate.go | 78 +++++++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 22 deletions(-) diff --git a/transform/migrate.go b/transform/migrate.go index b63bce0..6d3c72a 100644 --- a/transform/migrate.go +++ b/transform/migrate.go @@ -61,49 +61,83 @@ func Migrate(dataDriver ITransform, optionList ...SetMigrateOption) error { } var ( - sourceData map[string]string - err error - namespaceDetail *model.Namespace + sourceData map[string]string + err error + namespaceDetail *model.Namespace + hasCreateConfigList []*model.Config ) if sourceData, err = dataDriver.GetSourceData(); nil != err { return err } + namespaceID := int64(0) if namespaceDetail, err = manager.Namespace.GetNamespaceDetail(0, namespace); nil == err && nil != namespaceDetail { + namespaceID = namespaceDetail.ID if !mo.Force { // 非强制迁移, 报错 return fmt.Errorf("当前命名空间 %s(%v) 已存在, 迁移存在风险, 若要强制迁移, 请使用force=true", namespace, namespaceDetail.ID) } // 强制迁移, 需要查询已有的key, 用于判断后续是 + if hasCreateConfigList, err = manager.Config.GetNamespaceConfig(namespaceDetail.ID); nil != err { + return fmt.Errorf("当前命名空间 %s(%v) 已存在, 强制迁移查询已存在的配置失败: %s", namespace, namespaceDetail.ID, err.Error()) + } } - // 创建命名空间 - if err = manager.Namespace.Create(&form.CreateNamespace{ - Namespace: namespace, - Name: "apollo迁移-" + namespace, - Description: "从apollo迁移过来的配置", - CreateUserID: "migrate-script", - }); nil != err { - return errors.New("创建命名空间失败 : " + err.Error()) - } - // 激活命名空间 - if err = manager.Namespace.Active(&form.ActiveNamespace{ - NamespaceID: 0, - Namespace: namespace, - UpdateUserID: "system-script", - }); nil != err { - return errors.New("激活命名空间失败 : " + err.Error()) + if namespaceID == 0 { + // 创建命名空间 + if err = manager.Namespace.Create(&form.CreateNamespace{ + Namespace: namespace, + Name: "apollo迁移-" + namespace, + Description: "从apollo迁移过来的配置", + CreateUserID: "migrate-script", + }); nil != err { + return errors.New("创建命名空间失败 : " + err.Error()) + } } + if nil != namespaceDetail && namespaceDetail.Status != model.NamespaceStatusNormal { + // 激活命名空间 + if err = manager.Namespace.Active(&form.ActiveNamespace{ + NamespaceID: 0, + Namespace: namespace, + UpdateUserID: "system-script", + }); nil != err { + return errors.New("激活命名空间失败 : " + err.Error()) + } + } + + hasCreateConfigTable := make(map[string]*model.Config) + for _, item := range hasCreateConfigList { + hasCreateConfigTable[item.Field] = item + } // 迁移配置 for key, value := range sourceData { - if err = manager.Config.Create(&form.CreateOrUpdateConfig{ - NamespaceID: 0, + hasSetConfig, exist := hasCreateConfigTable[key] + if !exist { + if err = manager.Config.Create(&form.CreateOrUpdateConfig{ + NamespaceID: 0, + Namespace: namespace, + Key: key, + Value: value, + OperateUserID: "system-script", + Description: fmt.Sprintf("从apollo-%s-%s迁移过来的配置", namespace, key), + }, namespaceDetail); nil != err { + dataDriver.MigrateKeyFailCallback(namespace, key, value, err) + } + continue + } + // 已存在更新,执行下面的逻辑 + if hasSetConfig.Value == value { + // 已存在值和目标值一致,无需处理 + continue + } + if err = manager.Config.Update(&form.CreateOrUpdateConfig{ + NamespaceID: namespaceDetail.ID, Namespace: namespace, Key: key, Value: value, OperateUserID: "system-script", Description: fmt.Sprintf("从apollo-%s-%s迁移过来的配置", namespace, key), - }, namespaceDetail); nil != err { + }, namespaceDetail, hasSetConfig); nil != err { dataDriver.MigrateKeyFailCallback(namespace, key, value, err) } }