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

abeizn pushed a commit to branch release-v0.19
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/release-v0.19 by this push:
     new 27ec86057 fix: data too long for column file_metrics_key (#6157)
27ec86057 is described below

commit 27ec86057cbf79afdec91ed5ee0a1c4668dcfefa
Author: abeizn <[email protected]>
AuthorDate: Thu Sep 28 09:56:08 2023 +0800

    fix: data too long for column file_metrics_key (#6157)
    
    * fix: data too long for column file_metrics_key
    
    * fix: primaryKey
    
    * fix: add db transactions
    
    * fix: ci lint
---
 .../20230927_modify_file_metrics_key_length.go     | 82 ++++++++++++++++++++++
 .../sonarqube/models/migrationscripts/register.go  |  1 +
 .../sonarqube/models/sonarqube_file_metrics.go     |  6 +-
 3 files changed, 86 insertions(+), 3 deletions(-)

diff --git 
a/backend/plugins/sonarqube/models/migrationscripts/20230927_modify_file_metrics_key_length.go
 
b/backend/plugins/sonarqube/models/migrationscripts/20230927_modify_file_metrics_key_length.go
new file mode 100644
index 000000000..bd62e7cf3
--- /dev/null
+++ 
b/backend/plugins/sonarqube/models/migrationscripts/20230927_modify_file_metrics_key_length.go
@@ -0,0 +1,82 @@
+/*
+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/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 = (*modifyFileMetricsKeyLength)(nil)
+
+type modifyFileMetricsKeyLength struct{}
+
+type fileMetricsKey20230927 struct {
+       FileMetricsKey string `gorm:"primaryKey;type:varchar(500)"`
+}
+
+func (fileMetricsKey20230927) TableName() string {
+       return "_tool_sonarqube_file_metrics"
+}
+
+func (script *modifyFileMetricsKeyLength) Up(basicRes context.BasicRes) 
errors.Error {
+       db := basicRes.GetDal()
+       tx := db.Begin()
+       err := migrationhelper.ChangeColumnsType[fileMetricsKey20230927](
+               basicRes,
+               script,
+               fileMetricsKey20230927{}.TableName(),
+               []string{"file_metrics_key"},
+               func(tmpColumnParams []interface{}) errors.Error {
+                       return db.UpdateColumn(
+                               &fileMetricsKey20230927{},
+                               "file_metrics_key",
+                               dal.DalClause{Expr: " ? ", Params: 
tmpColumnParams},
+                               dal.Where("? != '' ", tmpColumnParams...),
+                       )
+               },
+       )
+       if err != nil {
+               return tx.Rollback()
+       }
+
+       if err = tx.Exec("ALTER TABLE _tool_sonarqube_file_metrics DROP PRIMARY 
KEY"); err != nil {
+               return tx.Rollback()
+       }
+
+       if err := tx.Exec("ALTER TABLE _tool_sonarqube_file_metrics ADD PRIMARY 
KEY (connection_id, file_metrics_key)"); err != nil {
+               return tx.Rollback()
+       }
+
+       if err := tx.Commit(); err != nil {
+               return tx.Rollback()
+       }
+
+       return nil
+}
+
+func (*modifyFileMetricsKeyLength) Version() uint64 {
+       return 20230927145127
+}
+
+func (*modifyFileMetricsKeyLength) Name() string {
+       return "modify _tool_sonarqube_file_metrics file_metrics_key from 
varchar(191) to varchar(500)"
+}
diff --git a/backend/plugins/sonarqube/models/migrationscripts/register.go 
b/backend/plugins/sonarqube/models/migrationscripts/register.go
index d1f70cf34..c756d0120 100644
--- a/backend/plugins/sonarqube/models/migrationscripts/register.go
+++ b/backend/plugins/sonarqube/models/migrationscripts/register.go
@@ -26,6 +26,7 @@ func All() []plugin.MigrationScript {
                new(modifyCharacterSet),
                new(expandProjectKey20230206),
                new(addRawParamTableForScope),
+               new(modifyFileMetricsKeyLength),
                new(modifyComponentLength),
        }
 }
diff --git a/backend/plugins/sonarqube/models/sonarqube_file_metrics.go 
b/backend/plugins/sonarqube/models/sonarqube_file_metrics.go
index c2e36f2fb..a7acef9c1 100644
--- a/backend/plugins/sonarqube/models/sonarqube_file_metrics.go
+++ b/backend/plugins/sonarqube/models/sonarqube_file_metrics.go
@@ -23,7 +23,7 @@ import (
 
 type SonarqubeFileMetrics struct {
        ConnectionId             uint64 `gorm:"primaryKey"`
-       FileMetricsKey           string `gorm:"primaryKey"`
+       FileMetricsKey           string `gorm:"primaryKey;type:varchar(500)"`
        ProjectKey               string `gorm:"index"`
        FileName                 string
        FilePath                 string
@@ -51,7 +51,7 @@ func (SonarqubeFileMetrics) TableName() string {
 
 type SonarqubeAdditionalFileMetrics struct {
        ConnectionId                        uint64 `gorm:"primaryKey"`
-       FileMetricsKey                      string `gorm:"primaryKey"`
+       FileMetricsKey                      string 
`gorm:"primaryKey;type:varchar(500)"`
        DuplicatedFiles                     int
        DuplicatedLines                     int
        EffortToReachMaintainabilityRatingA int
@@ -69,7 +69,7 @@ func (SonarqubeAdditionalFileMetrics) TableName() string {
 
 type SonarqubeWholeFileMetrics struct {
        ConnectionId                        uint64 `gorm:"primaryKey"`
-       FileMetricsKey                      string `gorm:"primaryKey"`
+       FileMetricsKey                      string 
`gorm:"primaryKey;type:varchar(500)"`
        ProjectKey                          string `gorm:"index"`
        FileName                            string `gorm:"type:varchar(255)"`
        FilePath                            string

Reply via email to