Repository navigation
refactor(relation): route first/last through the execute seam - #42
Open
dex-the-ai wants to merge 1 commit into
Open
dex-the-ai wants to merge 1 commit into
dex-the-ai wants to merge 1 commit into
Conversation
Relation#first and #last called cluster.query and build_query_options directly instead of going through #execute, so the query-option merge (N1ql.config + #with) and the debug logging lived in three places. They now go through #ids -> #query -> #execute, like every other terminal method. to_n1ql and to_n1ql_with_params now share one private build_select, so the SELECT shape is defined once. Add a spec that checks ids/first/last/count/to_a/update_all each run exactly one statement through #execute with the relation's options. The first/last cases fail on the old code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dex-the-ai
requested review from
pimpin and
prajwal-pai77
and
a balanced review from Copilot
November 23, 2026 02:49
There was a problem hiding this comment.
🟢 Approval recommended
The relation-query execution refactor has no active review issues blocking approval.
No open issues 🎉
What changed in this PR
This PR centralizes relation query execution so first and last use the same query-option handling and debug logging as other terminal methods, building on the prepared-statement support from #36.
Changes:
- Route
firstandlastthroughids→query→execute, preserving existing selection behavior. - Share SELECT statement construction through a private
build_selecthelper. - Add specs for execution routing, query options, record selection, and inline versus parameterized statements.
| File | Description |
|---|---|
| spec/relation_spec.rb | Adds execution-path, query-option, and statement-equivalence coverage. |
| lib/couchbase-orm/relation.rb | Centralizes first/last execution and deduplicates SELECT construction. |
Review effort: Balanced
This branch has not been deployed
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
CouchbaseOrm_Relation#executeis meant to be the one place where a relation statement reaches the cluster. It applies the query options (N1ql.configmerged with#with(...)) and writes the debug log.#firstand#lastdidn't use it. Each called@model.cluster.query(..., build_query_options(...))itself. That left three copies of the same logic, andfirst/lastnever wrote theRelation query:debug line.This PR:
first/lastnow go throughids→query→execute, likecount,to_a,pluck,delete_allandupdate_all. Behaviour is unchanged:firststill addslimit 1, and both still returnnilwhen nothing matches.to_n1ql/to_n1ql_with_paramsshare one privatebuild_select(params), so theselect raw meta().id … where … order by … limitshape is written once. Withparams = nil,to_n1qlstill inlines values for display.Net change in
lib/: −4 lines. No public API change.Why this seam
The adhoc / prepared-statement work (#36) added
query_optionsand#withto the relation. Any later option or logging change should touch onlyexecuteandbuild_query_options. Before this PR,firstandlastwould have to be updated separately, and a missed update would be silent.Tests
New
query execution seamblock inspec/relation_spec.rb:ids,first,last,count,to_aandupdate_all, it checks that exactly one statement goes through#executeand gets the relation's options (adhoc: false,client_context_id, defaultscan_consistency: :request_plus).firstandlastreturn the right records.to_n1qlandto_n1ql_with_paramsbuild the same statement, with only the values bound differently.Against the old
lib/, thefirstandlastseam cases fail and every other case passes. On this branch all of them pass, 5 out of 5 repeated runs.Validation
Ran locally against Couchbase Server Enterprise 7.6.12 in Docker, set up the same way as
ci/run_couchbase.sh(defaultbucket,testeruser,type/rating/nameindexes), on Ruby 3.3.8:Baseline on
masterbefore the change: 397 examples, 0 failures.🤖 Generated with Claude Code