Skip to content

fix(orm): typed MissingTable, ModelOps::find, and an insert-or-ignore that runs - #14

Merged
vheins merged 7 commits into
rustasea:masterfrom
gitfromwildan:fix/orm-missing-builders
Nov 22, 2026
Merged

vheins merged 7 commits into
rustasea:masterfrom
gitfromwildan:fix/orm-missing-builders

Conversation

@gitfromwildan

Copy link
Copy Markdown
Contributor

Summary

Closes three M2 gaps in rustasea-orm: a typed missing-table error, a find shorthand, and an insert-or-ignore that runs and that works on SQLite. Based on master; independent of #13 (a local merge of the two branches has no conflicts).

Changes

Commit Effect
fix(orm): report a missing table as OrmError::MissingTable The variant existed but was never constructed. The sqlx error conversion now recognises each driver's missing-table message and returns MissingTable { table }, without the schema prefix. Every other driver error stays Storage.
feat(orm): add ModelOps::find find(pool, id) loads and hydrates a row, or returns None. It goes through Model::query, so a soft-deleted row is not found; refresh still reads trashed rows.
fix(orm): execute insert-or-ignore, and ignore conflicts on SQLite InsertBuilder::insert_or_ignore(pool) runs the insert and returns whether the row was inserted. The SQLite shape becomes INSERT OR IGNORE, where it was a plain INSERT that failed on a conflict.
docs: record find, insert-or-ignore, and MissingTable in M2 status The M2 section, the data-orm module rows, and a new Fixed changelog section.

Behaviour changes

  • Error type: code that matched OrmError::Storage for a missing table now gets OrmError::MissingTable. Nothing in the workspace did; the CLI's tinker already formats MissingTable.
  • SQLite insert-or-ignore SQL: save_or_ignore_sql("sqlite") and the SQLite branch of ModelScopes::insert_sql (behind Model::insert_sql) return INSERT OR IGNORE.
  • Postgres conflict scope: insert_or_ignore uses ON CONFLICT DO NOTHING with no conflict target, so a conflict on any unique column is skipped, matching MySQL INSERT IGNORE and SQLite INSERT OR IGNORE. The existing SQL-only helpers keep their ON CONFLICT (id) shape.

Testing

On rustc 1.100.0:

  • cargo xtask ci passes.
  • cargo test --workspace --no-fail-fast: 192 suites pass. The one failure locally is generated_app_compiles_for_every_variant, which compiles a generated app offline and stopped on crates missing from my local cargo cache (foldhash, zerocopy-derive, async-tungstenite). This PR does not touch the scaffold; CI runs that test with a full fetch.
  • cargo test -p rustasea-storage --features aws,sftp passes.
  • New SQLite tests: tests/missing_table.rs, tests/model_find.rs, tests/insert_or_ignore.rs, plus a unit test for each driver's missing-table message in error.rs.

Not verified

The Postgres and MySQL paths: the missing-table messages are matched from each driver's documented format and unit-tested as strings, and the ON CONFLICT DO NOTHING / INSERT IGNORE statements were not run against a live server.

Not in this PR

make:migration --create, Model::insert_or_ignore_returning (still returns SQL text only), straight_join choosing its dialect at compile time, and the soft-delete partial index stay open in docs/milestones.md.

@vheins, could you review this?

OrmError::MissingTable existed but was never constructed: every driver error,
including a query against a table that does not exist, became an untyped
OrmError::Storage string.

The sqlx error conversion now recognises the missing-table message of each
driver (SQLite "no such table: t", Postgres 'relation "t" does not exist',
MySQL "Table 'db.t' doesn't exist") and returns MissingTable with the table
name, without its schema prefix. Every other driver error stays Storage.
There was no find shorthand; callers wrote query().where_key(id).first(pool)
and hydrated the row themselves. find(pool, id) does that and returns the
model, or None. It goes through Model::query, so global scopes apply and a
soft-deleted row is not found; refresh still reads trashed rows.
The insert-or-ignore helpers only returned SQL text that nothing executed,
and on SQLite they produced a plain INSERT, so a conflicting row failed
instead of being skipped.

The SQLite shape is now INSERT OR IGNORE (save_or_ignore_sql and the
dialect-aware ModelScopes::insert_sql). InsertBuilder::insert_or_ignore runs
the insert on a pool, choosing the statement by the pool's runtime dialect:
INSERT OR IGNORE, INSERT IGNORE, or ON CONFLICT DO NOTHING with no conflict
target, so a conflict on any unique column is skipped on every driver. It
returns whether the row was inserted.
docs/milestones.md: M2's Done list gains ModelOps::find, the executing
insert_or_ignore, and the typed missing-table error; the Partial item keeps
only the missing make:migration --create flag. Module rows: F-M2-BLD-01 and
F-M2-MIG-04 are Done; the F-M2-BLD-02 and F-M2-MOD-03 notes list what is left
(straight_join's compile-time dialect, the SQL-only
Model::insert_or_ignore_returning, the deleted_at partial index). Changelog
entries go in a new Fixed section.
@github-actions
github-actions Bot requested a review from vheins November 21, 2026 16:40
@vheins

vheins commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Review: PR #14 — typed MissingTable, ModelOps::find, insert-or-ignore that runs

Decision: NEEDS_FIX — one MEDIUM contract/doc issue; the code is otherwise correct and the merge is clean.

