From 75c4f31021f60fd5261ec0e7d473f3e2132a3a06 Mon Sep 17 00:00:00 2001 From: dex-the-ai Date: Mon, 23 Nov 2026 02:48:48 +0000 Subject: [PATCH] refactor(relation): route first/last through the execute seam 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 --- lib/couchbase-orm/relation.rb | 32 +++++++++++++--------------- spec/relation_spec.rb | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/lib/couchbase-orm/relation.rb b/lib/couchbase-orm/relation.rb index 927efe8..4e5ef24 100644 --- a/lib/couchbase-orm/relation.rb +++ b/lib/couchbase-orm/relation.rb @@ -20,23 +20,20 @@ def to_s "CouchbaseOrm_Relation: #{@model} where:#{@where.inspect} order:#{@order.inspect} limit: #{@limit} strict_loading: #{@strict_loading}" end + # Inlines condition values into the statement (for display/debugging). def to_n1ql - bucket_name = @model.bucket.name - where = build_where_with_params(nil) - order = build_order - limit = build_limit - "select raw meta().id from `#{bucket_name}` where #{where} order by #{order} #{limit}" + build_select(nil) end + # Binds condition values as positional parameters ($1, $2, ...). def to_n1ql_with_params - bucket_name = @model.bucket.name params = [] - where = build_where_with_params(params) - order = build_order - limit = build_limit - ["select raw meta().id from `#{bucket_name}` where #{where} order by #{order} #{limit}", params] + [build_select(params), params] end + # The single seam through which every relation statement reaches the + # cluster, so query options (N1ql.config merged with #with) and logging + # are applied in one place. def execute(n1ql_query, params = []) result = @model.cluster.query(n1ql_query, build_query_options(positional_parameters: params)) CouchbaseOrm.logger.debug { "Relation query: #{n1ql_query} params: #{params.inspect} return #{result.rows.to_a.length} rows" } @@ -76,17 +73,12 @@ def with(opts = {}) end def first - n1ql_query, params = self.limit(1).to_n1ql_with_params - result = @model.cluster.query(n1ql_query, build_query_options(positional_parameters: params)) - return unless (first_id = result.rows.to_a.first) - - @model.find(first_id, with_strict_loading: @strict_loading) + first_id = limit(1).ids.first + @model.find(first_id, with_strict_loading: @strict_loading) if first_id end def last - n1ql_query, params = to_n1ql_with_params - result = @model.cluster.query(n1ql_query, build_query_options(positional_parameters: params)) - last_id = result.rows.to_a.last + last_id = ids.last @model.find(last_id, with_strict_loading: @strict_loading) if last_id end @@ -172,6 +164,10 @@ def build_limit @limit ? "limit #{@limit}" : "" end + def build_select(params) + "select raw meta().id from `#{@model.bucket.name}` where #{build_where_with_params(params)} order by #{build_order} #{build_limit}" + end + def initializer_arguments { model: @model, order: @order, where: @where, limit: @limit, strict_loading: @strict_loading, query_options: @query_options } end diff --git a/spec/relation_spec.rb b/spec/relation_spec.rb index e9b4b84..824a045 100644 --- a/spec/relation_spec.rb +++ b/spec/relation_spec.rb @@ -534,5 +534,44 @@ def self.active expect(result).to match_array([m1]) end end + + describe "query execution seam" do + let(:relation) { RelationModel.where(active: true).order(age: :asc).with(adhoc: false, client_context_id: "relation-seam-spec") } + + before do + @young = RelationModel.create!(active: true, age: 10) + @old = RelationModel.create!(active: true, age: 30) + RelationModel.create!(active: false, age: 20) + end + + { + ids: ->(r) { r.ids }, + first: ->(r) { r.first }, + last: ->(r) { r.last }, + count: ->(r) { r.count }, + to_a: ->(r) { r.to_a }, + update_all: ->(r) { r.update_all(name: "touched") }, + }.each do |terminal, call| + it "routes #{terminal} through #execute with the relation's query options" do + expect_any_instance_of(CouchbaseOrm::Relation::CouchbaseOrm_Relation).to receive(:execute).once.and_call_original + expect(Couchbase::Options::Query).to receive(:new) + .with(hash_including(adhoc: false, client_context_id: "relation-seam-spec", scan_consistency: :request_plus)) + .once.and_call_original + call.call(relation) + end + end + + it "returns the first and last records" do + expect(relation.first).to eq @young + expect(relation.last).to eq @old + end + + it "builds the same statement with inlined values or positional parameters" do + inlined = relation.to_n1ql + parameterized, params = relation.send(:to_n1ql_with_params) + expect(params).to eq([true]) + expect(parameterized.sub("$1", "true")).to eq(inlined) + end + end end