check / check (push) Waiting to run
The main database, each webhook's event database and each archive file get their schema from SQL files under internal/db/migrations/, one directory per kind. Opening a file runs, in one transaction, each file that schema_migrations does not list. GORM stays for queries. AutoMigrate is gone, tests included. An event database holds only its own tables now, so the sweep that removed target rows copied into it is deleted. The association guard stays: without it a delivery write fails on the missing targets table. AutoMigrate also had GORM parse every model before a handle was shared. That is now done on its own: GORM races when two goroutines first use models that refer to each other. Model: opus-5-5
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package database_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"sneak.berlin/go/webhooker/internal/database"
|
|
"sneak.berlin/go/webhooker/internal/database/databasetest"
|
|
)
|
|
|
|
// TestEventDBCreateOmitsAssociations covers the connection-level
|
|
// guard directly: a Delivery carrying its Event and Target in
|
|
// memory, written through the manager's handle, must store only the
|
|
// delivery row.
|
|
func TestEventDBCreateOmitsAssociations(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
webhookID := uuid.New().String()
|
|
|
|
mgr := databasetest.NewWebhookDBManager(t, t.TempDir())
|
|
|
|
db, err := mgr.GetDB(webhookID)
|
|
require.NoError(t, err)
|
|
|
|
target := database.Target{
|
|
WebhookID: webhookID,
|
|
Name: "leaky-target",
|
|
Type: database.TargetTypeSlack,
|
|
Config: `{"webhookUrl":"https://hooks.example/secret"}`,
|
|
}
|
|
target.ID = uuid.New().String()
|
|
|
|
event := database.Event{
|
|
WebhookID: webhookID,
|
|
EntrypointID: uuid.New().String(),
|
|
Method: "POST",
|
|
Headers: `{}`,
|
|
Body: `{}`,
|
|
}
|
|
event.ID = uuid.New().String()
|
|
|
|
d := &database.Delivery{
|
|
EventID: event.ID,
|
|
TargetID: target.ID,
|
|
Status: database.DeliveryStatusPending,
|
|
Event: event,
|
|
Target: target,
|
|
}
|
|
d.ID = uuid.New().String()
|
|
|
|
require.NoError(t, db.Create(d).Error)
|
|
require.NoError(t, db.Model(d).
|
|
Update("status", database.DeliveryStatusDelivered).
|
|
Error)
|
|
|
|
// The file has no targets table, so the create and update above
|
|
// would have failed had GORM tried to save the target.
|
|
assert.False(t, db.Migrator().HasTable(&database.Target{}))
|
|
require.NoError(t, mgr.CloseAll())
|
|
}
|