Base is 93361c0; origin/master is d7cc118 (#13 ahead). git merge-tree --write-tree reports no conflicts; only CHANGELOG.md and docs/milestones.md overlap, in different hunks. The new tests are real in-memory SQLite integration tests and run in CI through the crate's default sqlite feature.

Verified good

  • ModelOps::find (model_ops.rs:319) goes through Model::query() → first → resolved(), so the soft-delete scope applies; refresh uses query_with_trashed() (model_ops.rs:423-430). Hydration errors propagate; no panic.
  • insert_or_ignore (m2.rs:133-140) uses execute_bind, and rows_affected > 0 is correct for SQLite INSERT OR IGNORE, MySQL INSERT IGNORE, and Postgres ON CONFLICT DO NOTHING. Placeholders are $n and execute_bind adapts them per dialect (db/exec.rs:58-75).
  • MissingTable is now constructed (error.rs:104-115) and consumed by the CLI tinker (rustasea-cli/src/tinker/verbs.rs:197).
  • Tests are non-tautological: insert_or_ignore.rs, model_find.rs, missing_table.rs run against a live in-memory SQLite pool.

MEDIUM — insert_or_ignore contract understates what the drivers suppress

m2.rs:121-127 says it skips "primary-key or unique" conflicts. In fact SQLite INSERT OR IGNORE and MySQL INSERT IGNORE suppress all insert-time constraint/data errors: NOT NULL and CHECK violations skip the row (Ok(false), indistinguishable from a duplicate), and MySQL also coerces/truncates invalid values and inserts them (Ok(true) with altered data). Postgres ON CONFLICT DO NOTHING ignores only unique/exclusion conflicts, so the same input errors on Postgres. A caller using the boolean as "duplicate vs inserted" will misclassify a data error.

Fix: correct the doc to state the per-dialect semantics, warn that this is not a validation path, and add one SQLite test that a NOT NULL violation returns Ok(false) so the behavior is intentional and pinned.

Nits (non-blocking)

  • db/raw.rs:36-37 still says a missing table surfaces as OrmError::Storage; it now returns MissingTable on the path the tinker uses. Update the # Errors doc.
  • error.rs:121-133: message matching is locale/format-sensitive and silently falls back to Storage; a missing sequence/view is labelled "table". sqlx 0.8.6 exposes DatabaseError::code() — check Postgres 42P01 / MySQL 1146 first, keep the message heuristic as fallback, and add an "already exists" false-positive test.
  • m2.rs:112-119 vs :133-140: two Postgres insert-or-ignore shapes (ON CONFLICT (id) vs no target) with similar names. Document the distinction.
  • m2.rs:101-105: or_ignore_sql rewrites the prefix with replacen; if to_sql changes its prefix it silently emits a plain INSERT. Build the prefix directly (as ignore_sql does).
  • docs/milestones.md:150: "on every driver" overstates the evidence; Postgres/MySQL were not run live (your own "Not verified" section). Qualify it.

Security

No secrets. InsertBuilder interpolates table/column names raw into SQL and insert_or_ignore now executes it; no workspace caller passes untrusted identifiers (tests only), but callers must not pass user input. MissingTable carries a driver-supplied name into a display string; no injection.

Claim check

Claim Verdict Evidence
MissingTable recognises each driver's message and strips schema; other errors stay Storage partially_supported error.rs:104-133; unit tests :245-264. No error-code use; locale/wrapped messages fall back to Storage; views/sequences labelled "table".
ModelOps::find respects soft deletes; refresh reads trashed supported model_ops.rs:319-327, :423-430; model.rs:346-357; builder/exec.rs:178-219; test tests/model_find.rs:44-57.
insert_or_ignore executes, returns inserted; SQLite INSERT OR IGNORE; PG no-target; MySQL INSERT IGNORE; helpers keep ON CONFLICT (id) supported m2.rs:133-140, :112-119; db/exec.rs:58-75. Doc conflict-only claim is inaccurate.
New SQLite tests + per-driver unit test supported tests/{missing_table,model_find,insert_or_ignore}.rs; error.rs:245-264.
Based on master; independent of #13; local merge has no conflicts supported git merge-tree --write-tree → d81b5bd, exit 0.
cargo xtask ci passes; 192 suites pass; generated_app_compiles_for_every_variant local failure is a cache issue partially_supported Test is rustasea-scaffold/tests/generated_compiles.rs:168, not in the diff, runs cargo check --offline (:134-142); CI runs cargo fetch first. Not executed here (no-build constraint).
Nothing matched Storage for a missing table; tinker already formats MissingTable supported git grep OrmError::Storage; tinker/verbs.rs:197.
Postgres/MySQL not run live supported New tests use sqlite::memory: only.
Model::insert_or_ignore_returning still SQL text only supported model.rs:425; model_sql.rs:24-25.

Verdict: fix the insert_or_ignore doc contract and the db/raw.rs doc, then this is good to merge.

- Detect a missing table from the structured driver code first
  Classify Postgres SQLSTATE 42P01 and MySQL SQLSTATE 42S02 (ER_NO_SUCH_TABLE,
  error number 1146) before parsing the message; keep the heuristic as the
  SQLite/fallback path and make it tolerate a driver-wrapped prefix. Add unit
  tests for the already-exists and wrapped-message cases.
- Harden the insert-or-ignore SQL and its documented contract
  Replace the fragile INSERT INTO string rewrite with a shared keyword-based
  builder, correct the insert_or_ignore doc to the real per-dialect suppression
  semantics, and distinguish save_or_ignore_sql (PK-targeted SQL text) from the
  executing insert_or_ignore. Add a SQLite test pinning that a NOT NULL
  violation returns Ok(false).
- Align the raw execution errors and M2 milestone claim with the code
  query_raw/execute_raw now document a missing table as OrmError::MissingTable
  with syntax/type errors staying Storage, and the milestone note qualifies that
  only SQLite is tested end-to-end for insert-or-ignore.
@vheins
vheins merged commit 86269ea into rustasea:master Nov 22, 2026
6 checks passed
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.

2 participants