Skip to content

Introduce Storage trait abstracting durable-runtime DB queries#101

Merged
thinkingfish merged 6 commits intomainfrom
claude/durable-sqlite-polling-eogdl
May 5, 2026
Merged

Introduce Storage trait abstracting durable-runtime DB queries#101
thinkingfish merged 6 commits intomainfrom
claude/durable-sqlite-polling-eogdl

Conversation

@thinkingfish
Copy link
Copy Markdown
Member

@thinkingfish thinkingfish commented May 5, 2026

Summary

Phase 1 of the storage abstraction discussed in https://claude.ai/code/session_013icXPFch1CZdPwMb5Mmu1S — a mechanical move of every sqlx::query! against the durable schema in worker.rs, task.rs, and plugin/durable/notify.rs behind a new Storage trait, with a PgStorage impl holding the existing query bodies verbatim.

The trait exists so a future swappable backend (e.g. SQLite with a polling executor) can be added without further restructuring. For now it is pub(crate) and Postgres-typed.

What this PR does NOT change

  • Transaction lifecycle stays at the call sites (pool.begin(), pool.acquire(), commit/rollback). Trait methods take a borrowed &mut sqlx::PgConnection, which works for both pooled connections and transactions via sqlx's Executor blanket impl.
  • No SQL changes. Every method body is a verbatim move of the SQL that previously lived inline.
  • No semantic changes. enter/exit decision logic, error handling paths, and the commit_event_with_log CTE all behave identically.

Notes for review

  • 36 inline query call sites collapse to ~22 trait methods (a few queries appear at multiple call sites).
  • TaskState enum (the durable.task_state Postgres enum mapping) moved from plugin/durable/notify.rs into storage::mod so both the runtime and the notify plugin share it.
  • SharedState gains an Arc<dyn Storage> field next to the existing pool: PgPool. Both reference the same pool. Phase 2 can collapse to one.
  • .sqlx/ was tidied as part of this PR: 18 new entries added for queries whose surrounding indentation changed when they moved into trait-method bodies, 21 unused entries deleted (the now-orphaned old strings plus 3 pre-existing orphans), and 1 missing entry added for SELECT state = 'suspended' as "state!" used at 5 call sites in durable-test/tests/it/{notify,dst_notify}.rs — so cargo build --workspace --tests is green under SQLX_OFFLINE=true for the first time on this branch. After this PR, every .sqlx/ entry maps to a query in the workspace and every macro-checked query has a matching entry.
  • Doc comments on the trait describe why a method exists (constraints, why two suspend variants exist, why commit_event_with_log is one CTE, etc.); methods whose names already convey their behaviour are left undocumented. No comment leaks impl-specific constants.

Out of scope

  • The workflow-visible SQL host under plugin/durable/sql/ — that's a separate concern (the wasm guest's SQL dialect).
  • The LISTEN/NOTIFY → polling executor swap itself. The EventSource trait already exists for that swap; this PR doesn't touch it.
  • Phase 2 of the storage abstraction (associated Conn/Tx types, Storage owning transaction lifecycle, removing pool: PgPool from SharedState).

Test plan

  • cargo check -p durable-runtime --all-targets clean
  • cargo +nightly fmt --check clean
  • cargo clippy -p durable-runtime --all-targets clean
  • cargo test -p durable-runtime --lib passes (15/15)
  • cargo build --workspace --tests succeeds under SQLX_OFFLINE=true

🤖 Generated with Claude Code

claude added 2 commits May 5, 2026 06:17
Move every sqlx query against the durable schema in `worker.rs`, `task.rs`,
and `plugin/durable/notify.rs` behind a new `Storage` trait, with a
`PgStorage` impl that holds the existing query bodies verbatim. Transaction
lifecycle (`pool.begin()`, `pool.acquire()`, commit/rollback) stays at the
call sites — methods take a borrowed `&mut sqlx::PgConnection`, which works
for both pooled connections and transactions via sqlx's `Executor` blanket
impl.

This is a mechanical refactor with no functional or behavioural changes.
The trait is the scaffolding for a future swappable backend (e.g. SQLite
with a polling executor); for now it is `pub(crate)` and Postgres-typed.

The new `.sqlx/query-*.json` files are regenerated cache entries for the
queries whose surrounding code indentation changed when they moved into
trait-method bodies. Their describe info is identical to the previous
entries — only the literal whitespace in the SQL string differs.
Comment thread crates/durable-runtime/src/plugin/durable/notify.rs Fixed
Comment thread crates/durable-runtime/src/plugin/durable/notify.rs Fixed
Comment thread crates/durable-runtime/src/plugin/durable/notify.rs Fixed
Comment thread crates/durable-runtime/src/plugin/durable/notify.rs Fixed
claude added 4 commits May 5, 2026 06:30
Use auto-deref for &mut Transaction at four call sites in plugin/durable/notify.rs (clippy::explicit_auto_deref) and replace a match with Option::unwrap_or in worker.rs::load_leader_id (clippy::manual_unwrap_or).
* Add the missing cache entry for `SELECT state = 'suspended' as "state!"`,
  used at 5 call sites in `durable-test/tests/it/{notify,dst_notify}.rs`.
  Without it, those tests failed to compile under `SQLX_OFFLINE=true`.
* Delete 21 unused cache entries left over from the storage refactor.
  Most were entries for the old SQL strings whose surrounding indentation
  changed when the queries moved into trait-method bodies in
  `crates/durable-runtime/src/storage/pg.rs`; the rest were pre-existing
  orphans.

After this commit, every entry under `.sqlx/` matches a query somewhere in
the workspace and every macro-checked query has a matching entry.
Per the project's commenting policy, remove doc comments that just restate the method or type name. Keep ones that explain why behaviour exists, document constraints, or describe non-obvious return semantics.
@thinkingfish thinkingfish marked this pull request as ready for review May 5, 2026 18:45
@thinkingfish thinkingfish merged commit 43389e3 into main May 5, 2026
7 checks passed
@thinkingfish thinkingfish deleted the claude/durable-sqlite-polling-eogdl branch May 5, 2026 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants