From b0df93aff840837e7b74639e69fa1aa53ac8bd02 Mon Sep 17 00:00:00 2001 From: Josip Stojak Date: Sun, 2 Jun 2024 10:35:33 +0200 Subject: [PATCH 1/5] adding pull_request_id index to pull_request_commits/comments tables --- ..._request_id_index_for_pr_comments_table.go | 29 +++++++++++++++++++ ...l_request_id_index_for_pr_commits_table.go | 29 +++++++++++++++++++ .../core/models/migrationscripts/register.go | 2 ++ 3 files changed, 60 insertions(+) create mode 100644 backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_comments_table.go create mode 100644 backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go diff --git a/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_comments_table.go b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_comments_table.go new file mode 100644 index 00000000000..f072edd6479 --- /dev/null +++ b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_comments_table.go @@ -0,0 +1,29 @@ +package migrationscripts + +import ( + "github.com/apache/incubator-devlake/core/context" + "github.com/apache/incubator-devlake/core/errors" + "github.com/apache/incubator-devlake/helpers/migrationhelper" +) + +type addPullRequestIdIndexToPullRequestComments struct{} + +type pullRequestComments20240602 struct { + PullRequestId string `gorm:"index"` +} + +func (pullRequestComments20240602) TableName() string { + return "pull_request_comments" +} + +func (u *addPullRequestIdIndexToPullRequestComments) Up(basicRes context.BasicRes) errors.Error { + return migrationhelper.AutoMigrateTables(basicRes, &pullRequestComments20240602{}) +} + +func (*addPullRequestIdIndexToPullRequestComments) Version() uint64 { + return 20240602103401 +} + +func (*addPullRequestIdIndexToPullRequestComments) Name() string { + return "add pull_request_id index for pull_request_comments" +} diff --git a/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go new file mode 100644 index 00000000000..b96bb00b9d7 --- /dev/null +++ b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go @@ -0,0 +1,29 @@ +package migrationscripts + +import ( + "github.com/apache/incubator-devlake/core/context" + "github.com/apache/incubator-devlake/core/errors" + "github.com/apache/incubator-devlake/helpers/migrationhelper" +) + +type addPullRequestIdIndexToPullRequestCommits struct{} + +type pullRequestCommits20240602 struct { + PullRequestId string `gorm:"index"` +} + +func (pullRequestCommits20240602) TableName() string { + return "pull_request_commits" +} + +func (u *addPullRequestIdIndexToPullRequestCommits) Up(basicRes context.BasicRes) errors.Error { + return migrationhelper.AutoMigrateTables(basicRes, &pullRequestCommits20240602{}) +} + +func (*addPullRequestIdIndexToPullRequestCommits) Version() uint64 { + return 20240602103400 +} + +func (*addPullRequestIdIndexToPullRequestCommits) Name() string { + return "add pull_request_id index for pull_request_commits" +} diff --git a/backend/core/models/migrationscripts/register.go b/backend/core/models/migrationscripts/register.go index c434700bcba..2fdcb0279a2 100644 --- a/backend/core/models/migrationscripts/register.go +++ b/backend/core/models/migrationscripts/register.go @@ -119,5 +119,7 @@ func All() []plugin.MigrationScript { new(updatePluginOptionInProjectMetricSetting), new(modifyCicdDeploymentCommitsRepoUrlLength), new(modifyCicdPipelineCommitsRepoUrlLength), + new(addPullRequestIdIndexToPullRequestCommits), + new(addPullRequestIdIndexToPullRequestComments), } } From 93f59dfd3c9a719fbb1b3af686b5369956670a4e Mon Sep 17 00:00:00 2001 From: Josip Stojak Date: Mon, 3 Jun 2024 13:34:38 +0200 Subject: [PATCH 2/5] change the pull_request_commits primary key columns order --- ...l_request_id_index_for_pr_commits_table.go | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go index b96bb00b9d7..c8976d17dc8 100644 --- a/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go +++ b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go @@ -3,21 +3,22 @@ package migrationscripts import ( "github.com/apache/incubator-devlake/core/context" "github.com/apache/incubator-devlake/core/errors" - "github.com/apache/incubator-devlake/helpers/migrationhelper" ) type addPullRequestIdIndexToPullRequestCommits struct{} -type pullRequestCommits20240602 struct { - PullRequestId string `gorm:"index"` -} - -func (pullRequestCommits20240602) TableName() string { - return "pull_request_commits" -} - func (u *addPullRequestIdIndexToPullRequestCommits) Up(basicRes context.BasicRes) errors.Error { - return migrationhelper.AutoMigrateTables(basicRes, &pullRequestCommits20240602{}) + db := basicRes.GetDal() + err := db.Exec("ALTER TABLE pull_request_commits DROP PRIMARY KEY;") + if err != nil { + return err + } + err = db.Exec("ALTER TABLE pull_request_commits ADD PRIMARY KEY (pull_request_id, commit_sha);") + if err != nil { + return err + } + + return nil } func (*addPullRequestIdIndexToPullRequestCommits) Version() uint64 { @@ -25,5 +26,5 @@ func (*addPullRequestIdIndexToPullRequestCommits) Version() uint64 { } func (*addPullRequestIdIndexToPullRequestCommits) Name() string { - return "add pull_request_id index for pull_request_commits" + return "changing pull_request_commits primary key columns order" } From 27ccff9b32c1fb46c1b498d8f2dd656c0ca38a9a Mon Sep 17 00:00:00 2001 From: Josip Stojak Date: Mon, 3 Jun 2024 20:50:33 +0200 Subject: [PATCH 3/5] adding Apache license header --- ...ll_request_id_index_for_pr_comments_table.go | 17 +++++++++++++++++ ...ull_request_id_index_for_pr_commits_table.go | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_comments_table.go b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_comments_table.go index f072edd6479..9e6d7b6edc9 100644 --- a/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_comments_table.go +++ b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_comments_table.go @@ -1,3 +1,20 @@ +/* +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 ( diff --git a/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go index c8976d17dc8..db779e830ce 100644 --- a/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go +++ b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go @@ -1,3 +1,20 @@ +/* +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 ( From 75a27892722d2e3d5816f43263789a1d48672c3d Mon Sep 17 00:00:00 2001 From: Josip Stojak Date: Mon, 3 Jun 2024 21:13:19 +0200 Subject: [PATCH 4/5] only run for mysql --- ...l_request_id_index_for_pr_commits_table.go | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go index db779e830ce..cf22f2b7c4d 100644 --- a/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go +++ b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go @@ -20,21 +20,31 @@ package migrationscripts import ( "github.com/apache/incubator-devlake/core/context" "github.com/apache/incubator-devlake/core/errors" + "net/url" ) type addPullRequestIdIndexToPullRequestCommits struct{} -func (u *addPullRequestIdIndexToPullRequestCommits) Up(basicRes context.BasicRes) errors.Error { - db := basicRes.GetDal() - err := db.Exec("ALTER TABLE pull_request_commits DROP PRIMARY KEY;") - if err != nil { - return err +func (*addPullRequestIdIndexToPullRequestCommits) Up(basicRes context.BasicRes) errors.Error { + dbUrl := basicRes.GetConfig("DB_URL") + if dbUrl == "" { + return errors.BadInput.New("DB_URL is required") } - err = db.Exec("ALTER TABLE pull_request_commits ADD PRIMARY KEY (pull_request_id, commit_sha);") - if err != nil { - return err + u, err1 := url.Parse(dbUrl) + if err1 != nil { + return errors.Convert(err1) + } + if u.Scheme == "mysql" { + db := basicRes.GetDal() + err := db.Exec("ALTER TABLE pull_request_commits DROP PRIMARY KEY;") + if err != nil { + return err + } + err = db.Exec("ALTER TABLE pull_request_commits ADD PRIMARY KEY (pull_request_id, commit_sha);") + if err != nil { + return err + } } - return nil } From a03564cd932c1fd10e7638a0634017058418da51 Mon Sep 17 00:00:00 2001 From: Josip Stojak Date: Thu, 6 Jun 2024 14:30:41 +0200 Subject: [PATCH 5/5] adding support for postgres --- ...l_request_id_index_for_pr_commits_table.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go index cf22f2b7c4d..ddf381edcd2 100644 --- a/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go +++ b/backend/core/models/migrationscripts/20240603_add_pull_request_id_index_for_pr_commits_table.go @@ -21,6 +21,7 @@ import ( "github.com/apache/incubator-devlake/core/context" "github.com/apache/incubator-devlake/core/errors" "net/url" + "strings" ) type addPullRequestIdIndexToPullRequestCommits struct{} @@ -34,7 +35,8 @@ func (*addPullRequestIdIndexToPullRequestCommits) Up(basicRes context.BasicRes) if err1 != nil { return errors.Convert(err1) } - if u.Scheme == "mysql" { + switch strings.ToLower(u.Scheme) { + case "mysql": db := basicRes.GetDal() err := db.Exec("ALTER TABLE pull_request_commits DROP PRIMARY KEY;") if err != nil { @@ -44,8 +46,21 @@ func (*addPullRequestIdIndexToPullRequestCommits) Up(basicRes context.BasicRes) if err != nil { return err } + return nil + case "postgresql", "postgres", "pg": + db := basicRes.GetDal() + err := db.Exec("ALTER TABLE pull_request_commits DROP CONSTRAINT pull_request_commits_pkey;") + if err != nil { + return err + } + err = db.Exec("ALTER TABLE pull_request_commits ADD PRIMARY KEY (pull_request_id, commit_sha);") + if err != nil { + return err + } + return nil + default: + return nil } - return nil } func (*addPullRequestIdIndexToPullRequestCommits) Version() uint64 {