From c0882707b039396c7ca8e99680de97e37c258cec Mon Sep 17 00:00:00 2001 From: Alexander Lang Date: Wed, 4 Jun 2025 18:11:52 +0200 Subject: [PATCH] add digest to single design doc name this way, instead of updating the existing one (and blocking all view queries during that time) during a deployment, a new design doc is created when any views are changed. --- lib/couch_potato/view/view_query.rb | 3 +++ spec/unit/view_query_spec.rb | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/couch_potato/view/view_query.rb b/lib/couch_potato/view/view_query.rb index c9b72e2..c1eb719 100644 --- a/lib/couch_potato/view/view_query.rb +++ b/lib/couch_potato/view/view_query.rb @@ -41,6 +41,9 @@ def update_view design_doc ||= empty_design_document if CouchPotato::Config.single_design_document design_doc['views'] = all_views + if CouchPotato::Config.digest_view_names + design_doc['_id'] = "_design/#{@design_document_name}-#{Digest::SHA256.hexdigest(design_doc['views'].to_json)}" + end else design_doc['views'][@view_name.to_s] = view_functions end diff --git a/spec/unit/view_query_spec.rb b/spec/unit/view_query_spec.rb index 232616d..6b71b79 100644 --- a/spec/unit/view_query_spec.rb +++ b/spec/unit/view_query_spec.rb @@ -8,6 +8,11 @@ CouchPotato::View::ViewQuery.clear_cache end + after(:each) do + CouchPotato::Config.single_design_document = false + CouchPotato::Config.digest_view_names = false + end + it 'does not pass a key if conditions are empty' do expect(db).to receive(:view).with(anything, {}) CouchPotato::View::ViewQuery.new(db, '', {:view0 => {}}).query_view! @@ -81,4 +86,23 @@ expect(db).to receive(:save_doc) CouchPotato::View::ViewQuery.new(db, 'design', :view8 => {:map => '', :reduce => ''}).query_view! end -end + + it 'adds a digest of all views to the design document if single_design_doc is true' do + CouchPotato::Config.single_design_document = true + CouchPotato::Config.digest_view_names = true + + allow(db).to receive(:get).and_return(nil) + allow(db).to receive(:save_doc).and_return(true) + view_class = double('view_class', + views: {view: {map: ''}}, + execute_view: double('view_spec', view_name: 'view', map_function: '', reduce_function: nil)) + allow(CouchPotato).to receive(:views).and_return([view_class]) + + CouchPotato::View::ViewQuery.new( + db, + 'couch_potato', + {:view => {:map => ''}}).query_view! + + expect(db).to have_received(:save_doc).with(hash_including({"_id" => "_design/couch_potato-56d286b4f0cd3a50fdd2ad428034d08a6483311539f7e138c45e781611b9dbbc"})) + end +end \ No newline at end of file