This is an automated email from the ASF dual-hosted git repository.

klesh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 208b50122 feat(framework): update `pluginOption` field's type in 
projects (#7505)
208b50122 is described below

commit 208b501222a7b9a783cd73d8ca9a79078213dac8
Author: Lynwee <[email protected]>
AuthorDate: Fri May 24 11:23:45 2024 +0800

    feat(framework): update `pluginOption` field's type in projects (#7505)
---
 ...ate_plugin_option_in_project_metric_settings.go | 69 ++++++++++++++++++++++
 backend/core/models/migrationscripts/register.go   |  1 +
 backend/core/models/project.go                     |  6 +-
 backend/server/services/blueprint.go               |  2 +-
 backend/test/helper/api.go                         |  4 +-
 5 files changed, 76 insertions(+), 6 deletions(-)

diff --git 
a/backend/core/models/migrationscripts/20240523_update_plugin_option_in_project_metric_settings.go
 
b/backend/core/models/migrationscripts/20240523_update_plugin_option_in_project_metric_settings.go
new file mode 100644
index 000000000..94fb29e18
--- /dev/null
+++ 
b/backend/core/models/migrationscripts/20240523_update_plugin_option_in_project_metric_settings.go
@@ -0,0 +1,69 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+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.
+*/
+
+package migrationscripts
+
+import (
+       "encoding/json"
+       "github.com/apache/incubator-devlake/core/context"
+       "github.com/apache/incubator-devlake/core/dal"
+       "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/plugin"
+       "github.com/apache/incubator-devlake/helpers/migrationhelper"
+)
+
+var _ plugin.MigrationScript = (*addCommitShaToCicdRelease)(nil)
+
+type projectMetricSettings20240523 struct {
+       PluginOption json.RawMessage `gorm:"type:json"`
+}
+
+func (projectMetricSettings20240523) TableName() string {
+       return "project_metric_settings"
+}
+
+type updatePluginOptionInProjectMetricSetting struct{}
+
+func (u *updatePluginOptionInProjectMetricSetting) Up(basicRes 
context.BasicRes) errors.Error {
+       db := basicRes.GetDal()
+       if err := 
migrationhelper.ChangeColumnsType[projectMetricSettings20240523](
+               basicRes,
+               u,
+               projectMetricSettings20240523{}.TableName(),
+               []string{"plugin_option"},
+               func(tmpColumnParams []interface{}) errors.Error {
+                       return db.UpdateColumn(
+                               &projectMetricSettings20240523{},
+                               "plugin_option",
+                               dal.DalClause{Expr: " ? ", Params: 
[]interface{}{"{}"}},
+                               dal.Where("? is not null", tmpColumnParams...),
+                       )
+               },
+       ); err != nil {
+               return err
+
+       }
+       return nil
+}
+
+func (*updatePluginOptionInProjectMetricSetting) Version() uint64 {
+       return 20240523194205
+}
+
+func (*updatePluginOptionInProjectMetricSetting) Name() string {
+       return "update plugin_option's type in project_metric_settings"
+}
diff --git a/backend/core/models/migrationscripts/register.go 
b/backend/core/models/migrationscripts/register.go
index 5e1efde06..08f5c6b25 100644
--- a/backend/core/models/migrationscripts/register.go
+++ b/backend/core/models/migrationscripts/register.go
@@ -116,5 +116,6 @@ func All() []plugin.MigrationScript {
                new(addCicdRelease),
                new(addCommitShaToCicdRelease),
                new(updateIssueKeyType),
+               new(updatePluginOptionInProjectMetricSetting),
        }
 }
diff --git a/backend/core/models/project.go b/backend/core/models/project.go
index 56cd3b500..99c61785d 100644
--- a/backend/core/models/project.go
+++ b/backend/core/models/project.go
@@ -39,9 +39,9 @@ func (Project) TableName() string {
 }
 
 type BaseMetric struct {
-       PluginName   string `json:"pluginName" mapstructure:"pluginName" 
gorm:"primaryKey;type:varchar(255)" validate:"required"`
-       PluginOption string `json:"pluginOption" mapstructure:"pluginOption" 
gorm:"type:text"`
-       Enable       bool   `json:"enable" mapstructure:"enable" 
gorm:"type:boolean"`
+       PluginName   string          `json:"pluginName" 
mapstructure:"pluginName" gorm:"primaryKey;type:varchar(255)" 
validate:"required"`
+       PluginOption json.RawMessage `json:"pluginOption" 
mapstructure:"pluginOption" gorm:"type:json"`
+       Enable       bool            `json:"enable" mapstructure:"enable" 
gorm:"type:boolean"`
 }
 
 type BaseProjectMetricSetting struct {
diff --git a/backend/server/services/blueprint.go 
b/backend/server/services/blueprint.go
index 2f7ddb51a..28da22159 100644
--- a/backend/server/services/blueprint.go
+++ b/backend/server/services/blueprint.go
@@ -356,7 +356,7 @@ func MakePlanForBlueprint(blueprint *models.Blueprint, 
syncPolicy *models.SyncPo
                        return nil, err
                }
                for _, projectMetric := range projectMetrics {
-                       metrics[projectMetric.PluginName] = 
json.RawMessage(projectMetric.PluginOption)
+                       metrics[projectMetric.PluginName] = 
projectMetric.PluginOption
                }
        }
        skipCollectors := false
diff --git a/backend/test/helper/api.go b/backend/test/helper/api.go
index 2af718f35..e07c89d4b 100644
--- a/backend/test/helper/api.go
+++ b/backend/test/helper/api.go
@@ -168,14 +168,14 @@ func (d *DevlakeClient) CreateProject(project 
*ProjectConfig) models.ApiOutputPr
                }
                metrics = append(metrics, &models.BaseMetric{
                        PluginName:   p.Name,
-                       PluginOption: string(ToJson(p.Options)),
+                       PluginOption: ToJson(p.Options),
                        Enable:       true,
                })
        }
        if project.EnableDora && !doraSeen {
                metrics = append(metrics, &models.BaseMetric{
                        PluginName:   "dora",
-                       PluginOption: string(ToJson(nil)),
+                       PluginOption: ToJson(nil),
                        Enable:       true,
                })
        }

Reply via email to