carlos / platform Public

console: plan lapse items for keep choices and what is turned off (plans 4d)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 529a7dfe129577a7ac613e5de5b45a9139acda56 parent 4beb75e
4 files changed, +229 −1
  • internal/console/planlapse_store.go +115 −0
  • internal/console/planlapse_store_test.go +108 −0
  • internal/console/store.go +5 −1
  • internal/console/store_gate.go +1 −0
diff --git a/internal/console/planlapse_store.go b/internal/console/planlapse_store.go
new file mode 100644
index 000000000..743d9ec7a
--- /dev/null
+++ b/internal/console/planlapse_store.go
@@ -0,0 +1,115 @@
+package console
+
+import (
+ "context"
+ "time"
+)
+
+// Plans 4d's own table, kept here so the feature is one file to read: one
+// row per thing a lapse governs that an owner chose to keep or the grace
+// pass turned off. thing_created ties a row to one incarnation, so a
+// recreated thing of the same name is a new thing.
+func (s *Store) migratePlanLapse() error {
+ _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS plan_lapse_items (
+ account_id INTEGER NOT NULL,
+ app TEXT NOT NULL,
+ kind TEXT NOT NULL,
+ ref TEXT NOT NULL,
+ thing_created INTEGER NOT NULL,
+ keep INTEGER NOT NULL DEFAULT 0,
+ disabled_at INTEGER NOT NULL DEFAULT 0,
+ updated_at INTEGER NOT NULL,
+ PRIMARY KEY (account_id, kind, ref)
+);`)
+ return err
+}
+
+// lapseItem is one plan_lapse_items row.
+type lapseItem struct {
+ AccountID int64
+ App string // the owning app (a domain's claim app)
+ Kind string // lapseApp | lapseInstance | lapseSidecar | lapseDomain
+ Ref string // the app name, or the host
+ ThingCreated time.Time // the thing's own creation time: a recreated thing is a new thing
+ Keep bool
+ DisabledAt time.Time // zero = on
+ UpdatedAt time.Time
+}
+
+// LapseItems is every lapse item row for the account.
+func (s *Store) LapseItems(accountID int64) ([]lapseItem, error) {
+ rows, err := s.db.Query(`SELECT app, kind, ref, thing_created, keep, disabled_at, updated_at
+ FROM plan_lapse_items WHERE account_id = ? ORDER BY kind, ref`, accountID)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ var out []lapseItem
+ for rows.Next() {
+ it := lapseItem{AccountID: accountID}
+ var created, disabled, updated int64
+ var keep int
+ if err := rows.Scan(&it.App, &it.Kind, &it.Ref, &created, &keep, &disabled, &updated); err != nil {
+ return nil, err
+ }
+ it.ThingCreated, it.DisabledAt, it.UpdatedAt = timeOrZero(created), timeOrZero(disabled), timeOrZero(updated)
+ it.Keep = keep != 0
+ out = append(out, it)
+ }
+ return out, rows.Err()
+}
+
+func (s *Store) claimRawPutLapseItem(it lapseItem) error {
+ keep := 0
+ if it.Keep {
+ keep = 1
+ }
+ _, err := s.db.Exec(`INSERT INTO plan_lapse_items
+ (account_id, app, kind, ref, thing_created, keep, disabled_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
+ ON CONFLICT(account_id, kind, ref) DO UPDATE SET app = excluded.app, thing_created = excluded.thing_created,
+ keep = excluded.keep, disabled_at = excluded.disabled_at, updated_at = excluded.updated_at`,
+ it.AccountID, it.App, it.Kind, it.Ref, unixOrZero(it.ThingCreated), keep, unixOrZero(it.DisabledAt), unixOrZero(it.UpdatedAt))
+ return err
+}
+
+func (s *Store) claimRawDeleteLapseItem(accountID int64, app, kind, ref string) error {
+ _, err := s.db.Exec(`DELETE FROM plan_lapse_items WHERE account_id = ? AND app = ? AND kind = ? AND ref = ?`,
+ accountID, app, kind, ref)
+ return err
+}
+
+// PutLapseItem upserts it on (account, kind, ref), admitted on its app.
+func (s *Store) PutLapseItem(claimContext context.Context, it lapseItem) (claimResult0 error) {
+ claimResult0 = s.withAutoAppSQL(claimContext, it.AccountID, it.App, "sql.PutLapseItem", func(claimView *Store) error {
+ return claimView.claimRawPutLapseItem(it)
+ })
+ return
+}
+
+// DeleteLapseItem removes one lapse item row, admitted on its app.
+func (s *Store) DeleteLapseItem(claimContext context.Context, accountID int64, app, kind, ref string) (claimResult0 error) {
+ claimResult0 = s.withAutoAppSQL(claimContext, accountID, app, "sql.DeleteLapseItem", func(claimView *Store) error {
+ return claimView.claimRawDeleteLapseItem(accountID, app, kind, ref)
+ })
+ return
+}
+
+// LiveAppsCreated is the account's running apps (runningThings' live set:
+// live rows and rows being restored) with their creation times.
+func (s *Store) LiveAppsCreated(accountID int64) (map[string]time.Time, error) {
+ rows, err := s.db.Query(`SELECT app, created_at FROM apps WHERE account_id = ? AND (deleted_at = 0 OR restoring = 1)`, accountID)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ out := map[string]time.Time{}
+ for rows.Next() {
+ var app string
+ var created int64
+ if err := rows.Scan(&app, &created); err != nil {
+ return nil, err
+ }
+ out[app] = time.Unix(created, 0).UTC()
+ }
+ return out, rows.Err()
+}
diff --git a/internal/console/planlapse_store_test.go b/internal/console/planlapse_store_test.go
new file mode 100644
index 000000000..af84f59c2
--- /dev/null
+++ b/internal/console/planlapse_store_test.go
@@ -0,0 +1,108 @@
+package console
+
+import (
+ "context"
+ "reflect"
+ "testing"
+ "time"
+)
+
+func TestLapseItemRoundTrip(t *testing.T) {
+ st, ids := planStore(t, "bab")
+ id := ids["bab"]
+ ctx := context.Background()
+ created := planNow.Add(-48*time.Hour + 500*time.Millisecond)
+ it := lapseItem{AccountID: id, App: "shop", Kind: "instance", Ref: "shop-1.example.com", ThingCreated: created, UpdatedAt: planNow}
+ if err := st.PutLapseItem(ctx, it); err != nil {
+ t.Fatal(err)
+ }
+ if err := st.PutLapseItem(ctx, lapseItem{AccountID: id, App: "shop", Kind: "app", Ref: "shop", ThingCreated: created, Keep: true, UpdatedAt: planNow}); err != nil {
+ t.Fatal(err)
+ }
+ got, err := st.LapseItems(id)
+ if err != nil || len(got) != 2 {
+ t.Fatalf("items = %+v, %v", got, err)
+ }
+ want := it
+ want.ThingCreated = created.Truncate(time.Second).UTC()
+ want.UpdatedAt = planNow.UTC()
+ var inst lapseItem
+ for _, g := range got {
+ if g.Kind == "instance" {
+ inst = g
+ }
+ }
+ if !reflect.DeepEqual(inst, want) {
+ t.Fatalf("instance item = %+v, want %+v", inst, want)
+ }
+ it.Keep, it.DisabledAt, it.UpdatedAt = true, planNow, planNow.Add(time.Hour)
+ if err := st.PutLapseItem(ctx, it); err != nil {
+ t.Fatal(err)
+ }
+ got, _ = st.LapseItems(id)
+ for _, g := range got {
+ if g.Kind == "instance" && (!g.Keep || !g.DisabledAt.Equal(planNow) || !g.UpdatedAt.Equal(planNow.Add(time.Hour))) {
+ t.Fatalf("updated item = %+v", g)
+ }
+ }
+ if err := st.DeleteLapseItem(ctx, id, "shop", "instance", "shop-1.example.com"); err != nil {
+ t.Fatal(err)
+ }
+ if got, _ = st.LapseItems(id); len(got) != 1 || got[0].Kind != "app" || !got[0].Keep {
+ t.Fatalf("after delete = %+v", got)
+ }
+}
+
+func TestLiveAppsCreated(t *testing.T) {
+ st, ids := planStore(t, "bab")
+ id := ids["bab"]
+ for i, app := range []string{"live", "restoring", "trashed"} {
+ if _, err := st.CreateAppIn(id, app, "o@example.com", planNow.Add(time.Duration(i)*time.Hour)); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if _, err := st.db.Exec(`UPDATE apps SET deleted_at = ?, restoring = 1 WHERE app = 'restoring'`, planNow.Unix()); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := st.db.Exec(`UPDATE apps SET deleted_at = ? WHERE app = 'trashed'`, planNow.Unix()); err != nil {
+ t.Fatal(err)
+ }
+ got, err := st.LiveAppsCreated(id)
+ if err != nil {
+ t.Fatal(err)
+ }
+ want := map[string]time.Time{"live": planNow.UTC(), "restoring": planNow.Add(time.Hour).UTC()}
+ if len(got) != len(want) {
+ t.Fatalf("got %v, want %v", got, want)
+ }
+ for app, at := range want {
+ if !got[app].Equal(at) {
+ t.Fatalf("%s created %v, want %v (all %v)", app, got[app], at, got)
+ }
+ }
+}
+
+func TestPutLapseItemGoesThroughTheAppGate(t *testing.T) {
+ s, _, _ := sqlIngestFixture(t)
+ _, err := s.db.Exec(`INSERT INTO plan_lapse_items (account_id, app, kind, ref, thing_created, updated_at)
+ VALUES (7, 'blog', 'app', 'blog', 1, 1)`)
+ requireDenied(t, "raw plan_lapse_items insert", err)
+ it := lapseItem{AccountID: gateAcct, App: "blog", Kind: "app", Ref: "blog", ThingCreated: planNow, Keep: true, UpdatedAt: planNow}
+ if err := s.PutLapseItem(t.Context(), it); err != nil {
+ t.Fatalf("PutLapseItem under the gate: %v", err)
+ }
+ if err := s.DeleteLapseItem(t.Context(), gateAcct, "blog", "app", "blog"); err != nil {
+ t.Fatalf("DeleteLapseItem under the gate: %v", err)
+ }
+}
+
+func TestClaimSQLCensusAcceptsPlanLapseItems(t *testing.T) {
+ s := openGatedStore(t)
+ present, err := claimSQLCensus(s.bootDB(), claimSQLTables)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if sp, ok := present["plan_lapse_items"]; !ok || sp.NonApp != "" || sp.App != "app" || sp.Account != "account_id" {
+ t.Fatalf("plan_lapse_items classified as %+v (present %v)", sp, ok)
+ }
+}
diff --git a/internal/console/store.go b/internal/console/store.go
index 4cd11da45..adb27026f 100644
--- a/internal/console/store.go
+++ b/internal/console/store.go
@@ -556,7 +556,11 @@ func (s *Store) migrate() error {
return err
}
// Plans 4a's cap state (plangrace_store.go), for the same reason.
- return s.migratePlanGrace()
+ if err := s.migratePlanGrace(); err != nil {
+ return err
+ }
+ // Plans 4d's lapse items (planlapse_store.go), for the same reason.
+ return s.migratePlanLapse()
}
// mailCountsBackfillVersion is the user_version this migration stamps. The
diff --git a/internal/console/store_gate.go b/internal/console/store_gate.go
index 5e1311baa..99ea9793f 100644
--- a/internal/console/store_gate.go
+++ b/internal/console/store_gate.go
@@ -191,6 +191,7 @@ var claimSQLTables = map[string]claimSQLTable{
"cli_tokens": {Account: "account_id", App: "app", FreeWhenNoApp: true}, // user tokens carry app ''
"door_keys": {Account: "account_id", App: "app", PurgeMonotone: "revoked_at"},
"door_key_revisions": {Account: "account_id", App: "app", PurgeMonotone: "revision"},
+ "plan_lapse_items": {Account: "account_id", App: "app"},
"pending_changes": {Account: "account_id", App: "app"},
"pending_confirms": {Account: "account_id", App: "app", Via: &claimSQLVia{Parent: "pending_changes", ChildCol: "change_id", ParentCol: "id"}},
"mail_counts": {Sqid: "account", App: "app"},