carlos / platform Public

console: request context through the app list, app page and instances

The busiest signed-in pages, judged from the routes: the account home
(GET /a/{sqid}/, the app list), an app's overview (GET .../apps/{app})
and its instances list (GET .../apps/{app}/instances), with appPage, the
frame every app tab shares. Their console-store reads now run under the
request's context:

- the app list: AppsInContext, linkedAppHomes, TrashedAppsIn, the stars
  and lifetimes (fillAppLists over AppLivesIn, StarredLifetimes) and the
  Getting started card (CLITokensContext, GettingStartedMarks,
  CompleteGettingStarted);
- region labels on all three: regionFactsFor, appRegion
  (AppRegionContext) and appPlacementRow; appPlacementContext for the
  instances page's region note (appPlacement is its wrapper, and the
  placement census names appPlacementContext as the caller);
- appPage: hasClaimedSource, AppLifeContext, linkedAppPage, and the
  account review (appAccountReview, routeAccountFor, over
  HasTrashedAppInContext);
- the overview: EventsContext and the pending card. That one had a
  ...Context method whose body dropped the context:
  PendingChangesForContext now hands it to its transaction (BeginTx,
  QueryContext, confirmsFor) and pendingTally to MemberConfirms.

Callers of these with no context yet (AppPlacement, fleetPlacedAppsIn,
LiveTemporaryApps, PendingChange, ConfirmChange) pass
context.Background() with a line saying so.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 4f056ed1c18a48a61eb56b27c96bd602c3ee5095 parent 39fc889
44 files changed, +205 −150
  • internal/console/accountlinks.go +3 −3
  • internal/console/accountreview.go +5 −4
  • internal/console/accountreview_test.go +7 −7
  • internal/console/apex.go +1 −1
  • internal/console/apicli_test.go +2 −2
  • internal/console/apiinstances.go +1 −1
  • internal/console/apitemporary.go +2 −2
  • internal/console/app_source.go +2 −2
  • internal/console/applist.go +5 −5
  • internal/console/applist_test.go +8 −7
  • internal/console/appregionset.go +1 −1
  • internal/console/appsettings.go +4 −4
  • internal/console/appstarcleanup_test.go +5 −4
  • internal/console/authstore.go +9 −2
  • internal/console/claim_invariant_probes_nonapp_test.go +2 −2
  • internal/console/deletion_test.go +1 −1
  • internal/console/demo_claim_page.go +1 −1
  • internal/console/economicspass.go +1 −1
  • internal/console/fleets.go +9 −1
  • internal/console/fleetsecretskey.go +2 −1
  • internal/console/gettingstarted.go +5 −4
  • internal/console/gettingstarted_handlers_test.go +2 −1
  • internal/console/gettingstarted_page_test.go +2 −2
  • internal/console/gettingstarted_store.go +6 −5
  • internal/console/gettingstarted_store_test.go +13 −12
  • internal/console/links_auth_test.go +1 −1
  • internal/console/links_store.go +2 −2
  • internal/console/materialize_test.go +2 −2
  • internal/console/mobilenav.go +1 −1
  • internal/console/ownership.go +1 −1
  • internal/console/pending.go +13 −11
  • internal/console/pendinghandlers.go +5 −5
  • internal/console/pipelinepage.go +2 −2
  • internal/console/placement_census_test.go +1 −1
  • internal/console/regioncandidates.go +1 −1
  • internal/console/regionconfig.go +5 −4
  • internal/console/regionconfig_test.go +3 −2
  • internal/console/regiondefault.go +1 −1
  • internal/console/regionfacts.go +4 −3
  • internal/console/regionfacts_test.go +1 −1
  • internal/console/server.go +14 −14
  • internal/console/store.go +40 −14
  • internal/console/store_test.go +3 −3
  • internal/console/tempstore.go +6 −5

CI cancelled — superseded by a newer push — run details

