Implement ORM global scopes (registration + without_global_scopes + get() wiring)#165
Open
tmgbedu wants to merge 1 commit into
Open
Implement ORM global scopes (registration + without_global_scopes + get() wiring)#165tmgbedu wants to merge 1 commit into
tmgbedu wants to merge 1 commit into
Conversation
…() wiring Add a declarative and programmatic global-scope system to the async ORM: - Model.__global_scopes__ declaration, scope_* method discovery, and Model.add_global_scope(name, callback, action) all feed a booted per-instance _global_scopes that set_model propagates to the QueryBuilder. - QueryBuilder.without_global_scopes() (plus a Model classmethod) clears registered scopes, and QueryBuilder gains table()/connection_name helpers. - QueryBuilder.get() now applies scopes via an idempotent run_scopes(), matching to_sql()/to_qmark() without double-applying wheres. - Normalize BelongsToMany pivot chains onto Pivot.on(name).table(t), and make QueryBuilder.create() honor an explicit table override so pivot inserts land on the right table instead of the model default. Covered by a real sqlite harness: declared scopes filter, scope_* discovery, runtime registration, without_global_scopes() bypass (mutation-checked), and a BelongsToMany attach pivot-insert path.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements global scopes for the async masoniteorm layer (task #777).
1. Registration API
Model.__global_scopes__— declarative{action: {name: callable(builder)}}map.scope_*discovery — anyscope_<name>(self, query)method is auto-registered as a select scope.Model.add_global_scope(name, callback, action="select")— programmatic, copy-on-write per class so subclasses never mutate a shared dict.All three feed
Model._boot_global_scopes()(merged across the MRO) into the instance_global_scopes, whichQueryBuilder.set_model()already propagates to the builder.2.
without_global_scopes()QueryBuilder(clears registered scopes, returns self) and exposed as aModelclassmethod.relationships/BelongsToMany.pythat previously raisedAttributeError. Also addedQueryBuilder.table()/connection_nameandModel.table(), and normalized the pivot chains ontoPivot.on(name).table(t).3.
get()applies scopesQueryBuilder.get()now callsrun_scopes()(the# TODO: apply scopes), matchingto_sql()/to_qmark().run_scopes()is made idempotent so scopes are applied exactly once regardless of entry point (no duplicateWHEREs).QueryBuilder.create()honors an explicit table override so pivot inserts land on the pivot table rather than the model default.Tests
New real-sqlite suite
tests/masoniteorm/sqlite/models/test_global_scopes.py:get(); scope is compiled into SQLwithout_global_scopes()bypass (mutation-checked: 2 vs 3 rows)Model.without_global_scopes()helperscope_*discovery filters + bypassesadd_global_scope()registration (mutation-checked)BelongsToMany.attachpivot-insert path writes to the correct pivot tableVerification
uv run pytest --ignore=tests/masoniteorm/postgres --cov: 1784 passed, 7 skipped, total coverage 75.96% (≥ 68%).ruff checkandruff format: clean.