You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by kl...@apache.org on 2023/11/23 09:47:30 UTC

(incubator-devlake) branch main updated: fix(db): add missing primary keys (#6505)

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 d15f6bf1a fix(db): add missing primary keys (#6505)
d15f6bf1a is described below

commit d15f6bf1a81bca8dfc3a172c72f2ef40b9674db9
Author: Lynwee <li...@merico.dev>
AuthorDate: Thu Nov 23 17:47:25 2023 +0800

    fix(db): add missing primary keys (#6505)
---
 backend/core/models/locking.go                     |  2 +-
 ...add_primary_key_for_bitbucket_pipeline_steps.go | 70 ++++++++++++++++++++++
 .../bitbucket/models/migrationscripts/register.go  |  1 +
 .../20231123_recreate_teambition_connections.go    | 57 ++++++++++++++++++
 .../teambition/models/migrationscripts/register.go |  1 +
 backend/server/services/locking.go                 |  3 +-
 6 files changed, 132 insertions(+), 2 deletions(-)

diff --git a/backend/core/models/locking.go b/backend/core/models/locking.go
index 3228eced3..3bf02fc20 100644
--- a/backend/core/models/locking.go
+++ b/backend/core/models/locking.go
@@ -43,7 +43,7 @@ func (LockingHistory) TableName() string {
 
 // LockingStub does nothing but offer a locking target
 type LockingStub struct {
-	Stub string
+	Stub string `gorm:"primaryKey"`
 }
 
 func (LockingStub) TableName() string {
diff --git a/backend/plugins/bitbucket/models/migrationscripts/20231123_add_primary_key_for_bitbucket_pipeline_steps.go b/backend/plugins/bitbucket/models/migrationscripts/20231123_add_primary_key_for_bitbucket_pipeline_steps.go
new file mode 100644
index 000000000..14db9ac10
--- /dev/null
+++ b/backend/plugins/bitbucket/models/migrationscripts/20231123_add_primary_key_for_bitbucket_pipeline_steps.go
@@ -0,0 +1,70 @@
+/*
+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"
+	"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
+	"github.com/apache/incubator-devlake/core/plugin"
+	"time"
+)
+
+var _ plugin.MigrationScript = (*reCreatBitBucketPipelineSteps)(nil)
+
+type bitbucketPipelineStep20231123 struct {
+	ConnectionId      uint64 `gorm:"primaryKey"`
+	BitbucketId       string `gorm:"primaryKey"`
+	PipelineId        string `gorm:"type:varchar(255)"`
+	Name              string `gorm:"type:varchar(255)"`
+	Trigger           string `gorm:"type:varchar(255)"`
+	State             string `gorm:"type:varchar(255)"`
+	Result            string `gorm:"type:varchar(255)"`
+	RepoId            string `gorm:"type:varchar(255)"`
+	MaxTime           int
+	StartedOn         *time.Time
+	CompletedOn       *time.Time
+	DurationInSeconds int
+	BuildSecondsUsed  int
+	RunNumber         int
+	Type              string `gorm:"type:varchar(255)"`
+	Environment       string `gorm:"type:varchar(255)"`
+	archived.NoPKModel
+}
+
+func (bitbucketPipelineStep20231123) TableName() string {
+	return "_tool_bitbucket_pipeline_steps"
+}
+
+type reCreatBitBucketPipelineSteps struct{}
+
+func (script *reCreatBitBucketPipelineSteps) Up(basicRes context.BasicRes) errors.Error {
+	db := basicRes.GetDal()
+	if err := db.DropTables(bitbucketPipelineStep20231123{}.TableName()); err != nil {
+		return err
+	}
+	return db.AutoMigrate(&bitbucketPipelineStep20231123{})
+}
+
+func (*reCreatBitBucketPipelineSteps) Version() uint64 {
+	return 20231123160001
+}
+
+func (script *reCreatBitBucketPipelineSteps) Name() string {
+	return "re create _tool_bitbucket_pipeline_steps, make sure primary keys exist."
+}
diff --git a/backend/plugins/bitbucket/models/migrationscripts/register.go b/backend/plugins/bitbucket/models/migrationscripts/register.go
index 22c7aa7fc..8a5a7026e 100644
--- a/backend/plugins/bitbucket/models/migrationscripts/register.go
+++ b/backend/plugins/bitbucket/models/migrationscripts/register.go
@@ -39,5 +39,6 @@ func All() []plugin.MigrationScript {
 		new(renameTr2ScopeConfig),
 		new(addRawParamTableForScope),
 		new(addBuildNumberToPipelines),
+		new(reCreatBitBucketPipelineSteps),
 	}
 }
diff --git a/backend/plugins/teambition/models/migrationscripts/20231123_recreate_teambition_connections.go b/backend/plugins/teambition/models/migrationscripts/20231123_recreate_teambition_connections.go
new file mode 100644
index 000000000..ca36340c2
--- /dev/null
+++ b/backend/plugins/teambition/models/migrationscripts/20231123_recreate_teambition_connections.go
@@ -0,0 +1,57 @@
+/*
+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"
+	"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
+	"github.com/apache/incubator-devlake/core/plugin"
+)
+
+var _ plugin.MigrationScript = (*reCreateTeambitionConnections)(nil)
+
+type teambitionConnection20231123 struct {
+	Name string `gorm:"type:varchar(100);uniqueIndex" json:"name" validate:"required"`
+	archived.Model
+	archived.RestConnection `mapstructure:",squash"`
+	TenantId                string `mapstructure:"tenantId" validate:"required" json:"tenantId"`
+	TenantType              string `mapstructure:"tenantType" validate:"required" json:"tenantType"`
+}
+
+func (teambitionConnection20231123) TableName() string {
+	return "_tool_teambition_connections"
+}
+
+type reCreateTeambitionConnections struct{}
+
+func (*reCreateTeambitionConnections) Up(basicRes context.BasicRes) errors.Error {
+	db := basicRes.GetDal()
+	if err := db.DropTables(teambitionConnection20231123{}.TableName()); err != nil {
+		return err
+	}
+	return db.AutoMigrate(&teambitionConnection20231123{})
+}
+
+func (*reCreateTeambitionConnections) Version() uint64 {
+	return 20231123160000
+}
+
+func (*reCreateTeambitionConnections) Name() string {
+	return "recreate teambition connection table"
+}
diff --git a/backend/plugins/teambition/models/migrationscripts/register.go b/backend/plugins/teambition/models/migrationscripts/register.go
index a26815ea5..dc265c849 100644
--- a/backend/plugins/teambition/models/migrationscripts/register.go
+++ b/backend/plugins/teambition/models/migrationscripts/register.go
@@ -23,5 +23,6 @@ import "github.com/apache/incubator-devlake/core/plugin"
 func All() []plugin.MigrationScript {
 	return []plugin.MigrationScript{
 		new(addInitTables),
+		new(reCreateTeambitionConnections),
 	}
 }
diff --git a/backend/server/services/locking.go b/backend/server/services/locking.go
index 1adc48dd5..f663b0c62 100644
--- a/backend/server/services/locking.go
+++ b/backend/server/services/locking.go
@@ -44,11 +44,12 @@ func lockDatabase() {
 	}
 	errors.Must(db.Create(lockingHistory))
 	// 2. obtain the lock: using a never released transaction
-	// This prevent multiple devlake instances from sharing the same database by locking the migration history table
+	// This prevents multiple devlake instances from sharing the same database by locking the migration history table
 	// However, it would not work if any older devlake instances were already using the database.
 	lockingTx = db.Begin()
 	c := make(chan bool, 1)
 	go func() {
+		errors.Must(lockingTx.DropTables(models.LockingStub{}.TableName()))
 		errors.Must(lockingTx.AutoMigrate(&models.LockingStub{}))
 		errors.Must(lockingTx.LockTables(dal.LockTables{{Table: "_devlake_locking_stub", Exclusive: true}}))
 		lockingHistory.Succeeded = true