Skip to content

Commit

Permalink
add primary key to foreignreference
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Dec 4, 2022
1 parent 8b39bad commit 961c87a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions models/foreignreference/foreignreference.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (

// ForeignReference represents external references
type ForeignReference struct {
ID int64 `xorm:"pk autoincr"`

// RepoID is the first column in all indices. now we only need 2 indices: (repo, local) and (repo, foreign, type)
RepoID int64 `xorm:"UNIQUE(repo_foreign_type) INDEX(repo_local)" `
LocalIndex int64 `xorm:"INDEX(repo_local)"` // the resource key inside Gitea, it can be IssueIndex, or some model ID.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-
repo_id: 1
local_index: 1
foreign_index: "foo"
type: "foo"
-
repo_id: 1
local_index: 2
foreign_index: "bar"
type: "foo"
-
repo_id: 2
local_index: 2
foreign_index: "foo"
type: "foo"
-
repo_id: 3
local_index: 1024
foreign_index: "1"
type: "normal"
-
repo_id: 2
local_index: 1
foreign_index: "bar"
type: "foo"
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,8 @@ var migrations = []Migration{
NewMigration("Add package cleanup rule table", v1_19.CreatePackageCleanupRuleTable),
// v235 -> v236
NewMigration("Add index for access_token", v1_19.AddIndexForAccessToken),
// v236 -> v237
NewMigration("Add primary key to foreign reference", v1_19.AddPrimaryKeyToForeignReference),
}

// GetCurrentDBVersion returns the current db version
Expand Down
21 changes: 21 additions & 0 deletions models/migrations/v1_19/v236.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2022 Gitea. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_19 //nolint

import "xorm.io/xorm"

func AddPrimaryKeyToForeignReference(x *xorm.Engine) error {
// ForeignReference represents external references
type ForeignReference struct {
ID int64 `xorm:"pk autoincr"`

// RepoID is the first column in all indices. now we only need 2 indices: (repo, local) and (repo, foreign, type)
RepoID int64 `xorm:"UNIQUE(repo_foreign_type) INDEX(repo_local)" `
LocalIndex int64 `xorm:"INDEX(repo_local)"` // the resource key inside Gitea, it can be IssueIndex, or some model ID.
ForeignIndex string `xorm:"INDEX UNIQUE(repo_foreign_type)"`
Type string `xorm:"VARCHAR(16) INDEX UNIQUE(repo_foreign_type)"`
}

return x.Sync(new(ForeignReference))
}
34 changes: 34 additions & 0 deletions models/migrations/v1_19/v236_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_19 //nolint

import (
"testing"

"code.gitea.io/gitea/models/migrations/base"
"github.com/stretchr/testify/assert"
)

func Test_AddPrimaryKeyToForeignReference(t *testing.T) {
// ForeignReference represents external references
type ForeignReference struct {
// RepoID is the first column in all indices. now we only need 2 indices: (repo, local) and (repo, foreign, type)
RepoID int64 `xorm:"UNIQUE(repo_foreign_type) INDEX(repo_local)" `
LocalIndex int64 `xorm:"INDEX(repo_local)"` // the resource key inside Gitea, it can be IssueIndex, or some model ID.
ForeignIndex string `xorm:"INDEX UNIQUE(repo_foreign_type)"`
Type string `xorm:"VARCHAR(16) INDEX UNIQUE(repo_foreign_type)"`
}

// Prepare and load the testing database
x, deferable := base.PrepareTestEnv(t, 0, new(ForeignReference))
defer deferable()
if x == nil || t.Failed() {
return
}

if err := AddPrimaryKeyToForeignReference(x); err != nil {
assert.NoError(t, err)
return
}
}

0 comments on commit 961c87a

Please sign in to comment.