From 957d1173da2dd5d859068bbe07f5bcf4dd8da566 Mon Sep 17 00:00:00 2001 From: abeizn Date: Wed, 5 Jun 2024 10:31:27 +0800 Subject: [PATCH] fix: add primary key to _tool_gitlab_assignees and _tool_gitlab_reviewers tables --- backend/plugins/gitlab/models/assignee.go | 2 +- ..._mr_assignees_and_reviewers_primary_key.go | 88 +++++++++++++++++++ .../models/migrationscripts/register.go | 1 + backend/plugins/gitlab/models/reviewer.go | 2 +- 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 backend/plugins/gitlab/models/migrationscripts/20240605_add_mr_assignees_and_reviewers_primary_key.go diff --git a/backend/plugins/gitlab/models/assignee.go b/backend/plugins/gitlab/models/assignee.go index 85f3b377b6f..0fa49606413 100644 --- a/backend/plugins/gitlab/models/assignee.go +++ b/backend/plugins/gitlab/models/assignee.go @@ -24,7 +24,7 @@ import ( type GitlabAssignee struct { ConnectionId uint64 `gorm:"primaryKey"` AssigneeId int `gorm:"primaryKey"` - MergeRequestId int `gorm:"index"` + MergeRequestId int `gorm:"primaryKey"` ProjectId int `gorm:"index"` Name string `gorm:"type:varchar(255)"` Username string `gorm:"type:varchar(255)"` diff --git a/backend/plugins/gitlab/models/migrationscripts/20240605_add_mr_assignees_and_reviewers_primary_key.go b/backend/plugins/gitlab/models/migrationscripts/20240605_add_mr_assignees_and_reviewers_primary_key.go new file mode 100644 index 00000000000..ef37640d246 --- /dev/null +++ b/backend/plugins/gitlab/models/migrationscripts/20240605_add_mr_assignees_and_reviewers_primary_key.go @@ -0,0 +1,88 @@ +/* +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/errors" + archivedCore "github.com/apache/incubator-devlake/core/models/migrationscripts/archived" + "github.com/apache/incubator-devlake/helpers/migrationhelper" + "github.com/apache/incubator-devlake/plugins/gitlab/models/migrationscripts/archived" +) + +type addGitlabAssigneeAndReviewerPrimaryKey struct{} + +type GitlabAssignee20240605 struct { + ConnectionId uint64 `gorm:"primaryKey"` + AssigneeId int `gorm:"primaryKey"` + MergeRequestId int `gorm:"primaryKey"` + ProjectId int `gorm:"index"` + Name string `gorm:"type:varchar(255)"` + Username string `gorm:"type:varchar(255)"` + State string `gorm:"type:varchar(255)"` + AvatarUrl string `gorm:"type:varchar(255)"` + WebUrl string `gorm:"type:varchar(255)"` + archivedCore.NoPKModel +} + +func (GitlabAssignee20240605) TableName() string { + return "_tool_gitlab_assignees" +} + +type GitlabReviewer20240605 struct { + ConnectionId uint64 `gorm:"primaryKey"` + ReviewerId int `gorm:"primaryKey"` + MergeRequestId int `gorm:"primaryKey"` + ProjectId int `gorm:"index"` + Name string `gorm:"type:varchar(255)"` + Username string `gorm:"type:varchar(255)"` + State string `gorm:"type:varchar(255)"` + AvatarUrl string `gorm:"type:varchar(255)"` + WebUrl string `gorm:"type:varchar(255)"` + archivedCore.NoPKModel +} + +func (GitlabReviewer20240605) TableName() string { + return "_tool_gitlab_reviewers" +} + +func (*addGitlabAssigneeAndReviewerPrimaryKey) Up(baseRes context.BasicRes) errors.Error { + err := baseRes.GetDal().DropTables(archived.GitlabAssignee{}, archived.GitlabReviewer{}) + if err != nil { + return err + } + + err = migrationhelper.AutoMigrateTables( + baseRes, + &GitlabAssignee20240605{}, + &GitlabReviewer20240605{}, + ) + if err != nil { + return err + } + + return nil +} + +func (*addGitlabAssigneeAndReviewerPrimaryKey) Version() uint64 { + return 20240605110339 +} + +func (*addGitlabAssigneeAndReviewerPrimaryKey) Name() string { + return "add primary key to _tool_gitlab_assignees and _tool_gitlab_reviewers tables" +} diff --git a/backend/plugins/gitlab/models/migrationscripts/register.go b/backend/plugins/gitlab/models/migrationscripts/register.go index b60a41e4e31..24af299b0fb 100644 --- a/backend/plugins/gitlab/models/migrationscripts/register.go +++ b/backend/plugins/gitlab/models/migrationscripts/register.go @@ -49,5 +49,6 @@ func All() []plugin.MigrationScript { new(modifyDeploymentCommitTitle), new(addWebUrlToGitlabPipelineProject), new(addGitlabAssignee), + new(addGitlabAssigneeAndReviewerPrimaryKey), } } diff --git a/backend/plugins/gitlab/models/reviewer.go b/backend/plugins/gitlab/models/reviewer.go index 8bffc2f54fc..3acef38afb7 100644 --- a/backend/plugins/gitlab/models/reviewer.go +++ b/backend/plugins/gitlab/models/reviewer.go @@ -24,7 +24,7 @@ import ( type GitlabReviewer struct { ConnectionId uint64 `gorm:"primaryKey"` ReviewerId int `gorm:"primaryKey"` - MergeRequestId int `gorm:"index"` + MergeRequestId int `gorm:"primaryKey"` ProjectId int `gorm:"index"` Name string `gorm:"type:varchar(255)"` Username string `gorm:"type:varchar(255)"`