aboutsummaryrefslogtreecommitdiffstats
path: root/models/db
diff options
context:
space:
mode:
authorJason Song <i@wolfogre.com>2022-12-23 19:35:43 +0800
committerGitHub <noreply@github.com>2022-12-23 19:35:43 +0800
commit71ca3067bcc6c7c7772d38fc7590505c8c7148ed (patch)
tree9c3719cb257e3976df229128f9f9d33056e3503d /models/db
parent41f0668da818d3a3ae74555bfe3de375448bacf3 (diff)
downloadgitea-71ca3067bcc6c7c7772d38fc7590505c8c7148ed.tar.gz
gitea-71ca3067bcc6c7c7772d38fc7590505c8c7148ed.zip
Check primary keys for all tables and drop ForeignReference (#21721)
Some dbs require that all tables have primary keys, see - #16802 - #21086 We can add a test to keep it from being broken again. Edit: ~Added missing primary key for `ForeignReference`~ Dropped the `ForeignReference` table to satisfy the check, so it closes #21086. More context can be found in comments. Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models/db')
-rw-r--r--models/db/engine_test.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/models/db/engine_test.go b/models/db/engine_test.go
index fa1ac08a17..5514366777 100644
--- a/models/db/engine_test.go
+++ b/models/db/engine_test.go
@@ -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"
)
@@ -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)
+ }
+ }
+}