Skip to content

review: one text per usage and content query, rendered per dialect - #51

Merged
CMGS merged 1 commit into
mainfrom
review/store-sql-dialect-share
Sep 4, 2026
Merged

review: one text per usage and content query, rendered per dialect#51
CMGS merged 1 commit into
mainfrom
review/store-sql-dialect-share

Conversation

@CMGS

@CMGS CMGS commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

What

crates/state/src/store.rs: the six Store methods that were written once for SQLite and once for Postgres (ledger_usage, usage_by_user, usage_series, security_events, content_list_user, and the INSERT of usage_rollup_advance) now share one query text each. The two copies differed only in four dialect tokens — ::BIGINT on SUM, ::text on a nullable parameter, GREATEST vs MAX, ''::text — so each query is a macro_rules! taking the dialect (sql_bigint!, sql_text!, sql_greatest!, sql_opt_eq!, usage_sums!), and the methods live in the shared sql_store_impl! body. usage_rollup_advance keeps its two bodies because Postgres takes an advisory lock; only its INSERT text is shared. content_erase_user, file_put and the batch methods stay dialect-bound (they differ in statements, not tokens).

Net −81 production lines (+322/−403), +88 test lines.

The placeholder trap, handled

The shared text uses numbered ?N placeholders because two of the queries repeat ?1/?2 and bind ?5 before ?4. pg_numbered used to renumber every ? by position, which would have turned ?1 into $11; it now keeps a numbered ?N as $N and only counts up bare ?. Its unit test covers MAX(?3, ?5) AND ?4 OR ?1.

Evidence that the SQL each backend runs is unchanged

  • shared_sql_renders_the_pre_share_text_per_dialect: renders all ten shared texts for both dialects and compares them, whitespace-normalised, with the literal text each backend carried before this change (the expected strings in the test are the pre-share literals).
  • cargo fmt --all -- --check, cargo clippy --workspace --all-targets -- -D warnings, cargo test --workspace: clean on macOS and in the pinned rust:1.98.0 linux/arm64 container (gw-state: 84 passed).
  • Postgres suite executed, not skipped: GW_TEST_PG_URL against a fresh postgres:16-alpine container — postgres_store_roundtrip, pg_rollup_and_erase_roundtrip, postgres_finalize_sees_result_committed_during_lock_wait, postgres_keystore_semantics_mirror_memory, publish_notifies_and_load_returns_latest all pass (84 passed, 0 failed); the run created database gwtest_1788530106228119000 on the server.
  • Comment lines: +3 −4.

ledger_usage, usage_by_user, usage_series, security_events,
content_list_user and the rollup INSERT were spelled once per backend
and differed only in four dialect tokens: the BIGINT cast on SUM, the
::text cast on a nullable parameter, GREATEST versus MAX and ''::text.
Each query is now one macro that takes the dialect; the methods move
into the shared impl body. usage_rollup_advance keeps its two bodies
(Postgres takes an advisory lock) and shares only the INSERT text.

pg_numbered keeps a numbered ?N as $N so the shared text can repeat a
parameter and bind out of order; a bare ? still counts up. A test
renders every shared query for both dialects and compares it,
whitespace-normalised, with the text each backend carried before.
@CMGS
CMGS merged commit 8993b00 into main Sep 4, 2026
2 checks passed
@CMGS
CMGS deleted the review/store-sql-dialect-share branch September 4, 2026 14:24
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.

1 participant