Skip to content

Refactor SQLPage functions into convention-driven modules#1328

Merged
lovasoa merged 2 commits into
mainfrom
split-functions.rs-into-multiple-files
Jun 12, 2026
Merged

Refactor SQLPage functions into convention-driven modules#1328
lovasoa merged 2 commits into
mainfrom
split-functions.rs-into-multiple-files

Conversation

@lovasoa

@lovasoa lovasoa commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Motivation

  • Reduce the cognitive load and risk of regressions from a 1,161-line monolithic functions.rs by isolating unrelated and security-sensitive implementations into their own files.
  • Make adding a new built-in SQL function trivial and deterministic by using convention-over-configuration and build-time discovery.
  • Eliminate duplication in the HTTP fetch pipeline used by fetch and fetch_with_meta.

Description

  • Split the previous single-file implementation into one Rust file per built-in SQL function under src/webserver/database/sqlpage_functions/functions/ and added README.md documenting the one-file-per-function convention and required marker comment // sqlpage-function: ....
  • Implemented build-time discovery in build.rs (generate_sqlpage_functions) that scans functions/, validates each file's marker against its filename, generates module use lines and the sqlpage_functions! registry into OUT_DIR and the lightweight functions.rs now includes the generated registry (include!(concat!(env!("OUT_DIR"), "/sqlpage_functions.rs"));).
  • Moved implementations and their unit tests (where present) into dedicated files for topical isolation (examples: exec.rs, run_sql.rs, hash_password.rs, hmac.rs, fetch.rs, fetch_with_meta.rs, etc.).
  • Deduplicated and shared the HTTP request pipeline by extracting span creation, client/request construction, and body preparation helpers used by both fetch and fetch_with_meta (helpers live alongside fetch).
  • Kept the public function surface unchanged by generating the centralized registry which preserves parameter conversion and dispatch behavior.

Testing

  • Ran formatter: cargo fmt --all -- --check and it passed.
  • Ran unit and integration tests: cargo test and all tests passed (reported run: 147 unit tests and 70 integration tests passed).
  • Ran linting: cargo clippy --all-targets --all-features -- -D warnings and it passed.
  • Performed a consistency check that the generated set of function modules matches the previous registry (validated 37 function modules).

Codex Task

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@lovasoa lovasoa force-pushed the split-functions.rs-into-multiple-files branch from 9fd55e8 to 0efbbfa Compare June 12, 2026 14:02
Each built-in `sqlpage.*` function is now a plain `async fn` in its own
file under `sqlpage_functions/functions/`, with an ordinary Rust signature
and no marker comments or macros inside it.

Registration is automatic: `build.rs` lists the files in `functions/` into a
`sqlpage_functions!` call (the only generated code, one line per function),
and that macro declares the modules and builds the `SqlPageFunctionName`
enum the SQL engine dispatches on. There is no marker-comment parsing and no
per-argument codegen. Argument extraction, dispatch and return-value
conversion are ordinary generic code in function_traits.rs (`Extract`,
`Handler`, `IntoCowResult`), using the same `Fn`-arity trick axum uses for
handlers, so a function's argument and return types are read straight from
its signature.

Also folds in main's FileAccess centralization (#1327) for the file-reading
functions.

Verified: `cargo test` passes (148 lib + 70 integration tests, including
run_all_sql_test_files, the hmac webhook tests, the upload tests and
test_file_upload_through_runsql which exercises the &mut DbConn path).
`cargo clippy` and `cargo fmt --check` are clean.
@lovasoa lovasoa force-pushed the split-functions.rs-into-multiple-files branch from 0efbbfa to d3673c5 Compare June 12, 2026 15:03
@lovasoa lovasoa merged commit 2e39331 into main Jun 12, 2026
51 checks passed
@lovasoa lovasoa deleted the split-functions.rs-into-multiple-files branch June 12, 2026 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant