diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index 0f4d98cd..89cff1c3 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -123,7 +123,6 @@ class Document extend Enum define_model_callbacks :initialize, :only => :after - define_model_callbacks :create, :destroy, :save, :update Metadata = Struct.new(:cas) @@ -212,6 +211,8 @@ class Base < Document extend Index extend IgnoredProperties + define_model_callbacks :create, :destroy, :save, :update + class << self def connect(**options) @bucket = BucketProxy.new(::MTLibcouchbase::Bucket.new(**options)) diff --git a/lib/couchbase-orm/n1ql.rb b/lib/couchbase-orm/n1ql.rb index 352ed30d..4dd5fe2d 100644 --- a/lib/couchbase-orm/n1ql.rb +++ b/lib/couchbase-orm/n1ql.rb @@ -8,6 +8,7 @@ module CouchbaseOrm module N1ql extend ActiveSupport::Concern NO_VALUE = :no_value_specified + DEFAULT_SCAN_CONSISTENCY = :request_plus # sanitize for injection query def self.sanitize(value) if value.is_a?(String) @@ -19,6 +20,13 @@ def self.sanitize(value) end end + def self.config(new_config = nil) + Thread.current['__couchbaseorm_n1ql_config__'] = new_config if new_config + Thread.current['__couchbaseorm_n1ql_config__'] || { + scan_consistency: DEFAULT_SCAN_CONSISTENCY + } + end + module ClassMethods # Defines a query N1QL for the model # @@ -49,7 +57,7 @@ def n1ql(name, query_fn: nil, emit_key: [], custom_order: nil, **options) @indexes[name] = method_opts singleton_class.__send__(:define_method, name) do |key: NO_VALUE, **opts, &result_modifier| - opts = options.merge(opts).reverse_merge(scan_consistency: :request_plus) + opts = options.merge(opts).reverse_merge(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency]) values = key == NO_VALUE ? NO_VALUE : convert_values(method_opts[:emit_key], key) current_query = run_query(method_opts[:emit_key], values, query_fn, custom_order: custom_order, **opts.except(:include_docs, :key)) if result_modifier @@ -116,7 +124,7 @@ def run_query(keys, values, query_fn, custom_order: nil, descending: false, limi limit = build_limit(limit) n1ql_query = "select raw meta().id from `#{bucket_name}` where #{where} order by #{order} #{limit}" result = cluster.query(n1ql_query, Couchbase::Options::Query.new(**options)) - CouchbaseOrm.logger.debug { "N1QL query: #{n1ql_query} return #{result.rows.to_a.length} rows" } + CouchbaseOrm.logger.debug "N1QL query: #{n1ql_query} return #{result.rows.to_a.length} rows with scan_consistency : #{options[:scan_consistency]}" N1qlProxy.new(result) end end diff --git a/lib/couchbase-orm/relation.rb b/lib/couchbase-orm/relation.rb index 8a92ece7..99d76a2e 100644 --- a/lib/couchbase-orm/relation.rb +++ b/lib/couchbase-orm/relation.rb @@ -27,8 +27,8 @@ def to_n1ql end def execute(n1ql_query) - result = @model.cluster.query(n1ql_query, Couchbase::Options::Query.new(scan_consistency: :request_plus)) - CouchbaseOrm.logger.debug { "Relation query: #{n1ql_query} return #{result.rows.to_a.length} rows" } + result = @model.cluster.query(n1ql_query, Couchbase::Options::Query.new(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency])) + CouchbaseOrm.logger.debug { "Relation query: #{n1ql_query} return #{result.rows.to_a.length} rows with scan_consistency : #{CouchbaseOrm::N1ql.config[:scan_consistency]}" } N1qlProxy.new(result) end @@ -52,13 +52,13 @@ def ids end def first - result = @model.cluster.query(self.limit(1).to_n1ql, Couchbase::Options::Query.new(scan_consistency: :request_plus)) + result = @model.cluster.query(self.limit(1).to_n1ql, Couchbase::Options::Query.new(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency])) first_id = result.rows.to_a.first @model.find(first_id) if first_id end def last - result = @model.cluster.query(to_n1ql, Couchbase::Options::Query.new(scan_consistency: :request_plus)) + result = @model.cluster.query(to_n1ql, Couchbase::Options::Query.new(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency])) last_id = result.rows.to_a.last @model.find(last_id) if last_id end diff --git a/lib/couchbase-orm/views.rb b/lib/couchbase-orm/views.rb index b2e55493..20135086 100644 --- a/lib/couchbase-orm/views.rb +++ b/lib/couchbase-orm/views.rb @@ -66,7 +66,7 @@ def view(name, map: nil, emit_key: nil, reduce: nil, **options) @views[name] = method_opts singleton_class.__send__(:define_method, name) do |**opts, &result_modifier| - opts = options.merge(opts).reverse_merge(scan_consistency: :request_plus) + opts = options.merge(opts).reverse_merge(scan_consistency: CouchbaseOrm::N1ql.config[:scan_consistency]) CouchbaseOrm.logger.debug("View [#{@design_document}, #{name.inspect}] options: #{opts.inspect}") if result_modifier include_docs(bucket.view_query(@design_document, name.to_s, Couchbase::Options::View.new(**opts.except(:include_docs)))).map(&result_modifier) diff --git a/spec/base_spec.rb b/spec/base_spec.rb index f2e33d2c..d9d7a0a1 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -220,6 +220,15 @@ class BaseTestWithIgnoredProperties < CouchbaseOrm::Base it_behaves_like "ActiveModel" end + it 'does not expose callbacks for nested that wont never be called' do + expect{ + class InvalidNested < CouchbaseOrm::NestedDocument + before_save {p "this should raise on loading class"} + end + + }.to raise_error NoMethodError + end + describe '.ignored_properties' do diff --git a/spec/n1ql_spec.rb b/spec/n1ql_spec.rb index 1418be31..c4266559 100644 --- a/spec/n1ql_spec.rb +++ b/spec/n1ql_spec.rb @@ -170,6 +170,26 @@ class N1QLTest < CouchbaseOrm::Base expect(Set.new(docs)).to eq(Set.new(%w[bob jane mel])) end + it "should log the default scan_consistency when n1ql query is executed" do + allow(CouchbaseOrm.logger).to receive(:debug) + N1QLTest.by_rating_reverse() + expect(CouchbaseOrm.logger).to have_received(:debug).at_least(:once).with("N1QL query: select raw meta().id from `#{CouchbaseOrm::Connection.bucket.name}` where type=\"n1_ql_test\" order by name DESC return 0 rows with scan_consistency : #{described_class::DEFAULT_SCAN_CONSISTENCY}") + end + + it "should log the set scan_consistency when n1ql query is executed with a specific scan_consistency" do + allow(CouchbaseOrm.logger).to receive(:debug) + default_n1ql_config = CouchbaseOrm::N1ql.config + CouchbaseOrm::N1ql.config({ scan_consistency: :not_bounded }) + puts "CouchbaseOrm::N1ql.config" + puts CouchbaseOrm::N1ql.config + N1QLTest.by_rating_reverse() + expect(CouchbaseOrm.logger).to have_received(:debug).at_least(:once).with("N1QL query: select raw meta().id from `#{CouchbaseOrm::Connection.bucket.name}` where type=\"n1_ql_test\" order by name DESC return 0 rows with scan_consistency : not_bounded") + + CouchbaseOrm::N1ql.config(default_n1ql_config) + N1QLTest.by_rating_reverse() + expect(CouchbaseOrm.logger).to have_received(:debug).at_least(:once).with("N1QL query: select raw meta().id from `#{CouchbaseOrm::Connection.bucket.name}` where type=\"n1_ql_test\" order by name DESC return 0 rows with scan_consistency : #{described_class::DEFAULT_SCAN_CONSISTENCY}") + end + after(:all) do N1QLTest.delete_all end