Skip to content

Check primary keys for all tables and drop ForeignReference #21721

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

Merged
merged 18 commits into from
Dec 23, 2022
Merged
Changes from 1 commit
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
Next Next commit
tests: add TestPrimaryKeys
  • Loading branch information
wolfogre committed Nov 8, 2022
commit 59adfd6c815230e3cb5a42d9acc501a68643097c
32 changes: 32 additions & 0 deletions models/db/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"testing"

_ "code.gitea.io/gitea/cmd" // for TestPrimaryKeys
"code.gitea.io/gitea/models/db"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/unittest"
Expand Down Expand Up @@ -52,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)
}
}
}