Skip to content

Commit 345c209

Browse files
kleshsstojak1Josip Stojak
authored
adding pull_request_id index to pull_request_commits/comments tables (#7559) (#7744)
* adding pull_request_id index to pull_request_commits/comments tables * change the pull_request_commits primary key columns order * adding Apache license header * only run for mysql * adding support for postgres --------- Co-authored-by: sstojak1 <18380216+sstojak1@users.noreply.github.com> Co-authored-by: Josip Stojak <Josip.Stojak@infobip.com>
1 parent 52433a8 commit 345c209

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package migrationscripts
19+
20+
import (
21+
"github.com/apache/incubator-devlake/core/context"
22+
"github.com/apache/incubator-devlake/core/errors"
23+
"github.com/apache/incubator-devlake/helpers/migrationhelper"
24+
)
25+
26+
type addPullRequestIdIndexToPullRequestComments struct{}
27+
28+
type pullRequestComments20240602 struct {
29+
PullRequestId string `gorm:"index"`
30+
}
31+
32+
func (pullRequestComments20240602) TableName() string {
33+
return "pull_request_comments"
34+
}
35+
36+
func (u *addPullRequestIdIndexToPullRequestComments) Up(basicRes context.BasicRes) errors.Error {
37+
return migrationhelper.AutoMigrateTables(basicRes, &pullRequestComments20240602{})
38+
}
39+
40+
func (*addPullRequestIdIndexToPullRequestComments) Version() uint64 {
41+
return 20240602103401
42+
}
43+
44+
func (*addPullRequestIdIndexToPullRequestComments) Name() string {
45+
return "add pull_request_id index for pull_request_comments"
46+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package migrationscripts
19+
20+
import (
21+
"github.com/apache/incubator-devlake/core/context"
22+
"github.com/apache/incubator-devlake/core/errors"
23+
"net/url"
24+
"strings"
25+
)
26+
27+
type addPullRequestIdIndexToPullRequestCommits struct{}
28+
29+
func (*addPullRequestIdIndexToPullRequestCommits) Up(basicRes context.BasicRes) errors.Error {
30+
dbUrl := basicRes.GetConfig("DB_URL")
31+
if dbUrl == "" {
32+
return errors.BadInput.New("DB_URL is required")
33+
}
34+
u, err1 := url.Parse(dbUrl)
35+
if err1 != nil {
36+
return errors.Convert(err1)
37+
}
38+
switch strings.ToLower(u.Scheme) {
39+
case "mysql":
40+
db := basicRes.GetDal()
41+
err := db.Exec("ALTER TABLE pull_request_commits DROP PRIMARY KEY;")
42+
if err != nil {
43+
return err
44+
}
45+
err = db.Exec("ALTER TABLE pull_request_commits ADD PRIMARY KEY (pull_request_id, commit_sha);")
46+
if err != nil {
47+
return err
48+
}
49+
return nil
50+
case "postgresql", "postgres", "pg":
51+
db := basicRes.GetDal()
52+
err := db.Exec("ALTER TABLE pull_request_commits DROP CONSTRAINT pull_request_commits_pkey;")
53+
if err != nil {
54+
return err
55+
}
56+
err = db.Exec("ALTER TABLE pull_request_commits ADD PRIMARY KEY (pull_request_id, commit_sha);")
57+
if err != nil {
58+
return err
59+
}
60+
return nil
61+
default:
62+
return nil
63+
}
64+
}
65+
66+
func (*addPullRequestIdIndexToPullRequestCommits) Version() uint64 {
67+
return 20240602103400
68+
}
69+
70+
func (*addPullRequestIdIndexToPullRequestCommits) Name() string {
71+
return "changing pull_request_commits primary key columns order"
72+
}

backend/core/models/migrationscripts/register.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,7 @@ func All() []plugin.MigrationScript {
123123
new(modifyPrAssigneeAndReviewerId),
124124
new(addChangesToPr),
125125
new(addMergedByToPr),
126+
new(addPullRequestIdIndexToPullRequestCommits),
127+
new(addPullRequestIdIndexToPullRequestComments),
126128
}
127129
}

0 commit comments

Comments
 (0)