fix(lagoon): run deferred:purge and the migrate commands on the app's published database (quick-261106-kt2)

The nightly framework entry deferred:purge failed under serve's worker and
schedule:run with "backpack: duplicate provider for *sql.DB": withDB always
opened and published a second pool. It now reuses the published *gorm.DB and
leaves that pool open; the CLI path (nothing published) is unchanged.
This commit is contained in:
Jakub Zych
2026-11-06 15:23:28 +01:00
parent 8ebfedf64c
commit 16a20088a3
3 changed files with 94 additions and 0 deletions

View File

@@ -254,6 +254,44 @@ func TestScheduleRunsCommand(t *testing.T) {
logs.waitFor(t, slog.LevelWarn, "schedule: command not registered; skipping", "command", "acme:missing")
}
// TestScheduledDeferredPurgeInProcess runs the framework entry deferred:purge
// the way serve's worker does (callScheduled through the published catalog)
// and through schedule:run --once, both on an app whose pool is already
// published. Both used to fail with "backpack: duplicate provider for
// *sql.DB".
func TestScheduledDeferredPurgeInProcess(t *testing.T) {
db, dsn := migratedDB(t)
app, _ := testApp(t, db, dsn, map[string]any{"app.key": testAppKey})
logs := &captureHandler{}
if err := app.Publish(slog.New(logs)); err != nil {
t.Fatal(err)
}
if err := app.Publish(bonfire.NewCatalog(lagoon.RuntimeCommands(app, nil))); err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
skipped, err := callScheduled(t.Context(), app, loggerFromApp(app), "deferred:purge", nil, &buf)
if skipped || err != nil {
t.Fatalf("callScheduled: skipped=%v err=%v\n%s", skipped, err, buf.String())
}
if !strings.Contains(buf.String(), "purged 0 deferred bindings older than 5 days") {
t.Fatalf("output = %q", buf.String())
}
if !logs.find(slog.LevelInfo, "schedule: command finished", "command", "deferred:purge") {
t.Fatal("no Info 'schedule: command finished' record for deferred:purge")
}
if err := db.PingContext(t.Context()); err != nil {
t.Fatalf("published pool closed by the command: %v", err)
}
out, err := onceRun(t, app, nil, mustTime(t, time.UTC, "2026-03-10 03:00:00"))
if err != nil {
t.Fatalf("schedule:run --once: %v\n%s", err, out)
}
if !strings.Contains(out, "Ran 1 due entries (0 failed)") {
t.Fatalf("schedule:run --once output = %q", out)
}
}
// onceRun runs `schedule:run --once` for plugins at the given app time and
// returns its output.
func onceRun(t *testing.T, app *backpack.App, plugins []party.Plugin, now time.Time) (string, error) {