diff --git a/internal/console/accountlinks.go b/internal/console/accountlinks.go
index aa5051133..965ff2176 100644
--- a/internal/console/accountlinks.go
+++ b/internal/console/accountlinks.go
@@ -231,8 +231,8 @@ type linkedAppView struct {
// linkedAppPage is the linked-app view of app in acct for its pages' nav,
// or nil for an ordinary app. A failed read is logged and costs only the
// linked view: every refused tab still refuses (linkedAppRefusal).
-func (s *Server) linkedAppPage(acct *Account, app string) *linkedAppView {
- linkID, err := s.store.appLinkID(acct.ID, app)
+func (s *Server) linkedAppPage(ctx context.Context, acct *Account, app string) *linkedAppView {
+ linkID, err := s.store.appLinkIDContext(ctx, acct.ID, app)
if err != nil {
log.Printf("console: linked app page %s/%s: %v", acct.Sqid, app, err)
return nil
@@ -241,7 +241,7 @@ func (s *Server) linkedAppPage(acct *Account, app string) *linkedAppView {
return nil
}
v := &linkedAppView{LinkID: linkID, Home: linkID}
- if l, err := s.store.LinkByID(linkID); err != nil {
+ if l, err := s.store.LinkByIDContext(ctx, linkID); err != nil {
log.Printf("console: linked app page %s/%s: link %s: %v", acct.Sqid, app, linkID, err)
} else if l != nil && l.Name != "" {
v.Home = l.Name
diff --git a/internal/console/accountreview.go b/internal/console/accountreview.go
index 81af04eac..318a92e37 100644
--- a/internal/console/accountreview.go
+++ b/internal/console/accountreview.go
@@ -1,6 +1,7 @@
package console
import (
+ "context"
"net/http"
"sort"
"strconv"
@@ -29,7 +30,7 @@ func (a AccountReview) SummaryText() string {
// appAccountReview returns only candidates whose source account the viewer
// can access. A source with its own same-named app is a legitimate sibling,
// not evidence that the viewed app is half-migrated.
-func (s *Server) appAccountReview(address string, viewed *Account, app string) (AccountReview, bool, error) {
+func (s *Server) appAccountReview(ctx context.Context, address string, viewed *Account, app string) (AccountReview, bool, error) {
reg, err := openRegistry(s.cfg.Registry)
if err != nil {
return AccountReview{}, false, err
@@ -38,7 +39,7 @@ func (s *Server) appAccountReview(address string, viewed *Account, app string) (
if err != nil {
return AccountReview{}, false, err
}
- accounts, err := s.store.AccountsFor(address)
+ accounts, err := s.store.AccountsForContext(ctx, address)
if err != nil {
return AccountReview{}, false, err
}
@@ -62,12 +63,12 @@ func (s *Server) appAccountReview(address string, viewed *Account, app string) (
hidden = true
continue
}
- has, err := s.store.HasAppIn(account.ID, app)
+ has, err := s.store.HasAppInContext(ctx, account.ID, app)
if err != nil {
return AccountReview{}, false, err
}
if !has {
- has, err = s.store.HasTrashedAppIn(account.ID, app)
+ has, err = s.store.HasTrashedAppInContext(ctx, account.ID, app)
if err != nil {
return AccountReview{}, false, err
}
diff --git a/internal/console/accountreview_test.go b/internal/console/accountreview_test.go
index 74d80d6f2..e6d69d8d9 100644
--- a/internal/console/accountreview_test.go
+++ b/internal/console/accountreview_test.go
@@ -30,7 +30,7 @@ func TestAccountReviewExcludesLegitimateSameNamedApp(t *testing.T) {
if err != nil || viewed == nil {
t.Fatal(err)
}
- review, all, err := fx.srv.appAccountReview("paul@keymail.dev", viewed, "kass")
+ review, all, err := fx.srv.appAccountReview(context.Background(), "paul@keymail.dev", viewed, "kass")
if err != nil {
t.Fatal(err)
}
@@ -63,14 +63,14 @@ func TestAccountReviewSiblingPreventsUnrelatedMove(t *testing.T) {
fx.addRoute(t, registry.Route{Host: "sibling.example", App: "kass", Account: "cdef", Kind: "instance"})
fx.addRoute(t, registry.Route{Host: "candidate.example", App: "kass", Account: "cghi", Kind: "instance"})
viewed, _ := fx.st.AccountBySqid("bab")
- review, all, err := fx.srv.appAccountReview("paul@keymail.dev", viewed, "kass")
+ review, all, err := fx.srv.appAccountReview(context.Background(), "paul@keymail.dev", viewed, "kass")
if err != nil {
t.Fatal(err)
}
if review.Count != 1 || all {
t.Fatalf("review = %+v all=%v; sibling must prevent move authority", review, all)
}
- if _, agreed, err := fx.srv.routeAccountFor("paul@keymail.dev", viewed, "kass"); !errors.Is(err, errMoveTargetUnavailable) || agreed {
+ if _, agreed, err := fx.srv.routeAccountFor(context.Background(), "paul@keymail.dev", viewed, "kass"); !errors.Is(err, errMoveTargetUnavailable) || agreed {
t.Fatalf("routeAccountFor agreed=%v err=%v; same-named sibling must hide the move target", agreed, err)
}
}
@@ -79,14 +79,14 @@ func TestAccountReviewHidesInaccessibleSource(t *testing.T) {
fx := newConsoleFixture(t)
fx.addRoute(t, registry.Route{Host: "secret.example", App: "kass", Account: "unknown", Kind: "instance"})
viewed, _ := fx.st.AccountBySqid("bab")
- review, all, err := fx.srv.appAccountReview("paul@keymail.dev", viewed, "kass")
+ review, all, err := fx.srv.appAccountReview(context.Background(), "paul@keymail.dev", viewed, "kass")
if err != nil {
t.Fatal(err)
}
if review.Count != 0 || all {
t.Fatalf("inaccessible source disclosed: %+v all=%v", review, all)
}
- if _, agreed, err := fx.srv.routeAccountFor("paul@keymail.dev", viewed, "kass"); !errors.Is(err, errMoveTargetUnavailable) || agreed {
+ if _, agreed, err := fx.srv.routeAccountFor(context.Background(), "paul@keymail.dev", viewed, "kass"); !errors.Is(err, errMoveTargetUnavailable) || agreed {
t.Fatalf("routeAccountFor agreed=%v err=%v; an inaccessible source must hide the move target", agreed, err)
}
}
@@ -105,14 +105,14 @@ func TestAccountReviewTreatsTrashedSameNamedAppAsSibling(t *testing.T) {
}
fx.addRoute(t, registry.Route{Host: "trashed-sibling.example", App: "kass", Account: "cdef", Kind: "instance"})
viewed, _ := fx.st.AccountBySqid("bab")
- review, all, err := fx.srv.appAccountReview("paul@keymail.dev", viewed, "kass")
+ review, all, err := fx.srv.appAccountReview(context.Background(), "paul@keymail.dev", viewed, "kass")
if err != nil {
t.Fatal(err)
}
if review.Count != 0 || all {
t.Fatalf("trashed sibling became a move candidate: %+v all=%v", review, all)
}
- if _, agreed, err := fx.srv.routeAccountFor("paul@keymail.dev", viewed, "kass"); !errors.Is(err, errMoveTargetUnavailable) || agreed {
+ if _, agreed, err := fx.srv.routeAccountFor(context.Background(), "paul@keymail.dev", viewed, "kass"); !errors.Is(err, errMoveTargetUnavailable) || agreed {
t.Fatalf("routeAccountFor agreed=%v err=%v; trashed sibling must hide the move target", agreed, err)
}
}
diff --git a/internal/console/apex.go b/internal/console/apex.go
index b32abfcf7..8e21836e1 100644
--- a/internal/console/apex.go
+++ b/internal/console/apex.go
@@ -133,7 +133,7 @@ func (s *Server) hostHolder(ctx context.Context, acct *Account, host, own string
if err != nil {
return nil, &apexErr{err: err}
}
- trashed, err := s.store.TrashedAppsIn(acct.ID)
+ trashed, err := s.store.TrashedAppsIn(ctx, acct.ID)
if err != nil {
return nil, &apexErr{err: err}
}
diff --git a/internal/console/apicli_test.go b/internal/console/apicli_test.go
index ced8f9f48..e3da6f3c4 100644
--- a/internal/console/apicli_test.go
+++ b/internal/console/apicli_test.go
@@ -2190,7 +2190,7 @@ func TestAPITrashAndRestoreApp(t *testing.T) {
// One clock, and it is the MARKER's: the auto-purge pass measures the 30
// days from there, so a row stamped with anything else would give the
// Trash page a "days remaining" nothing honours.
- trashed, err := fx.st.TrashedAppsIn(OpsAccountID)
+ trashed, err := fx.st.TrashedAppsIn(context.Background(), OpsAccountID)
if err != nil || len(trashed) != 1 || trashed[0].App != "titogo" {
t.Fatalf("TrashedAppsIn = %+v %v, want exactly titogo", trashed, err)
}
@@ -2357,7 +2357,7 @@ func TestAPITrashUnconfirmedWindowIsRefused(t *testing.T) {
if want := old.Add(release.TrashCooloff); until.Sub(want) > time.Minute || want.Sub(until) > time.Minute {
t.Fatalf("restorable_until = %s, want the ORIGINAL marker's window ~%s", until, want)
}
- trashed, err := fx.st.TrashedAppsIn(OpsAccountID)
+ trashed, err := fx.st.TrashedAppsIn(context.Background(), OpsAccountID)
if err != nil || len(trashed) != 1 {
t.Fatalf("TrashedAppsIn = %+v %v, want exactly titogo", trashed, err)
}
diff --git a/internal/console/apiinstances.go b/internal/console/apiinstances.go
index 11acdc052..552fca003 100644
--- a/internal/console/apiinstances.go
+++ b/internal/console/apiinstances.go
@@ -1938,7 +1938,7 @@ func (s *Server) handleAPIInstanceCreate(w http.ResponseWriter, r *http.Request)
raw = restoreRegion
} else if raw == "" && placed == "" && strings.TrimSpace(req.Role) == "" &&
(provision.Record{Kind: strings.TrimSpace(req.Kind), Addrs: req.Addrs}).DerivesPaths() {
- appRegion, _, err := s.appRegion(acct.ID, app)
+ appRegion, _, err := s.appRegion(r.Context(), acct.ID, app)
if err != nil {
log.Printf("console: api instances create %s/%s: read app region: %v", acct.Sqid, app, err)
jsonError(w, http.StatusBadGateway, msgAppRegionUnreadable)
diff --git a/internal/console/apitemporary.go b/internal/console/apitemporary.go
index c7b5ee775..7bccb1544 100644
--- a/internal/console/apitemporary.go
+++ b/internal/console/apitemporary.go
@@ -393,7 +393,7 @@ func (s *Server) handleAPIAppList(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
- lives, err := s.store.AppLivesIn(acct.ID)
+ lives, err := s.store.AppLivesIn(r.Context(), acct.ID)
if err != nil {
log.Printf("console: api list %s: %v", acct.Sqid, err)
jsonError(w, http.StatusInternalServerError, "could not load apps")
@@ -419,7 +419,7 @@ func (s *Server) handleAPIAppList(w http.ResponseWriter, r *http.Request) {
// SQLite and the one registry listing above. region_name is always
// sent by this console, "" when the placement could not be read, so
// a CLI can tell an unknown from an older console.
- f := s.regionFactsFor(acct, l.App, fleetFromRoutes(routes, acct.Sqid, l.App), routesOK)
+ f := s.regionFactsFor(r.Context(), acct, l.App, fleetFromRoutes(routes, acct.Sqid, l.App), routesOK)
entry["region_name"] = f.Text()
if f.Known {
entry["region"] = f.Code()
diff --git a/internal/console/app_source.go b/internal/console/app_source.go
index 96c264284..b6e681ecc 100644
--- a/internal/console/app_source.go
+++ b/internal/console/app_source.go
@@ -284,8 +284,8 @@ func (s *Server) serveAppSource(w http.ResponseWriter, r *http.Request, actor st
_, _ = w.Write(b)
}
-func (s *Store) hasClaimedSource(accountID int64, app string) (bool, error) {
+func (s *Store) hasClaimedSource(ctx context.Context, accountID int64, app string) (bool, error) {
var exists bool
- err := s.db.QueryRow(`SELECT claimed_source!='' FROM apps WHERE account_id=? AND app=?`, accountID, app).Scan(&exists)
+ err := s.db.QueryRowContext(ctx, `SELECT claimed_source!='' FROM apps WHERE account_id=? AND app=?`, accountID, app).Scan(&exists)
return exists, err
}
diff --git a/internal/console/applist.go b/internal/console/applist.go
index 5a87df44b..0d9c086fa 100644
--- a/internal/console/applist.go
+++ b/internal/console/applist.go
@@ -71,8 +71,8 @@ ON CONFLICT (account_id, address, lifetime) DO NOTHING`, accountID, address, lif
// StarredLifetimes is the set of app lifetimes address has starred in the
// account.
-func (s *Store) StarredLifetimes(accountID int64, address string) (map[string]bool, error) {
- rows, err := s.db.Query(`SELECT lifetime FROM app_stars WHERE account_id = ? AND address = ?`, accountID, address)
+func (s *Store) StarredLifetimes(ctx context.Context, accountID int64, address string) (map[string]bool, error) {
+ rows, err := s.db.QueryContext(ctx, `SELECT lifetime FROM app_stars WHERE account_id = ? AND address = ?`, accountID, address)
if err != nil {
return nil, err
}
@@ -109,12 +109,12 @@ func partitionApps(rows []appRow) (starred, regular, temporary []appRow) {
// app's expiry, then fills the page's three lists from the rows q keeps.
// p.Apps stays every live app, so the page can tell "no apps" from "no
// matches".
-func (s *Server) fillAppLists(p *page, acct *Account, addr, q string) error {
- lives, err := s.store.AppLivesIn(acct.ID)
+func (s *Server) fillAppLists(ctx context.Context, p *page, acct *Account, addr, q string) error {
+ lives, err := s.store.AppLivesIn(ctx, acct.ID)
if err != nil {
return err
}
- stars, err := s.store.StarredLifetimes(acct.ID, addr)
+ stars, err := s.store.StarredLifetimes(ctx, acct.ID, addr)
if err != nil {
return err
}
diff --git a/internal/console/applist_test.go b/internal/console/applist_test.go
index dcb79cd1e..3caeb0deb 100644
--- a/internal/console/applist_test.go
+++ b/internal/console/applist_test.go
@@ -1,6 +1,7 @@
package console
import (
+ "context"
"github.com/carlosframework/platform/internal/release"
"net/http"
"net/url"
@@ -12,12 +13,12 @@ import (
func starredOf(t *testing.T, fx *consoleFixture, address string) map[string]bool {
t.Helper()
- lifes, err := fx.st.StarredLifetimes(OpsAccountID, address)
+ lifes, err := fx.st.StarredLifetimes(context.Background(), OpsAccountID, address)
if err != nil {
t.Fatal(err)
}
out := map[string]bool{}
- lives, err := fx.st.AppLivesIn(OpsAccountID)
+ lives, err := fx.st.AppLivesIn(context.Background(), OpsAccountID)
if err != nil {
t.Fatal(err)
}
@@ -104,7 +105,7 @@ func TestAppStarGates(t *testing.T) {
t.Errorf("non-member star = %d %q, want a bare 404", status, body)
}
}
- if l, _ := fx.st.StarredLifetimes(OpsAccountID, "eve@keymail.dev"); len(l) != 0 {
+ if l, _ := fx.st.StarredLifetimes(context.Background(), OpsAccountID, "eve@keymail.dev"); len(l) != 0 {
t.Fatalf("a non-member's star was written: %v", l)
}
// Signed out: sent to the front page, nothing written.
@@ -136,7 +137,7 @@ func TestAppStarFollowsTheAppLifetime(t *testing.T) {
if status, _ := star(t, fx, paul, csrf, "blog", "true"); status != http.StatusSeeOther {
t.Fatalf("star = %d", status)
}
- if l, _ := fx.st.StarredLifetimes(otherID, "paul@keymail.dev"); len(l) != 0 {
+ if l, _ := fx.st.StarredLifetimes(context.Background(), otherID, "paul@keymail.dev"); len(l) != 0 {
t.Fatalf("a star in bab shows in another account: %v", l)
}
if err := fx.st.RenameAppIn(OpsAccountID, "blog", "blog-two"); err != nil {
@@ -218,7 +219,7 @@ func TestAccountHomePartitionsByViewer(t *testing.T) {
for _, a := range []string{"kass", "titogo", "tmp-a", "tmp-b"} {
p.Apps = append(p.Apps, appRow{App: a})
}
- if err := fx.srv.fillAppLists(&p, acct, addr, q); err != nil {
+ if err := fx.srv.fillAppLists(context.Background(), &p, acct, addr, q); err != nil {
t.Fatal(err)
}
return p
@@ -265,10 +266,10 @@ func TestAppStarsSurviveReopen(t *testing.T) {
t.Fatal(err)
}
defer st.Close()
- if l, err := st.StarredLifetimes(1, "paul@keymail.dev"); err != nil || !l["life-1"] || len(l) != 1 {
+ if l, err := st.StarredLifetimes(context.Background(), 1, "paul@keymail.dev"); err != nil || !l["life-1"] || len(l) != 1 {
t.Fatalf("after reopen = %v %v", l, err)
}
- if l, _ := st.StarredLifetimes(2, "paul@keymail.dev"); len(l) != 0 {
+ if l, _ := st.StarredLifetimes(context.Background(), 2, "paul@keymail.dev"); len(l) != 0 {
t.Fatalf("account 2 sees account 1's star: %v", l)
}
}
diff --git a/internal/console/appregionset.go b/internal/console/appregionset.go
index 81375bd7b..c85a71329 100644
--- a/internal/console/appregionset.go
+++ b/internal/console/appregionset.go
@@ -212,7 +212,7 @@ func (s *Server) handleAPIAppRegionGet(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, appRegionView{App: app, Region: l.Code, Label: l.Text, Fleet: name})
return
}
- region, source, err := s.appRegion(acct.ID, app)
+ region, source, err := s.appRegion(r.Context(), acct.ID, app)
if err != nil {
log.Printf("console: app region %s/%s: read: %v", acct.Sqid, app, err)
jsonError(w, http.StatusBadGateway, msgAppRegionUnreadable)
diff --git a/internal/console/appsettings.go b/internal/console/appsettings.go
index 5cce435fc..2ab87ddc5 100644
--- a/internal/console/appsettings.go
+++ b/internal/console/appsettings.go
@@ -1106,7 +1106,7 @@ func (s *Server) appPolicyState(ctx context.Context, acct *Account, name string)
// behind, and the moved app, which no create clears for, would take up an
// earlier app's at the destination. "" means the move may go ahead.
func (s *Server) movePolicyRefusal(ctx context.Context, from, to *Account, app string) string {
- if has, err := s.store.hasClaimedSource(from.ID, app); err != nil {
+ if has, err := s.store.hasClaimedSource(ctx, from.ID, app); err != nil {
return sourceUnavailable
} else if has {
return sourceMoveRefused
@@ -1720,7 +1720,7 @@ func (s *Server) handleAppMoveAccount(w http.ResponseWriter, r *http.Request) {
back("err", msg)
return
}
- target, agreed, err := s.routeAccountFor(addr, acct, app)
+ target, agreed, err := s.routeAccountFor(r.Context(), addr, acct, app)
if errors.Is(err, errMoveTargetUnavailable) {
http.NotFound(w, r)
return
@@ -1845,8 +1845,8 @@ var errMoveTargetUnavailable = errors.New("move target unavailable to this viewe
// routeAccountFor recomputes the viewer-safe diagnostic at action time. It
// grants a target only when every relevant route points to one accessible
// account which does not already claim its own same-named app.
-func (s *Server) routeAccountFor(address string, source *Account, app string) (target string, agreed bool, err error) {
- review, all, err := s.appAccountReview(address, source, app)
+func (s *Server) routeAccountFor(ctx context.Context, address string, source *Account, app string) (target string, agreed bool, err error) {
+ review, all, err := s.appAccountReview(ctx, address, source, app)
if err != nil {
return "", false, err
}
diff --git a/internal/console/appstarcleanup_test.go b/internal/console/appstarcleanup_test.go
index 2b93267bb..332d01404 100644
--- a/internal/console/appstarcleanup_test.go
+++ b/internal/console/appstarcleanup_test.go
@@ -1,6 +1,7 @@
package console
import (
+ "context"
"testing"
"time"
)
@@ -18,7 +19,7 @@ func TestAppStarsEndWithMembership(t *testing.T) {
if err := st.RemoveMember(id, "member@example.com"); err != nil {
t.Fatal(err)
}
- stars, err := st.StarredLifetimes(id, "member@example.com")
+ stars, err := st.StarredLifetimes(context.Background(), id, "member@example.com")
if err != nil || len(stars) != 0 {
t.Fatalf("stars after removal: %v %v", stars, err)
}
@@ -41,7 +42,7 @@ func TestAppStarsEndWithAccount(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- stars, err := st.StarredLifetimes(id, "member@example.com")
+ stars, err := st.StarredLifetimes(context.Background(), id, "member@example.com")
if err != nil || len(stars) != 0 {
t.Fatalf("reap=%v: stars after deletion: %v %v", reap, stars, err)
}
@@ -62,7 +63,7 @@ func TestAppStarsEndOnlyWhenAppPurged(t *testing.T) {
if err := st.PurgeAppRow(id, "demo"); err != nil {
t.Fatal(err)
}
- stars, err := st.StarredLifetimes(id, "member@example.com")
+ stars, err := st.StarredLifetimes(context.Background(), id, "member@example.com")
if err != nil || len(stars) != 1 {
t.Fatal("live app lost star")
}
@@ -72,7 +73,7 @@ func TestAppStarsEndOnlyWhenAppPurged(t *testing.T) {
if err := st.PurgeAppRow(id, "demo"); err != nil {
t.Fatal(err)
}
- stars, err = st.StarredLifetimes(id, "member@example.com")
+ stars, err = st.StarredLifetimes(context.Background(), id, "member@example.com")
if err != nil || len(stars) != 0 {
t.Fatalf("stars after purge: %v %v", stars, err)
}
diff --git a/internal/console/authstore.go b/internal/console/authstore.go
index e1b6e4a11..893978003 100644
--- a/internal/console/authstore.go
+++ b/internal/console/authstore.go
@@ -850,9 +850,16 @@ func scanCLITokenRows(rows *sql.Rows) ([]CLIToken, error) {
//
// A link credential is not listed: its address is the person who approved
// the link, but it does not speak as them, and it is managed (rotated,
-// unlinked) on the account, never revoked from a person's settings.
+// unlinked) on the account, never revoked from a person's settings. It is
+// CLITokensContext for callers with no request to follow.
func (s *Store) CLITokens(address string) ([]CLIToken, error) {
- rows, err := s.db.Query(`SELECT token_hash, address, label, remote_addr, kind, account_id, app,
+ return s.CLITokensContext(context.Background(), address)
+}
+
+// CLITokensContext is CLITokens under the caller's context: the account
+// home's Getting started card reads it.
+func (s *Store) CLITokensContext(ctx context.Context, address string) ([]CLIToken, error) {
+ rows, err := s.db.QueryContext(ctx, `SELECT token_hash, address, label, remote_addr, kind, account_id, app,
role, name, created_at, expires_at, last_used, revoked_at, rotated_at, rungs, commands, scope
FROM cli_tokens WHERE address = ? AND revoked_at = 0 AND kind != ? ORDER BY created_at DESC, token_hash`, address, cliKindLink)
if err != nil {
diff --git a/internal/console/claim_invariant_probes_nonapp_test.go b/internal/console/claim_invariant_probes_nonapp_test.go
index e9318a893..4d6417813 100644
--- a/internal/console/claim_invariant_probes_nonapp_test.go
+++ b/internal/console/claim_invariant_probes_nonapp_test.go
@@ -718,7 +718,7 @@ var claimNonAppProbes = map[string]claimProbe{
if err != nil || l == nil {
return fmt.Sprintf("read kass: %v", err)
}
- stars, err := cx.st.StarredLifetimes(OpsAccountID, claimFxOwner)
+ stars, err := cx.st.StarredLifetimes(context.Background(), OpsAccountID, claimFxOwner)
if err != nil {
return err.Error()
}
@@ -1564,7 +1564,7 @@ func claimGettingStartedProbe(action string, kv []string, mark gsMark, want bool
do: claimFormReply("/a/bab/getting-started/"+action, form...),
status: http.StatusSeeOther, loc: "/a/bab/",
effect: func(t *testing.T, cx *claimCase, r claimReply) string {
- marks, err := cx.st.GettingStartedMarks(OpsAccountID, claimFxOwner)
+ marks, err := cx.st.GettingStartedMarks(context.Background(), OpsAccountID, claimFxOwner)
if err != nil {
return err.Error()
}
diff --git a/internal/console/deletion_test.go b/internal/console/deletion_test.go
index 3f4c043e3..eab2fb3aa 100644
--- a/internal/console/deletion_test.go
+++ b/internal/console/deletion_test.go
@@ -245,7 +245,7 @@ func TestTrashPageSurvivesAnUnreadableMarker(t *testing.T) {
}
// The ROW's own date stands in, so the page still says when this will be
// destroyed rather than showing the zero time.
- rows, err := fx.st.TrashedAppsIn(OpsAccountID)
+ rows, err := fx.st.TrashedAppsIn(context.Background(), OpsAccountID)
if err != nil || len(rows) != 1 {
t.Fatalf("TrashedAppsIn = %+v %v", rows, err)
}
diff --git a/internal/console/demo_claim_page.go b/internal/console/demo_claim_page.go
index 6db225e31..e7d324d12 100644
--- a/internal/console/demo_claim_page.go
+++ b/internal/console/demo_claim_page.go
@@ -217,7 +217,7 @@ func (s *Server) handleDemoClaimPage(w http.ResponseWriter, r *http.Request) {
p.Copy["agentDetail"] = "Give your agent this prompt so it can help you sign in, fetch the source, and deploy changes to this app."
p.Copy["agentLine"] = fmt.Sprintf("Continue app %s in account %s (ID %s) on console %s. Follow https://carlosframework.com/start#after-you-keep-it to help me sign in, fetch its claimed source, then build and deploy my changes to that existing app.", p.App, acct.Name, acct.Sqid, s.cfg.Origin)
p.Copy["sourceNote"] = "Source was saved when you kept this app. Later deployments do not update it."
- if has, err := s.store.hasClaimedSource(acct.ID, p.App); err == nil && has {
+ if has, err := s.store.hasClaimedSource(r.Context(), acct.ID, p.App); err == nil && has {
p.SourceSteps = claimSourceSteps(p.App, acct.Sqid, s.cfg.Origin)
} else {
p.Copy["sourceNote"] = "No saved source for this app."
diff --git a/internal/console/economicspass.go b/internal/console/economicspass.go
index 8afa5f78f..9c248fee1 100644
--- a/internal/console/economicspass.go
+++ b/internal/console/economicspass.go
@@ -200,7 +200,7 @@ func (s *Server) censusRecords(ctx context.Context) ([]censusTenancy, error) {
if err != nil {
return nil, err
}
- gone, err := s.store.TrashedAppsIn(a.ID)
+ gone, err := s.store.TrashedAppsIn(ctx, a.ID)
if err != nil {
return nil, err
}
diff --git a/internal/console/fleets.go b/internal/console/fleets.go
index 0362897f0..aabe82b1d 100644
--- a/internal/console/fleets.go
+++ b/internal/console/fleets.go
@@ -733,8 +733,16 @@ func (s *Server) handleAPIFleetDrainBox(w http.ResponseWriter, r *http.Request)
// fleet/<f> route can outlive its app and name a born-HA app recreated
// under the same name; effectivePlacement answers "" for that row whatever
// its column or its routes say.
+//
+// appPlacement is appPlacementContext for callers with no request to
+// follow.
func (s *Server) appPlacement(reg consoleRegistry, acct *Account, app string) (string, error) {
- stored, haPool, err := s.store.appPlacementRow(acct.ID, app)
+ return s.appPlacementContext(context.Background(), reg, acct, app)
+}
+
+// appPlacementContext is appPlacement with its store read under ctx.
+func (s *Server) appPlacementContext(ctx context.Context, reg consoleRegistry, acct *Account, app string) (string, error) {
+ stored, haPool, err := s.store.appPlacementRow(ctx, acct.ID, app)
if err != nil {
return "", err
}
diff --git a/internal/console/fleetsecretskey.go b/internal/console/fleetsecretskey.go
index 4ddd2806b..5adc5ccab 100644
--- a/internal/console/fleetsecretskey.go
+++ b/internal/console/fleetsecretskey.go
@@ -378,7 +378,8 @@ func (s *Server) fleetPlacedAppsIn(acct *Account, fleet string, includeTrash boo
listed := false
var out []string
for _, app := range apps {
- stored, haPool, err := s.store.appPlacementRow(acct.ID, app)
+ // fleetPlacedAppsIn's callers carry no context yet.
+ stored, haPool, err := s.store.appPlacementRow(context.Background(), acct.ID, app)
if err != nil {
return nil, err
}
diff --git a/internal/console/gettingstarted.go b/internal/console/gettingstarted.go
index 4ea3aa3fb..b2d8ccf61 100644
--- a/internal/console/gettingstarted.go
+++ b/internal/console/gettingstarted.go
@@ -1,6 +1,7 @@
package console
import (
+ "context"
"log"
"reflect"
"strings"
@@ -142,15 +143,15 @@ func buildGettingStarted(marks map[gsMark]bool, connected, anyLive bool, role st
// not the filtered lists. Only console-database reads: no bucket call. A
// failed read never fails the page: unread tokens leave step 1 open,
// unread marks show no card and no Show link.
-func (s *Server) fillGettingStarted(p *page, acct *Account, addr, role string, live map[string]bool) {
+func (s *Server) fillGettingStarted(ctx context.Context, p *page, acct *Account, addr, role string, live map[string]bool) {
now := time.Now()
connected := false
- if tokens, err := s.store.CLITokens(addr); err != nil {
+ if tokens, err := s.store.CLITokensContext(ctx, addr); err != nil {
log.Printf("console: getting started tokens %s: %v", acct.Sqid, err)
} else {
connected = personConnected(tokens, now)
}
- marks, err := s.store.GettingStartedMarks(acct.ID, addr)
+ marks, err := s.store.GettingStartedMarks(ctx, acct.ID, addr)
if err != nil {
log.Printf("console: getting started marks %s: %v", acct.Sqid, err)
return
@@ -167,7 +168,7 @@ func (s *Server) fillGettingStarted(p *page, acct *Account, addr, role string, l
// Conditional on the manual steps' marks still being there: a Show
// pressed since the read above wins (CompleteGettingStarted). Either
// way this response offers Show; the next one reads the live state.
- if _, err := s.store.CompleteGettingStarted(acct.ID, addr, now); err != nil {
+ if _, err := s.store.CompleteGettingStarted(ctx, acct.ID, addr, now); err != nil {
log.Printf("console: getting started completed %s: %v", acct.Sqid, err)
}
p.ShowGettingStarted = true
diff --git a/internal/console/gettingstarted_handlers_test.go b/internal/console/gettingstarted_handlers_test.go
index 1ce07addf..fbe6146c8 100644
--- a/internal/console/gettingstarted_handlers_test.go
+++ b/internal/console/gettingstarted_handlers_test.go
@@ -1,6 +1,7 @@
package console
import (
+ "context"
"net/http"
"net/url"
"testing"
@@ -9,7 +10,7 @@ import (
func gsMarksOf(t *testing.T, fx *consoleFixture, address string) map[gsMark]bool {
t.Helper()
- got, err := fx.st.GettingStartedMarks(OpsAccountID, address)
+ got, err := fx.st.GettingStartedMarks(context.Background(), OpsAccountID, address)
if err != nil {
t.Fatal(err)
}
diff --git a/internal/console/gettingstarted_page_test.go b/internal/console/gettingstarted_page_test.go
index ee7ffc4a8..a9a99791f 100644
--- a/internal/console/gettingstarted_page_test.go
+++ b/internal/console/gettingstarted_page_test.go
@@ -369,7 +369,7 @@ func TestGettingStartedSecretsNoLinkWithoutLiveApp(t *testing.T) {
for _, live := range []map[string]bool{{}, {"elsewhere": true}} {
var p page
p.Apps = apps
- fx.srv.fillGettingStarted(&p, acct, "paul@keymail.dev", RoleOwner, live)
+ fx.srv.fillGettingStarted(context.Background(), &p, acct, "paul@keymail.dev", RoleOwner, live)
v := p.GettingStarted
if v == nil {
t.Fatalf("live %v: no card", live)
@@ -380,7 +380,7 @@ func TestGettingStartedSecretsNoLinkWithoutLiveApp(t *testing.T) {
}
var p page
p.Apps = apps
- fx.srv.fillGettingStarted(&p, acct, "paul@keymail.dev", RoleOwner, map[string]bool{"idle": true})
+ fx.srv.fillGettingStarted(context.Background(), &p, acct, "paul@keymail.dev", RoleOwner, map[string]bool{"idle": true})
if want := appSettingsURL(fx.sqid, "idle", "environments"); p.GettingStarted == nil || p.GettingStarted.SecretsURL != want {
t.Errorf("live idle: view %+v, want secrets URL %q", p.GettingStarted, want)
}
diff --git a/internal/console/gettingstarted_store.go b/internal/console/gettingstarted_store.go
index f7278bfa7..f41f7f4f5 100644
--- a/internal/console/gettingstarted_store.go
+++ b/internal/console/gettingstarted_store.go
@@ -1,6 +1,7 @@
package console
import (
+ "context"
"errors"
"time"
)
@@ -38,8 +39,8 @@ func (s *Store) migrateGettingStarted() error {
}
// GettingStartedMarks is the set of marks address has set in the account.
-func (s *Store) GettingStartedMarks(accountID int64, address string) (map[gsMark]bool, error) {
- rows, err := s.db.Query(`SELECT mark FROM getting_started_marks WHERE account_id = ? AND address = ?`, accountID, address)
+func (s *Store) GettingStartedMarks(ctx context.Context, accountID int64, address string) (map[gsMark]bool, error) {
+ rows, err := s.db.QueryContext(ctx, `SELECT mark FROM getting_started_marks WHERE account_id = ? AND address = ?`, accountID, address)
if err != nil {
return nil, err
}
@@ -90,8 +91,8 @@ ON CONFLICT (account_id, address, mark) DO NOTHING`, accountID, address, string(
// then undo a Show pressed since: the reset deleted those marks, so the
// write finds nothing to stand on. ok reports whether completed is stored
// (already present counts).
-func (s *Store) CompleteGettingStarted(accountID int64, address string, now time.Time) (ok bool, err error) {
- if _, err := s.db.Exec(`INSERT INTO getting_started_marks (account_id, address, mark, marked_at)
+func (s *Store) CompleteGettingStarted(ctx context.Context, accountID int64, address string, now time.Time) (ok bool, err error) {
+ if _, err := s.db.ExecContext(ctx, `INSERT INTO getting_started_marks (account_id, address, mark, marked_at)
SELECT ?, ?, ?, ?
WHERE EXISTS (SELECT 1 FROM getting_started_marks WHERE account_id = ? AND address = ? AND mark IN (?, ?))
AND EXISTS (SELECT 1 FROM getting_started_marks WHERE account_id = ? AND address = ? AND mark IN (?, ?))
@@ -102,7 +103,7 @@ ON CONFLICT (account_id, address, mark) DO NOTHING`,
return false, err
}
var n int
- if err := s.db.QueryRow(`SELECT COUNT(*) FROM getting_started_marks WHERE account_id = ? AND address = ? AND mark = ?`,
+ if err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM getting_started_marks WHERE account_id = ? AND address = ? AND mark = ?`,
accountID, address, string(gsCompleted)).Scan(&n); err != nil {
return false, err
}
diff --git a/internal/console/gettingstarted_store_test.go b/internal/console/gettingstarted_store_test.go
index 1403fcf7e..ab3c52c60 100644
--- a/internal/console/gettingstarted_store_test.go
+++ b/internal/console/gettingstarted_store_test.go
@@ -1,6 +1,7 @@
package console
import (
+ "context"
"reflect"
"testing"
"time"
@@ -15,7 +16,7 @@ func TestGettingStartedMarksRoundTrip(t *testing.T) {
if err := st.SetGettingStartedMark(1, "a@example.com", gsHidden, now); err != nil {
t.Fatal(err)
}
- got, err := st.GettingStartedMarks(1, "a@example.com")
+ got, err := st.GettingStartedMarks(context.Background(), 1, "a@example.com")
if err != nil {
t.Fatal(err)
}
@@ -34,7 +35,7 @@ func TestGettingStartedDoneReplacesSkip(t *testing.T) {
t.Fatal(err)
}
}
- got, err := st.GettingStartedMarks(1, addr)
+ got, err := st.GettingStartedMarks(context.Background(), 1, addr)
if err != nil {
t.Fatal(err)
}
@@ -52,7 +53,7 @@ func TestGettingStartedReset(t *testing.T) {
t.Fatal(err)
}
}
- if ok, err := st.CompleteGettingStarted(1, "a@example.com", now); err != nil || !ok {
+ if ok, err := st.CompleteGettingStarted(context.Background(), 1, "a@example.com", now); err != nil || !ok {
t.Fatalf("complete: %v %v", ok, err)
}
if err := st.SetGettingStartedMark(1, "b@example.com", gsHidden, now); err != nil {
@@ -61,11 +62,11 @@ func TestGettingStartedReset(t *testing.T) {
if err := st.ResetGettingStarted(1, "a@example.com"); err != nil {
t.Fatal(err)
}
- got, err := st.GettingStartedMarks(1, "a@example.com")
+ got, err := st.GettingStartedMarks(context.Background(), 1, "a@example.com")
if err != nil || len(got) != 0 {
t.Fatalf("after reset: %v %v", got, err)
}
- if other, _ := st.GettingStartedMarks(1, "b@example.com"); !other[gsHidden] {
+ if other, _ := st.GettingStartedMarks(context.Background(), 1, "b@example.com"); !other[gsHidden] {
t.Fatal("reset touched another person")
}
}
@@ -75,10 +76,10 @@ func TestGettingStartedMarksPerAccount(t *testing.T) {
if err := st.SetGettingStartedMark(1, "a@example.com", gsHidden, time.Now()); err != nil {
t.Fatal(err)
}
- if got, _ := st.GettingStartedMarks(2, "a@example.com"); len(got) != 0 {
+ if got, _ := st.GettingStartedMarks(context.Background(), 2, "a@example.com"); len(got) != 0 {
t.Fatalf("leaked to account 2: %v", got)
}
- if got, _ := st.GettingStartedMarks(1, "b@example.com"); len(got) != 0 {
+ if got, _ := st.GettingStartedMarks(context.Background(), 1, "b@example.com"); len(got) != 0 {
t.Fatalf("leaked to another address: %v", got)
}
}
@@ -96,7 +97,7 @@ func TestGettingStartedCleanup(t *testing.T) {
if err := st.RemoveMember(id, "member@example.com"); err != nil {
t.Fatal(err)
}
- if got, err := st.GettingStartedMarks(id, "member@example.com"); err != nil || len(got) != 0 {
+ if got, err := st.GettingStartedMarks(context.Background(), id, "member@example.com"); err != nil || len(got) != 0 {
t.Fatalf("after RemoveMember: %v %v", got, err)
}
for _, reap := range []bool{false, true} {
@@ -114,7 +115,7 @@ func TestGettingStartedCleanup(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- if got, err := st.GettingStartedMarks(id, "member@example.com"); err != nil || len(got) != 0 {
+ if got, err := st.GettingStartedMarks(context.Background(), id, "member@example.com"); err != nil || len(got) != 0 {
t.Fatalf("reap=%v: %v %v", reap, got, err)
}
}
@@ -128,11 +129,11 @@ func TestGettingStartedCompleteConditional(t *testing.T) {
const addr = "a@example.com"
complete := func(want bool) {
t.Helper()
- ok, err := st.CompleteGettingStarted(1, addr, now)
+ ok, err := st.CompleteGettingStarted(context.Background(), 1, addr, now)
if err != nil {
t.Fatal(err)
}
- got, _ := st.GettingStartedMarks(1, addr)
+ got, _ := st.GettingStartedMarks(context.Background(), 1, addr)
if ok != want || got[gsCompleted] != want {
t.Fatalf("complete = %v, stored %v, want %v (marks %v)", ok, got[gsCompleted], want, got)
}
@@ -162,7 +163,7 @@ func TestGettingStartedCompleteConditional(t *testing.T) {
if err := st.SetGettingStartedMark(1, addr, gsCompleted, now); err == nil {
t.Fatal("SetGettingStartedMark wrote completed unconditionally")
}
- if got, _ := st.GettingStartedMarks(1, addr); got[gsCompleted] {
+ if got, _ := st.GettingStartedMarks(context.Background(), 1, addr); got[gsCompleted] {
t.Fatal("refused completed was stored")
}
}
diff --git a/internal/console/links_auth_test.go b/internal/console/links_auth_test.go
index a49b06bb3..9220aa441 100644
--- a/internal/console/links_auth_test.go
+++ b/internal/console/links_auth_test.go
@@ -829,7 +829,7 @@ func TestLinkDoorsRefuseEveryOtherBearer(t *testing.T) {
}
}
// None of them changed anything a link door would.
- if apps, err := fx.st.AppLivesIn(acct.ID); err != nil {
+ if apps, err := fx.st.AppLivesIn(context.Background(), acct.ID); err != nil {
t.Fatal(err)
} else {
for _, a := range apps {
diff --git a/internal/console/links_store.go b/internal/console/links_store.go
index fb3c42746..eb121b6d2 100644
--- a/internal/console/links_store.go
+++ b/internal/console/links_store.go
@@ -539,8 +539,8 @@ WHERE t.token_hash = ? AND t.kind = ? AND l.token_hash = t.token_hash AND l.acco
// linkedAppHomes maps each linked app of accountID to the name of the
// deployment it is served from: what the account's app list labels it
// with (LD-C31) and what the Fleet tab leaves out of its platform count.
-func (s *Store) linkedAppHomes(accountID int64) (map[string]string, error) {
- rows, err := s.db.Query(`SELECT p.app, l.name FROM apps p JOIN links l ON l.id = p.linked_link_id
+func (s *Store) linkedAppHomes(ctx context.Context, accountID int64) (map[string]string, error) {
+ rows, err := s.db.QueryContext(ctx, `SELECT p.app, l.name FROM apps p JOIN links l ON l.id = p.linked_link_id
WHERE p.account_id = ? AND p.linked_link_id != ''`, accountID)
if err != nil {
return nil, err
diff --git a/internal/console/materialize_test.go b/internal/console/materialize_test.go
index 2a68399f5..09dadf8e7 100644
--- a/internal/console/materialize_test.go
+++ b/internal/console/materialize_test.go
@@ -436,7 +436,7 @@ func TestMaterializeReapsTombstonedAccounts(t *testing.T) {
if apps, _ := st.AppsIn(id); len(apps) != 0 {
t.Errorf("AppsIn = %v, want none", apps)
}
- if trashed, _ := st.TrashedAppsIn(id); len(trashed) != 0 {
+ if trashed, _ := st.TrashedAppsIn(context.Background(), id); len(trashed) != 0 {
t.Errorf("TrashedAppsIn = %+v, want none — the app rows are reaped with the account", trashed)
}
if last, err := st.LastAccount(context.Background(), "paul@keymail.dev"); err != nil || last != 0 {
@@ -607,7 +607,7 @@ func TestMaterializeAppsConverges(t *testing.T) {
// The cooloff clock comes from the MARKER, not from this pass: the bucket
// records when the app was trashed, and a console catching up days later
// must not restart the 30 days.
- trashedRows, err := st.TrashedAppsIn(id)
+ trashedRows, err := st.TrashedAppsIn(context.Background(), id)
if err != nil {
t.Fatal(err)
}
diff --git a/internal/console/mobilenav.go b/internal/console/mobilenav.go
index a8b0e877b..13425e74b 100644
--- a/internal/console/mobilenav.go
+++ b/internal/console/mobilenav.go
@@ -235,7 +235,7 @@ func (s *Server) handleAppIndex(w http.ResponseWriter, r *http.Request) {
p := s.accountPage(w, r, addr, raw, acct, role)
p.Title = app
p.AppName = app
- p.LinkedApp = s.linkedAppPage(acct, app)
+ p.LinkedApp = s.linkedAppPage(r.Context(), acct, app)
p.ActiveNav = "apps"
p.IndexOf = indexApp
s.render(w, http.StatusOK, "nav_index", p)
diff --git a/internal/console/ownership.go b/internal/console/ownership.go
index 09a060570..a2f9d46c1 100644
--- a/internal/console/ownership.go
+++ b/internal/console/ownership.go
@@ -237,7 +237,7 @@ func (s *Server) handleTrash(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
- rows, err := s.store.TrashedAppsIn(acct.ID)
+ rows, err := s.store.TrashedAppsIn(r.Context(), acct.ID)
if err != nil {
log.Printf("console: trash %s: %v", acct.Sqid, err)
s.renderMessage(w, r, http.StatusInternalServerError, "Error",
diff --git a/internal/console/pending.go b/internal/console/pending.go
index 1061842ba..9ea4c9bb5 100644
--- a/internal/console/pending.go
+++ b/internal/console/pending.go
@@ -181,7 +181,8 @@ func (s *Store) ConfirmChange(id, address string, now time.Time) (*PendingChange
id, address, now.Unix()); err != nil {
return nil, false, err
}
- confirms, err := confirmsFor(tx, id)
+ // ConfirmChange carries no context yet.
+ confirms, err := confirmsFor(context.Background(), tx, id)
if err != nil {
return nil, false, err
}
@@ -193,13 +194,13 @@ func (s *Store) ConfirmChange(id, address string, now time.Time) (*PendingChange
}
// PendingChangesFor lists an account/app's unexpired, unexecuted changes.
-func (s *Store) claimRawPendingChangesFor(accountID int64, app string, now time.Time) ([]PendingChange, error) {
- tx, err := s.db.Begin()
+func (s *Store) claimRawPendingChangesFor(ctx context.Context, accountID int64, app string, now time.Time) ([]PendingChange, error) {
+ tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer tx.Rollback()
- rows, err := tx.Query(`SELECT id, account_id, app, kind, payload, quorum, proposer, created_at, expires_at
+ rows, err := tx.QueryContext(ctx, `SELECT id, account_id, app, kind, payload, quorum, proposer, created_at, expires_at
FROM pending_changes
WHERE account_id = ? AND app = ? AND executed_at = 0 AND expires_at > ?
ORDER BY created_at, id`, accountID, app, now.Unix())
@@ -226,7 +227,7 @@ ORDER BY created_at, id`, accountID, app, now.Unix())
return nil, err
}
for i, id := range ids {
- confirms, err := confirmsFor(tx, id)
+ confirms, err := confirmsFor(ctx, tx, id)
if err != nil {
return nil, err
}
@@ -260,7 +261,8 @@ func (s *Store) PendingChange(id string, now time.Time) (*PendingChange, error)
if !executedAtZero(tx, id) {
return nil, nil
}
- confirms, err := confirmsFor(tx, id)
+ // PendingChange carries no context yet.
+ confirms, err := confirmsFor(context.Background(), tx, id)
if err != nil {
return nil, err
}
@@ -285,8 +287,8 @@ func (s *Store) PendingChange(id string, now time.Time) (*PendingChange, error)
// keeps showing every row — the gap between the two numbers is the honest
// picture, and a card that quietly dropped a name would make a stuck change
// unexplainable.
-func (s *Store) MemberConfirms(accountID int64, id string) ([]string, error) {
- rows, err := s.db.Query(`SELECT c.address FROM pending_confirms c
+func (s *Store) MemberConfirms(ctx context.Context, accountID int64, id string) ([]string, error) {
+ rows, err := s.db.QueryContext(ctx, `SELECT c.address FROM pending_confirms c
JOIN memberships m ON m.address = c.address AND m.account_id = ?
WHERE c.change_id = ? ORDER BY c.confirmed_at, c.address`, accountID, id)
if err != nil {
@@ -367,8 +369,8 @@ func executedAtZero(tx *sql.Tx, id string) bool {
// confirmsFor returns a change's confirming addresses, sorted by when they
// confirmed.
-func confirmsFor(tx *sql.Tx, id string) ([]string, error) {
- rows, err := tx.Query(`SELECT address FROM pending_confirms WHERE change_id = ? ORDER BY confirmed_at, address`, id)
+func confirmsFor(ctx context.Context, tx *sql.Tx, id string) ([]string, error) {
+ rows, err := tx.QueryContext(ctx, `SELECT address FROM pending_confirms WHERE change_id = ? ORDER BY confirmed_at, address`, id)
if err != nil {
return nil, err
}
@@ -392,7 +394,7 @@ func (s *Store) PendingChangesFor(accountID int64, app string, now time.Time) (c
// shows the pending card register with the gate and refuse under a foreign
// operation. The armed triggers still refuse any write it could ever grow.
func (s *Store) PendingChangesForContext(claimContext context.Context, accountID int64, app string, now time.Time) (claimResult0 []PendingChange, claimResult1 error) {
- return s.claimRawPendingChangesFor(accountID, app, now)
+ return s.claimRawPendingChangesFor(claimContext, accountID, app, now)
}
func (s *Store) ProposeChange(accountID int64, app, kind string, payload []byte, quorum int, proposer string, proposerConfirmed bool, now time.Time) (claimResult0 *PendingChange, claimResult1 error) {
return s.ProposeChangeContext(context.Background(), accountID, app, kind, payload, quorum, proposer, proposerConfirmed, now)
diff --git a/internal/console/pendinghandlers.go b/internal/console/pendinghandlers.go
index 012518167..3f5adda8d 100644
--- a/internal/console/pendinghandlers.go
+++ b/internal/console/pendinghandlers.go
@@ -332,7 +332,7 @@ func (s *Server) executePending(ctx context.Context, acct *Account, app, actor s
if err != nil {
return "", err
}
- counted, err := s.store.MemberConfirms(pc.AccountID, pc.ID)
+ counted, err := s.store.MemberConfirms(ctx, pc.AccountID, pc.ID)
if err != nil {
log.Printf("console: execute %s %s: member confirms: %v", pc.Kind, pc.ID, err)
return "", errors.New("could not check who has confirmed this change")
@@ -454,9 +454,9 @@ func requiredQuorum(pc *PendingChange, pipe *release.Pipeline) (int, error) {
// The gap between this and len(pc.Confirms) is deliberately not hidden: a
// change stuck because someone left the account shows fewer confirmations
// than it has rows, which is the honest explanation for why it is stuck.
-func (s *Server) pendingTally(pc PendingChange, pipe *release.Pipeline) (confirmed, needed int) {
+func (s *Server) pendingTally(ctx context.Context, pc PendingChange, pipe *release.Pipeline) (confirmed, needed int) {
confirmed, needed = len(pc.Confirms), pc.Quorum
- if counted, err := s.store.MemberConfirms(pc.AccountID, pc.ID); err != nil {
+ if counted, err := s.store.MemberConfirms(ctx, pc.AccountID, pc.ID); err != nil {
log.Printf("console: pending tally %s: member confirms: %v", pc.ID, err)
} else {
confirmed = len(counted)
@@ -773,7 +773,7 @@ func (s *Server) handlePendingConfirm(w http.ResponseWriter, r *http.Request) {
// gets and the row the trail keeps say the same thing the gate does.
// Deciding on it also keeps the not-met path a plain progress message
// rather than an execution that refuses a beat later.
- confirmed, needed := s.pendingTally(*pc, s.pendingPipeline(ctx, acct, app))
+ confirmed, needed := s.pendingTally(ctx, *pc, s.pendingPipeline(ctx, acct, app))
if confirmed < needed {
s.audit(acct, app, addr, "confirmed", fmt.Sprintf("%s (%d of %d confirmations)",
pendingSummary(*pc), confirmed, needed))
@@ -877,7 +877,7 @@ func (s *Server) handleAPIPending(w http.ResponseWriter, r *http.Request) {
}
out := make([]map[string]any, 0, len(list))
for _, pc := range list {
- confirmed, needed := s.pendingTally(pc, pipe)
+ confirmed, needed := s.pendingTally(r.Context(), pc, pipe)
out = append(out, map[string]any{
"id": pc.ID,
"kind": pc.Kind,
diff --git a/internal/console/pipelinepage.go b/internal/console/pipelinepage.go
index f9f913bf2..cdda44d2f 100644
--- a/internal/console/pipelinepage.go
+++ b/internal/console/pipelinepage.go
@@ -56,10 +56,10 @@ type PendingChangeView struct {
// pipe is the app's pipeline, read ONCE by the caller for the whole listing
// (nil is fine — a legacy app, or a read that failed: the tally then degrades
// to the stored quorum, which is exactly what it was before this existed).
-func (s *Server) pendingChangeViews(pcs []PendingChange, pipe *release.Pipeline) []PendingChangeView {
+func (s *Server) pendingChangeViews(ctx context.Context, pcs []PendingChange, pipe *release.Pipeline) []PendingChangeView {
out := make([]PendingChangeView, len(pcs))
for i, pc := range pcs {
- confirmed, needed := s.pendingTally(pc, pipe)
+ confirmed, needed := s.pendingTally(ctx, pc, pipe)
out[i] = PendingChangeView{
ID: pc.ID, Kind: pc.Kind, Summary: pendingSummary(pc),
Confirmed: confirmed, Needed: needed,
diff --git a/internal/console/placement_census_test.go b/internal/console/placement_census_test.go
index b32140eff..9e6f274c4 100644
--- a/internal/console/placement_census_test.go
+++ b/internal/console/placement_census_test.go
@@ -55,7 +55,7 @@ func TestEffectivePlacementIsTheOnlyRouteFallback(t *testing.T) {
},
"appPlacementRow": {
"store.go:AppPlacement": "hands both columns to effectivePlacement (no fold)",
- "fleets.go:appPlacement": "hands both columns to effectivePlacement",
+ "fleets.go:appPlacementContext": "hands both columns to effectivePlacement (appPlacement is its wrapper)",
"fleetsecretskey.go:fleetPlacedAppsIn": "hands both columns to effectivePlacement",
"regionfacts.go:regionFactsFor": "hands both columns to effectivePlacement",
},
diff --git a/internal/console/regioncandidates.go b/internal/console/regioncandidates.go
index 2952abd72..039a9bd8c 100644
--- a/internal/console/regioncandidates.go
+++ b/internal/console/regioncandidates.go
@@ -301,7 +301,7 @@ func (s *Server) appPolicy(ctx context.Context, acct *Account, app string) regio
log.Printf("console: region policy %s/%s: open registry: %v", acct.Sqid, app, err)
return regionPolicy{State: policyUnreadable}
}
- placed, err := s.appPlacement(reg, acct, app)
+ placed, err := s.appPlacementContext(ctx, reg, acct, app)
if err != nil {
log.Printf("console: region policy %s/%s: placement: %v", acct.Sqid, app, err)
return regionPolicy{State: policyUnreadable}
diff --git a/internal/console/regionconfig.go b/internal/console/regionconfig.go
index 015260c6c..eff83a944 100644
--- a/internal/console/regionconfig.go
+++ b/internal/console/regionconfig.go
@@ -1,6 +1,7 @@
package console
import (
+ "context"
"log"
"github.com/carlosframework/platform/internal/appconfig"
@@ -28,13 +29,13 @@ func (s *Server) expectedSecretsFP() string {
return fp
}
-// appRegion reads the app's stored region through the test seam when one
-// is set.
-func (s *Server) appRegion(accountID int64, app string) (string, string, error) {
+// appRegion reads the app's stored region, under ctx, through the test seam
+// when one is set.
+func (s *Server) appRegion(ctx context.Context, accountID int64, app string) (string, string, error) {
if s.appRegionOf != nil {
return s.appRegionOf(accountID, app)
}
- return s.store.AppRegion(accountID, app)
+ return s.store.AppRegionContext(ctx, accountID, app)
}
// logRegionConfig names, once at startup, each configuration that leaves
diff --git a/internal/console/regionconfig_test.go b/internal/console/regionconfig_test.go
index 39ce9ab84..98bc1898b 100644
--- a/internal/console/regionconfig_test.go
+++ b/internal/console/regionconfig_test.go
@@ -2,6 +2,7 @@ package console
import (
"bytes"
+ "context"
"crypto/ecdh"
"encoding/base64"
"log"
@@ -56,13 +57,13 @@ func TestExpectedSecretsFP(t *testing.T) {
func TestAppRegionUsesTheSeamWhenSet(t *testing.T) {
fx := newConsoleFixture(t)
- if r, src, err := fx.srv.appRegion(OpsAccountID, "nobody"); err != nil || r != "" || src != "" {
+ if r, src, err := fx.srv.appRegion(context.Background(), OpsAccountID, "nobody"); err != nil || r != "" || src != "" {
t.Fatalf("store read of a missing app = %q %q %v, want empty", r, src, err)
}
fx.srv.appRegionOf = func(id int64, app string) (string, string, error) {
return "uk", "timezone", nil
}
- if r, src, err := fx.srv.appRegion(OpsAccountID, "nobody"); err != nil || r != "uk" || src != "timezone" {
+ if r, src, err := fx.srv.appRegion(context.Background(), OpsAccountID, "nobody"); err != nil || r != "uk" || src != "timezone" {
t.Fatalf("seam read = %q %q %v, want uk timezone", r, src, err)
}
}
diff --git a/internal/console/regiondefault.go b/internal/console/regiondefault.go
index 83ceae823..c221cd3bb 100644
--- a/internal/console/regiondefault.go
+++ b/internal/console/regiondefault.go
@@ -34,7 +34,7 @@ type appDefaultView struct {
func boolp(b bool) *bool { return &b }
func (s *Server) appDefault(ctx context.Context, acct *Account, app string) appDefaultView {
- stored, source, rerr := s.appRegion(acct.ID, app)
+ stored, source, rerr := s.appRegion(ctx, acct.ID, app)
snap, ferr := s.liveEdges(ctx)
cands := homeOnlyCandidates()
if ferr == nil {
diff --git a/internal/console/regionfacts.go b/internal/console/regionfacts.go
index fa937cc31..ad0f0627d 100644
--- a/internal/console/regionfacts.go
+++ b/internal/console/regionfacts.go
@@ -1,6 +1,7 @@
package console
import (
+ "context"
"errors"
"fmt"
"log"
@@ -180,8 +181,8 @@ func (s *Server) registryRoutes() ([]registry.Route, bool) {
// store's columns and a route fleet the caller already folded from rows it
// listed (routeFleet, with routesOK false when that listing failed). An
// unread fact is unknown: never the home by default.
-func (s *Server) regionFactsFor(acct *Account, app, routeFleet string, routesOK bool) appRegionFacts {
- stored, haPool, err := s.store.appPlacementRow(acct.ID, app)
+func (s *Server) regionFactsFor(ctx context.Context, acct *Account, app, routeFleet string, routesOK bool) appRegionFacts {
+ stored, haPool, err := s.store.appPlacementRow(ctx, acct.ID, app)
if err != nil {
log.Printf("console: region labels %s/%s: placement: %v", acct.Sqid, app, err)
return appRegionFacts{}
@@ -198,7 +199,7 @@ func (s *Server) regionFactsFor(acct *Account, app, routeFleet string, routesOK
if name, ok := registry.FleetPlacement(placed); ok {
return appRegionFacts{Known: true, Fleet: name, Label: regionlabel.ForCode(placed, s.regionHome(), s.regionNames())}
}
- region, source, err := s.appRegion(acct.ID, app)
+ region, source, err := s.appRegion(ctx, acct.ID, app)
if err != nil {
log.Printf("console: region labels %s/%s: region: %v", acct.Sqid, app, err)
return appRegionFacts{}
diff --git a/internal/console/regionfacts_test.go b/internal/console/regionfacts_test.go
index d6fd70db7..d9d91f22c 100644
--- a/internal/console/regionfacts_test.go
+++ b/internal/console/regionfacts_test.go
@@ -52,7 +52,7 @@ func TestRegionFactsByCase(t *testing.T) {
{"legacy", nil, false, "", "", ""},
{"cart", nil, false, "fleet private", "This app runs on your fleet private.", ""}, // the store's placement needs no registry
} {
- f := fx.srv.regionFactsFor(acct, c.app, fleetFromRoutes(c.routes, fx.sqid, c.app), c.routesOK)
+ f := fx.srv.regionFactsFor(context.Background(), acct, c.app, fleetFromRoutes(c.routes, fx.sqid, c.app), c.routesOK)
if f.Text() != c.text || f.Sentence() != c.sentence || f.SourceSentence() != c.source {
t.Errorf("%s (registry read %v): %q / %q / %q, want %q / %q / %q", c.app, c.routesOK,
f.Text(), f.Sentence(), f.SourceSentence(), c.text, c.sentence, c.source)
diff --git a/internal/console/server.go b/internal/console/server.go
index 2d8e9b856..546ac6b9b 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -4105,7 +4105,7 @@ func (s *Server) handleAccountHome(w http.ResponseWriter, r *http.Request) {
p.Welcome = true
s.clearCookie(w, welcomeCookie)
}
- apps, err := s.store.AppsIn(acct.ID)
+ apps, err := s.store.AppsInContext(r.Context(), acct.ID)
if err != nil {
s.renderMessage(w, r, http.StatusInternalServerError, "Error", "Could not read your apps. Please try again.")
return
@@ -4121,14 +4121,14 @@ func (s *Server) handleAccountHome(w http.ResponseWriter, r *http.Request) {
}
// Linked apps are labelled by their home. Best-effort: a failed read
// costs the label, never the page.
- homes, err := s.store.linkedAppHomes(acct.ID)
+ homes, err := s.store.linkedAppHomes(r.Context(), acct.ID)
if err != nil {
log.Printf("console: app list %s: linked apps: %v", acct.Sqid, err)
}
for _, v := range views {
row := summarize(v, acct.Sqid)
// Assemble listed the registry: its rows give the legacy fleet.
- row.Region = s.regionFactsFor(acct, v.App, v.routeFleet, true)
+ row.Region = s.regionFactsFor(r.Context(), acct, v.App, v.routeFleet, true)
row.Elsewhere = elsewhereCounts(v.Routes, acct.Sqid, row.Region)
if home, ok := homes[v.App]; ok {
row.ServedFrom = linkCopy(copyLD31, "home", home)
@@ -4140,7 +4140,7 @@ func (s *Server) handleAccountHome(w http.ResponseWriter, r *http.Request) {
}
}
// The viewer's stars, the expiries and the q filter (applist.go).
- if err := s.fillAppLists(&p, acct, addr, r.URL.Query().Get("q")); err != nil {
+ if err := s.fillAppLists(r.Context(), &p, acct, addr, r.URL.Query().Get("q")); err != nil {
log.Printf("console: app list %s: %v", acct.Sqid, err)
s.renderMessage(w, r, http.StatusInternalServerError, "Error", "Could not read your apps. Please try again.")
return
@@ -4148,12 +4148,12 @@ func (s *Server) handleAccountHome(w http.ResponseWriter, r *http.Request) {
}
// The Trash link, and only when there is something in it. Best-effort: a
// failed count costs a link, never the page.
- if trashed, err := s.store.TrashedAppsIn(acct.ID); err != nil {
+ if trashed, err := s.store.TrashedAppsIn(r.Context(), acct.ID); err != nil {
log.Printf("console: trashed apps: %v", err)
} else {
p.TrashN = len(trashed)
}
- s.fillGettingStarted(&p, acct, addr, role, live)
+ s.fillGettingStarted(r.Context(), &p, acct, addr, role, live)
s.render(w, http.StatusOK, "apps", p)
}
@@ -4348,24 +4348,24 @@ func (s *Server) appPage(w http.ResponseWriter, r *http.Request, tab string) (pa
p := s.accountPage(w, r, addr, raw, acct, role)
p.Title = app
p.AppName = app
- p.LinkedApp = s.linkedAppPage(acct, app)
+ p.LinkedApp = s.linkedAppPage(r.Context(), acct, app)
if role == RoleOwner || role == RoleMember {
- p.HasClaimedSource, _ = s.store.hasClaimedSource(acct.ID, app)
+ p.HasClaimedSource, _ = s.store.hasClaimedSource(r.Context(), acct.ID, app)
}
p.ActiveTab = tab
p.ActiveNav = "apps"
p.Detail = &views[0]
- p.StoredRegion = s.regionFactsFor(acct, app, views[0].routeFleet, true)
+ p.StoredRegion = s.regionFactsFor(r.Context(), acct, app, views[0].routeFleet, true)
p.StoredElsewhere = elsewhereCounts(views[0].Routes, acct.Sqid, p.StoredRegion)
end := perf.Span(r, "db")
- life, err := s.store.AppLife(acct.ID, app)
+ life, err := s.store.AppLifeContext(r.Context(), acct.ID, app)
end()
if err != nil {
log.Printf("console: app lifecycle %s/%s: %v", acct.Sqid, app, err)
} else if life != nil && life.Temporary() {
p.ExpiresAt = time.Unix(life.ExpiresAt, 0).UTC()
}
- if review, all, err := s.appAccountReview(addr, acct, app); err != nil {
+ if review, all, err := s.appAccountReview(r.Context(), addr, acct, app); err != nil {
log.Printf("console: account review %s/%s: %v", acct.Sqid, app, err)
} else {
// The review itself reads the console box's registry: it is about
@@ -4576,7 +4576,7 @@ func (s *Server) handleApp(w http.ResponseWriter, r *http.Request) {
}
p.Flow = assembleFlow(p.Detail, p.InstanceRows, latest, latestAt, p.Nav+"/apps/"+p.AppName+"/instances")
end := perf.Span(r, "db")
- events, err := s.store.Events(acct.ID, app, overviewActivityCap)
+ events, err := s.store.EventsContext(r.Context(), acct.ID, app, overviewActivityCap)
end()
if err != nil {
log.Printf("console: events %s: %v", app, err)
@@ -4591,7 +4591,7 @@ func (s *Server) handleApp(w http.ResponseWriter, r *http.Request) {
} else if len(pcs) > 0 {
// The frame's pipeline for the whole card: the rows show the quorum
// EXECUTION will require, not the one they were proposed under.
- p.Pending = s.pendingChangeViews(pcs, p.Detail.Pipeline)
+ p.Pending = s.pendingChangeViews(r.Context(), pcs, p.Detail.Pipeline)
}
end = perf.Span(r, "s3")
wait := time.NewTimer(trafficWait(start, time.Now()))
@@ -4645,7 +4645,7 @@ func (s *Server) handleAppReleases(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Printf("console: pending changes %s/%s: %v", acct.Sqid, p.AppName, err)
} else if len(pcs) > 0 {
- p.Pending = s.pendingChangeViews(pcs, p.Detail.Pipeline)
+ p.Pending = s.pendingChangeViews(r.Context(), pcs, p.Detail.Pipeline)
}
p.ReleaseSummary = assembleReleaseSummary(p.Detail.Channels, p.Detail.Routes, acct.Sqid)
p.PromoteChannels = promoteChannelOptions(p.Detail.Channels, p.Detail.Pipeline)
diff --git a/internal/console/store.go b/internal/console/store.go
index 3ba6c6b2d..40a2bb177 100644
--- a/internal/console/store.go
+++ b/internal/console/store.go
@@ -1964,9 +1964,15 @@ func (s *Store) CreateAppInRegion(accountID int64, app, createdBy, region, sourc
}
// AppRegion reads an app's region and its source label. A missing row
-// reads as the home with no source.
+// reads as the home with no source. It is AppRegionContext for callers with
+// no request to follow.
func (s *Store) AppRegion(accountID int64, app string) (region, source string, err error) {
- err = s.db.QueryRow(`SELECT region, region_source FROM apps WHERE account_id = ? AND app = ?`, accountID, app).
+ return s.AppRegionContext(context.Background(), accountID, app)
+}
+
+// AppRegionContext is AppRegion under the caller's context.
+func (s *Store) AppRegionContext(ctx context.Context, accountID int64, app string) (region, source string, err error) {
+ err = s.db.QueryRowContext(ctx, `SELECT region, region_source FROM apps WHERE account_id = ? AND app = ?`, accountID, app).
Scan(&region, &source)
if errors.Is(err, sql.ErrNoRows) {
return "", "", nil
@@ -2042,9 +2048,16 @@ WHERE p.region != '' AND p.deleted_at = 0 ORDER BY a.sqid, p.app`)
// filtered out — every door this feeds (API resolution, ship claim, account
// page listings) is a door a trashed app must vanish from automatically,
// without each caller having to remember to ask. TrashedAppsIn is the
-// dedicated listing for the Trash page itself.
+// dedicated listing for the Trash page itself. It is AppsInContext for
+// callers with no request to follow.
func (s *Store) AppsIn(accountID int64) ([]string, error) {
- rows, err := s.db.Query(`SELECT app FROM apps WHERE account_id = ? AND deleted_at = 0 ORDER BY app`, accountID)
+ return s.AppsInContext(context.Background(), accountID)
+}
+
+// AppsInContext is AppsIn under the caller's context: the account home's
+// app list reads it.
+func (s *Store) AppsInContext(ctx context.Context, accountID int64) ([]string, error) {
+ rows, err := s.db.QueryContext(ctx, `SELECT app FROM apps WHERE account_id = ? AND deleted_at = 0 ORDER BY app`, accountID)
if err != nil {
return nil, err
}
@@ -2184,7 +2197,8 @@ func (s *Store) claimRawSetAppBuildProbe(accountID int64, app, path string) erro
// A born-HA row reads "" whatever its column says (effectivePlacement, the
// one placement rule).
func (s *Store) AppPlacement(accountID int64, app string) (string, error) {
- placement, haPool, err := s.appPlacementRow(accountID, app)
+ // AppPlacement's callers carry no context yet.
+ placement, haPool, err := s.appPlacementRow(context.Background(), accountID, app)
if err != nil {
return "", err
}
@@ -2192,9 +2206,9 @@ func (s *Store) AppPlacement(accountID int64, app string) (string, error) {
}
// appPlacementRow is the row's raw placement column and HA pool, for
-// effectivePlacement; a missing row reads "", "".
-func (s *Store) appPlacementRow(accountID int64, app string) (placement, haPool string, err error) {
- err = s.db.QueryRow(`SELECT placement, ha_pool FROM apps WHERE account_id = ? AND app = ?`, accountID, app).
+// effectivePlacement; a missing row reads "", "". It runs under ctx.
+func (s *Store) appPlacementRow(ctx context.Context, accountID int64, app string) (placement, haPool string, err error) {
+ err = s.db.QueryRowContext(ctx, `SELECT placement, ha_pool FROM apps WHERE account_id = ? AND app = ?`, accountID, app).
Scan(&placement, &haPool)
if errors.Is(err, sql.ErrNoRows) {
return "", "", nil
@@ -2326,8 +2340,8 @@ type TrashedApp struct {
// TrashedAppsIn lists an account's trashed apps, sorted by name — the Trash
// page's listing, and the mirror image of AppsIn's "live only" filter.
-func (s *Store) TrashedAppsIn(accountID int64) ([]TrashedApp, error) {
- rows, err := s.db.Query(`SELECT app, deleted_at, purge_after FROM apps WHERE account_id = ? AND deleted_at != 0 ORDER BY app`,
+func (s *Store) TrashedAppsIn(ctx context.Context, accountID int64) ([]TrashedApp, error) {
+ rows, err := s.db.QueryContext(ctx, `SELECT app, deleted_at, purge_after FROM apps WHERE account_id = ? AND deleted_at != 0 ORDER BY app`,
accountID)
if err != nil {
return nil, err
@@ -2363,10 +2377,16 @@ func (s *Store) AppNameState(accountID int64, app string) (live, trashed bool, e
// HasTrashedAppIn reports whether an account holds a TRASHED row for app —
// the check the claim-once door uses to explain a refusal ("that name is in
-// the trash") rather than just saying no.
+// the trash") rather than just saying no. It is HasTrashedAppInContext for
+// callers with no request to follow.
func (s *Store) HasTrashedAppIn(accountID int64, app string) (bool, error) {
+ return s.HasTrashedAppInContext(context.Background(), accountID, app)
+}
+
+// HasTrashedAppInContext is HasTrashedAppIn under the caller's context.
+func (s *Store) HasTrashedAppInContext(ctx context.Context, accountID int64, app string) (bool, error) {
var n int
- err := s.db.QueryRow(`SELECT COUNT(*) FROM apps WHERE account_id = ? AND app = ? AND deleted_at != 0`,
+ err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM apps WHERE account_id = ? AND app = ? AND deleted_at != 0`,
accountID, app).Scan(&n)
return n > 0, err
}
@@ -3210,9 +3230,15 @@ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, accountID, app, address, activity.Action,
}
// Events returns one account's audit rows for an app, newest first, capped at
-// limit.
+// limit. It is EventsContext for callers with no request to follow.
func (s *Store) Events(accountID int64, app string, limit int) ([]Event, error) {
- rows, err := s.db.Query(`SELECT app, address, action, target_type, target, message, detail, created_at
+ return s.EventsContext(context.Background(), accountID, app, limit)
+}
+
+// EventsContext is Events under the caller's context: the app overview's
+// activity strip reads it.
+func (s *Store) EventsContext(ctx context.Context, accountID int64, app string, limit int) ([]Event, error) {
+ rows, err := s.db.QueryContext(ctx, `SELECT app, address, action, target_type, target, message, detail, created_at
FROM audit WHERE account_id = ? AND app = ? ORDER BY id DESC LIMIT ?`, accountID, app, limit)
if err != nil {
return nil, err
diff --git a/internal/console/store_test.go b/internal/console/store_test.go
index 28dd79e77..3e7e03770 100644
--- a/internal/console/store_test.go
+++ b/internal/console/store_test.go
@@ -459,7 +459,7 @@ func TestTrashAppRowRoundTrip(t *testing.T) {
if has, err := s.HasTrashedAppIn(acct, "zephyr"); err != nil || !has {
t.Fatalf("HasTrashedAppIn after trash = %v %v, want true", has, err)
}
- trashed, err := s.TrashedAppsIn(acct)
+ trashed, err := s.TrashedAppsIn(context.Background(), acct)
if err != nil || len(trashed) != 1 || trashed[0].App != "zephyr" {
t.Fatalf("TrashedAppsIn = %+v %v, want one zephyr row", trashed, err)
}
@@ -476,7 +476,7 @@ func TestTrashAppRowRoundTrip(t *testing.T) {
if err := s.TrashAppRow(acct, "zephyr", later); err != nil {
t.Fatal(err)
}
- trashed, err = s.TrashedAppsIn(acct)
+ trashed, err = s.TrashedAppsIn(context.Background(), acct)
if err != nil || len(trashed) != 1 || !trashed[0].DeletedAt.Equal(time.Unix(now.Unix(), 0).UTC()) {
t.Fatalf("second TrashAppRow moved the timestamp: %+v %v", trashed, err)
}
@@ -707,7 +707,7 @@ func TestReapAccountClearsAndMarks(t *testing.T) {
if apps, err := s.AppsIn(id); err != nil || len(apps) != 0 {
t.Fatalf("AppsIn after reap = %v %v, want empty", apps, err)
}
- if trashed, err := s.TrashedAppsIn(id); err != nil || len(trashed) != 0 {
+ if trashed, err := s.TrashedAppsIn(context.Background(), id); err != nil || len(trashed) != 0 {
t.Fatalf("TrashedAppsIn after reap = %v %v, want empty — reap deletes app rows, it does not trash them", trashed, err)
}
if invites, err := s.PendingInvites(id); err != nil || len(invites) != 0 {
diff --git a/internal/console/tempstore.go b/internal/console/tempstore.go
index 5d940fbf2..a22260aff 100644
--- a/internal/console/tempstore.go
+++ b/internal/console/tempstore.go
@@ -68,8 +68,8 @@ func (s *Store) AppLifeContext(ctx context.Context, accountID int64, app string)
return &l, nil
}
-func (s *Store) appLives(query string, args ...any) ([]AppLife, error) {
- rows, err := s.db.Query(query, args...)
+func (s *Store) appLives(ctx context.Context, query string, args ...any) ([]AppLife, error) {
+ rows, err := s.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
@@ -87,8 +87,8 @@ func (s *Store) appLives(query string, args ...any) ([]AppLife, error) {
// AppLivesIn is every row in the account, trashed included, by name: the
// input to GET /api/cli/apps/{account}/list (spec §7).
-func (s *Store) AppLivesIn(accountID int64) ([]AppLife, error) {
- return s.appLives(`SELECT `+appLifeCols+` FROM apps WHERE account_id = ? ORDER BY app`, accountID)
+func (s *Store) AppLivesIn(ctx context.Context, accountID int64) ([]AppLife, error) {
+ return s.appLives(ctx, `SELECT `+appLifeCols+` FROM apps WHERE account_id = ? ORDER BY app`, accountID)
}
// liveTemporaryWhere is the cap's definition of a live temporary app (spec
@@ -98,7 +98,8 @@ const liveTemporaryWhere = `expires_at > 0 AND (deleted_at = 0 OR restoring = 1)
// LiveTemporaryApps lists the rows the cap counts, soonest expiry first, so
// the refusal can say what each one is and when it goes.
func (s *Store) LiveTemporaryApps(accountID int64) ([]AppLife, error) {
- return s.appLives(`SELECT `+appLifeCols+` FROM apps WHERE account_id = ? AND `+liveTemporaryWhere+` ORDER BY expires_at, app`, accountID)
+ // Its one caller, temporaryCapCheck, carries no context yet.
+ return s.appLives(context.Background(), `SELECT `+appLifeCols+` FROM apps WHERE account_id = ? AND `+liveTemporaryWhere+` ORDER BY expires_at, app`, accountID)
}
// CreateTemporaryAppIn is CreateTemporaryAppInRegion at the home, the way