carlos / platform Public

console: index invites by address for KnownAddress (H4-M5 review 1)

KnownAddress runs on every public sign-in request and scanned the
invites table. Add invites_by_address (address, accepted_at,
expires_at) in the store's schema list, with a query-plan test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 5a5642d24b3c66e84cb8f37f48ef1b865ceb63f2 parent 8fdb4e1
2 files changed, +27 −0
  • internal/console/emailsignin_store_test.go +24 −0
  • internal/console/store.go +3 −0
diff --git a/internal/console/emailsignin_store_test.go b/internal/console/emailsignin_store_test.go
index 007f961af..f8f535571 100644
--- a/internal/console/emailsignin_store_test.go
+++ b/internal/console/emailsignin_store_test.go
@@ -6,6 +6,7 @@ import (
"database/sql"
"encoding/hex"
"path/filepath"
+ "strings"
"testing"
"time"
)
@@ -503,3 +504,26 @@ func TestHeldChallengeNeverRedeemsACode(t *testing.T) {
t.Fatalf("CreateEmailChallenge row = %+v, %v; want not held", s, ok)
}
}
+
+// KnownAddress's invite lookup is indexed (H4-M5 review 1): it runs for every
+// public sign-in request.
+func TestKnownAddressInviteLookupIsIndexed(t *testing.T) {
+ st := openTestStore(t)
+ rows, err := st.db.Query(`EXPLAIN QUERY PLAN SELECT 1 FROM invites WHERE address = ? AND accepted_at = 0 AND expires_at >= ?`, "a@example.com", 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer rows.Close()
+ var plan []string
+ for rows.Next() {
+ var id, parent, unused int
+ var detail string
+ if err := rows.Scan(&id, &parent, &unused, &detail); err != nil {
+ t.Fatal(err)
+ }
+ plan = append(plan, detail)
+ }
+ if got := strings.Join(plan, "; "); !strings.Contains(got, "invites_by_address") {
+ t.Fatalf("invite lookup plan = %q, want it to use invites_by_address", got)
+ }
+}
diff --git a/internal/console/store.go b/internal/console/store.go
index 5a0f91b72..3be01b226 100644
--- a/internal/console/store.go
+++ b/internal/console/store.go
@@ -254,6 +254,9 @@ func (s *Store) migrate() error {
expires_at INTEGER NOT NULL,
accepted_at INTEGER NOT NULL DEFAULT 0
);`,
+ // Store.KnownAddress asks, on the public sign-in path, whether an
+ // address has a live invite; without this that is a table scan.
+ `CREATE INDEX IF NOT EXISTS invites_by_address ON invites (address, accepted_at, expires_at);`,
// The alert watcher's memory (alertwatch.go): the firing state the
// console last told ONE operator about, per (box, rule, address). A
// row's absence means "never told them", which is why AlertSeen