mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	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>
This commit is contained in:
		@@ -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)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user