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 06afe2cfd refactor: rename project metrics (#4023)
06afe2cfd is described below

commit 06afe2cfd1f3a7eaa87028df9c5dde7e4fbf24fb
Author: mappjzc <[email protected]>
AuthorDate: Mon Dec 26 14:42:14 2022 +0800

    refactor: rename project metrics (#4023)
    
    rename project metrics to project metric setting.
    
    Nddtfjiang <[email protected]>
---
 .../20221109_rename_project_metrics.go             | 48 ++++++++++++++++++++++
 models/migrationscripts/register.go                |  1 +
 models/project.go                                  | 10 ++---
 services/blueprint.go                              |  2 +-
 services/project.go                                | 10 ++---
 5 files changed, 60 insertions(+), 11 deletions(-)

diff --git a/models/migrationscripts/20221109_rename_project_metrics.go 
b/models/migrationscripts/20221109_rename_project_metrics.go
new file mode 100644
index 000000000..5f141b5b7
--- /dev/null
+++ b/models/migrationscripts/20221109_rename_project_metrics.go
@@ -0,0 +1,48 @@
+/*
+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 (
+       "github.com/apache/incubator-devlake/errors"
+       "github.com/apache/incubator-devlake/plugins/core"
+)
+
+var _ core.MigrationScript = (*renameProjectMetrics)(nil)
+
+type renameProjectMetrics struct{}
+
+type ProjectMetricSetting struct {
+}
+
+func (ProjectMetricSetting) TableName() string {
+       return "project_metric_settings"
+}
+
+func (script *renameProjectMetrics) Up(basicRes core.BasicRes) errors.Error {
+       db := basicRes.GetDal()
+       // To create multiple tables with migrationhelper
+       return db.RenameTable(ProjectMetric{}.TableName(), 
ProjectMetricSetting{}.TableName())
+}
+
+func (*renameProjectMetrics) Version() uint64 {
+       return 20222123191424
+}
+
+func (*renameProjectMetrics) Name() string {
+       return "rename project metrics"
+}
diff --git a/models/migrationscripts/register.go 
b/models/migrationscripts/register.go
index 9d0e01fd0..f1487f6e0 100644
--- a/models/migrationscripts/register.go
+++ b/models/migrationscripts/register.go
@@ -67,5 +67,6 @@ func All() []core.MigrationScript {
                new(addOriginalProject),
                new(addErrorName),
                new(encryptTask221221),
+               new(renameProjectMetrics),
        }
 }
diff --git a/models/project.go b/models/project.go
index 2b7c4ab22..6e8cba30e 100644
--- a/models/project.go
+++ b/models/project.go
@@ -39,18 +39,18 @@ type BaseMetric struct {
        Enable       bool   `json:"enable" mapstructure:"enable" 
gorm:"type:boolean"`
 }
 
-type BaseProjectMetric struct {
+type BaseProjectMetricSetting struct {
        ProjectName string `json:"projectName" mapstructure:"projectName" 
gorm:"primaryKey;type:varchar(255)"`
        BaseMetric  `mapstructure:",squash"`
 }
 
-type ProjectMetric struct {
-       BaseProjectMetric `mapstructure:",squash"`
+type ProjectMetricSetting struct {
+       BaseProjectMetricSetting `mapstructure:",squash"`
        common.NoPKModel
 }
 
-func (ProjectMetric) TableName() string {
-       return "project_metrics"
+func (ProjectMetricSetting) TableName() string {
+       return "project_metric_settings"
 }
 
 type ApiInputProject struct {
diff --git a/services/blueprint.go b/services/blueprint.go
index 8c98556b2..2b0eb6319 100644
--- a/services/blueprint.go
+++ b/services/blueprint.go
@@ -319,7 +319,7 @@ func MakePlanForBlueprint(blueprint *models.Blueprint) 
(core.PipelinePlan, error
        case "2.0.0":
                // load project metric plugins and convert it to a map
                metrics := make(map[string]json.RawMessage)
-               projectMetrics := make([]models.ProjectMetric, 0)
+               projectMetrics := make([]models.ProjectMetricSetting, 0)
                if blueprint.ProjectName != "" {
                        err = db.All(&projectMetrics, dal.Where("project_name = 
? AND enable = ?", blueprint.ProjectName, true))
                        if err != nil {
diff --git a/services/project.go b/services/project.go
index 0063ec355..96be27e0a 100644
--- a/services/project.go
+++ b/services/project.go
@@ -166,7 +166,7 @@ func PatchProject(name string, body map[string]interface{}) 
(*models.ApiOutputPr
        if name != project.Name {
                // ProjectMetric
                err = tx.UpdateColumn(
-                       &models.ProjectMetric{},
+                       &models.ProjectMetricSetting{},
                        "project_name", project.Name,
                        dal.Where("project_name = ?", name),
                )
@@ -250,14 +250,14 @@ func PatchProject(name string, body 
map[string]interface{}) (*models.ApiOutputPr
 }
 
 func refreshProjectMetrics(tx dal.Transaction, projectInput 
*models.ApiInputProject) errors.Error {
-       err := tx.Delete(&models.ProjectMetric{}, dal.Where("project_name = ?", 
projectInput.Name))
+       err := tx.Delete(&models.ProjectMetricSetting{}, 
dal.Where("project_name = ?", projectInput.Name))
        if err != nil {
                return err
        }
 
        for _, baseMetric := range *projectInput.Metrics {
-               err = tx.Create(&models.ProjectMetric{
-                       BaseProjectMetric: models.BaseProjectMetric{
+               err = tx.Create(&models.ProjectMetricSetting{
+                       BaseProjectMetricSetting: 
models.BaseProjectMetricSetting{
                                ProjectName: projectInput.Name,
                                BaseMetric:  baseMetric,
                        },
@@ -273,7 +273,7 @@ func makeProjectOutput(baseProject *models.BaseProject) 
(*models.ApiOutputProjec
        projectOutput := &models.ApiOutputProject{}
        projectOutput.BaseProject = *baseProject
        // load project metrics
-       projectMetrics := make([]models.ProjectMetric, 0)
+       projectMetrics := make([]models.ProjectMetricSetting, 0)
        err := db.All(&projectMetrics, dal.Where("project_name = ?", 
projectOutput.Name))
        if err != nil {
                return nil, errors.Default.Wrap(err, "failed to load project 
metrics")

Reply via email to