Skip to content

fix(db): tenant scoping for bulk, nested and value-subquery DML shapes - #817

Merged
antosubash merged 5 commits into
mainfrom
fix/db-review-dml-scope
Nov 4, 2026
Merged

antosubash merged 5 commits into
mainfrom
fix/db-review-dml-scope

Conversation

@antosubash

Copy link
Copy Markdown
Owner

Summary

  • Tenant scoping in simple_module_db now applies to more statement shapes:
    • ORM bulk UPDATE by primary key (session.execute(update(Model), rows));
    • update().ordered_values() assigning tenant_id (it now raises, like .values());
    • subqueries in an INSERT's values, multi-row values, RETURNING and on_conflict_do_update() set_ / where;
    • UPDATE / DELETE embedded as a CTE or attached with add_cte();
    • UPDATE / DELETE on an aliased Core table;
    • tables read only through an UPDATE's SET (UPDATE … FROM).
  • Behaviour change: these shapes now raise TenantIsolationError unless run inside all_tenants():
    • an INSERT into a tenant table inside another statement (any install);
    • SQLite INSERT OR REPLACE into a tenant table while a tenant is bound;
    • the legacy Session.bulk_* methods on MultiTenantMixin models while a tenant is bound or strict mode is on.
  • docs/framework/multi-tenancy.md, docs/database/mixins.md, the database skill and the CHANGELOG describe the covered and refused shapes.

Verification

  • Local CI: make lint passed, along with these suites:
    • make test-py: 7647 passed
    • make test-js: 891 passed
    • make doctor: clean
    • e2e: 53 passed against a live API + Vite with the CI module set
  • framework/db/tests on a disposable Postgres database: 698 passed.
  • Not browser-tested: this changes the DB layer only and has no UI.
  • Verification report: https://claude.ai/artifact/6Kc2kGaHk1HusesEyakUJn

Report links are Claude Artifacts and start private; the author can share them from claude.ai/code/artifacts if reviewers need access.

Test plan

  • CI is green, including the Postgres workflow (this PR touches framework/db)
  • The new tests in framework/db/tests/test_db_{dml_target_scope,insert_value_subqueries,nested_dml_and_bulk_api}.py cover each shape

https://claude.ai/code/session_01FXHA3ZicrtZ4C5QPEgxfgq

Tenant scoping now applies to:
- ORM bulk UPDATE by primary key (execute(update(Model), rows)): each row
  gets WHERE tenant_id = <bound>; the session's own-tenant objects are
  synchronized as synchronize_session="evaluate" would;
- update().ordered_values(): assigning tenant_id raises like .values();
- subqueries in an INSERT's values, multi-row values and
  on_conflict_do_update() set_/where, whatever the insert target (strict
  mode with no tenant raises);
- UPDATE / DELETE embedded as a CTE in another statement;
- UPDATE / DELETE on an aliased Core table;
- a table read only through an UPDATE's SET values (UPDATE ... FROM).

Refused outside all_tenants() (TenantIsolationError): an INSERT into a
tenant table embedded as a CTE, SQLite INSERT OR REPLACE into a tenant
table while a tenant is bound, and Session.bulk_save_objects /
bulk_insert_mappings / bulk_update_mappings on a MultiTenantMixin model
while a tenant is bound or strict mode is on.

The predicate factories move from query_filter to scope_predicates.

Claude-Session: https://claude.ai/code/session_01FXHA3ZicrtZ4C5QPEgxfgq
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Deploying simple-module-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: 9ecdebd
Status: ✅  Deploy successful!
Preview URL: https://dc4130b1.simple-module-python.pages.dev
Branch Preview URL: https://fix-db-review-dml-scope.simple-module-python.pages.dev

View logs

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review ✅ Completed 2026-11-04T14:28:18.125027Z 9ecdebd PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@antosubash
antosubash merged commit cbb0f99 into main Nov 4, 2026
17 checks passed
@antosubash
antosubash deleted the fix/db-review-dml-scope branch November 4, 2026 14:32
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