Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check primary keys for all tables and drop ForeignReference #21721

Merged
merged 18 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions models/db/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/setting"

_ "code.gitea.io/gitea/cmd" // for TestPrimaryKeys

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -51,3 +53,34 @@ func TestDeleteOrphanedObjects(t *testing.T) {
assert.NoError(t, err)
assert.EqualValues(t, countBefore, countAfter)
}

func TestPrimaryKeys(t *testing.T) {
// Some dbs require that all tables have primary keys, see
// https://github.com/go-gitea/gitea/issues/21086
// https://github.com/go-gitea/gitea/issues/16802
// To avoid creating tables without primary key again, this test will check them.
// Import "code.gitea.io/gitea/cmd" to make sure each db.RegisterModel in init functions has been called.

beans, err := db.NamesToBean()
if err != nil {
t.Fatal(err)
}

whitelist := map[string]string{
"the_table_name_to_skip_checking": "Write a note here to explain why",
}

for _, bean := range beans {
table, err := db.TableInfo(bean)
if err != nil {
t.Fatal(err)
}
if why, ok := whitelist[table.Name]; ok {
t.Logf("ignore %q because %q", table.Name, why)
continue
}
if len(table.PrimaryKeys) == 0 {
t.Errorf("table %q has no primary key", table.Name)
}
}
}
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
}
}