-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Andrew Thornton <art27@cantab.net>
- Loading branch information
Showing
5 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
models/migrations/fixtures/Test_AddPrimaryKeyToForeignReference/foreign_reference.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |