Repository navigation
fix(orm): typed MissingTable, ModelOps::find, and an insert-or-ignore that runs - #14
Conversation
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.
Review: PR #14 — typed
|
| 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.
Summary
Closes three M2 gaps in
rustasea-orm: a typed missing-table error, afindshorthand, and an insert-or-ignore that runs and that works on SQLite. Based onmaster; independent of #13 (a local merge of the two branches has no conflicts).Changes
fix(orm): report a missing table as OrmError::MissingTablesqlxerror conversion now recognises each driver's missing-table message and returnsMissingTable { table }, without the schema prefix. Every other driver error staysStorage.feat(orm): add ModelOps::findfind(pool, id)loads and hydrates a row, or returnsNone. It goes throughModel::query, so a soft-deleted row is not found;refreshstill reads trashed rows.fix(orm): execute insert-or-ignore, and ignore conflicts on SQLiteInsertBuilder::insert_or_ignore(pool)runs the insert and returns whether the row was inserted. The SQLite shape becomesINSERT OR IGNORE, where it was a plainINSERTthat failed on a conflict.docs: record find, insert-or-ignore, and MissingTable in M2 statusFixedchangelog section.Behaviour changes
OrmError::Storagefor a missing table now getsOrmError::MissingTable. Nothing in the workspace did; the CLI's tinker already formatsMissingTable.save_or_ignore_sql("sqlite")and the SQLite branch ofModelScopes::insert_sql(behindModel::insert_sql) returnINSERT OR IGNORE.insert_or_ignoreusesON CONFLICT DO NOTHINGwith no conflict target, so a conflict on any unique column is skipped, matching MySQLINSERT IGNOREand SQLiteINSERT OR IGNORE. The existing SQL-only helpers keep theirON CONFLICT (id)shape.Testing
On rustc 1.100.0:
cargo xtask cipasses.cargo test --workspace --no-fail-fast: 192 suites pass. The one failure locally isgenerated_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,sftppasses.tests/missing_table.rs,tests/model_find.rs,tests/insert_or_ignore.rs, plus a unit test for each driver's missing-table message inerror.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 IGNOREstatements 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_joinchoosing its dialect at compile time, and the soft-delete partial index stay open indocs/milestones.md.@vheins, could you review this?