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

@@ -483,7 +483,16 @@ func purgeBucket(ctx context.Context, app *backpack.App) (*blob.Bucket, func(),
return b, func() { _ = b.Close() }, nil
}
// withDB runs fn on the app's published database when there is one: an
// in-process run (the scheduler in serve or schedule:run, through
// bonfire.Catalog.Call) borrows that pool and leaves it open. Otherwise it
// opens and publishes a pool for the CLI run and closes it when fn returns.
func withDB(ctx context.Context, app *backpack.App, fn func(*gorm.DB) error) error {
if app != nil {
if gdb, ok := app.Lookup[*gorm.DB](); ok && gdb != nil {
return fn(gdb)
}
}
sqlDB, gdb, err := OpenFromApp(ctx, app)
if err != nil {
return err