Fix startup deadlock with existing metrics database (#32043)

This commit is contained in:
andig 2026-07-22 11:56:07 +02:00 • committed by GitHub
parent 01ce52c203
commit 209b72b817
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 38 additions and 3 deletions

View file

@ -2,8 +2,10 @@ package db
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUnitNewDriver(t *testing.T) {
@ -63,3 +65,36 @@ func TestUnitNewDriver(t *testing.T) {
})
}
}
type migrationParent struct {
Id int `gorm:"column:id;primarykey"`
}
func (migrationParent) TableName() string { return "migration_parents" }
type migrationChild struct {
ParentId int `gorm:"column:parent_id"`
Parent migrationParent `gorm:"foreignkey:ParentId;references:Id"`
}
func (migrationChild) TableName() string { return "migration_children" }
// TestUnitMigrateConstraint guards against migrator implementations that pin a
// connection and then query the pool again: the single connection deadlocks.
func TestUnitMigrateConstraint(t *testing.T) {
db, err := New("sqlite", t.TempDir()+"/evcc.db")
require.NoError(t, err)
// existing table without the foreign key, forces the migrator to recreate it
require.NoError(t, db.Exec("CREATE TABLE migration_children (parent_id integer)").Error)
done := make(chan error, 1)
go func() { done <- db.AutoMigrate(new(migrationParent), new(migrationChild)) }()
select {
case err := <-done:
require.NoError(t, err)
case <-time.After(10 * time.Second):
t.Fatal("AutoMigrate deadlocked")
}
}