Skip to content

Commit b56db47

Browse files
authored
add digest to single design doc name (#170)
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.
1 parent 3799dc7 commit b56db47

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

lib/couch_potato/view/view_query.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def update_view
4141
design_doc ||= empty_design_document
4242
if CouchPotato::Config.single_design_document
4343
design_doc['views'] = all_views
44+
if CouchPotato::Config.digest_view_names
45+
design_doc['_id'] = "_design/#{@design_document_name}-#{Digest::SHA256.hexdigest(design_doc['views'].to_json)}"
46+
end
4447
else
4548
design_doc['views'][@view_name.to_s] = view_functions
4649
end

spec/unit/view_query_spec.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
CouchPotato::View::ViewQuery.clear_cache
99
end
1010

11+
after(:each) do
12+
CouchPotato::Config.single_design_document = false
13+
CouchPotato::Config.digest_view_names = false
14+
end
15+
1116
it 'does not pass a key if conditions are empty' do
1217
expect(db).to receive(:view).with(anything, {})
1318
CouchPotato::View::ViewQuery.new(db, '', {:view0 => {}}).query_view!
@@ -81,4 +86,23 @@
8186
expect(db).to receive(:save_doc)
8287
CouchPotato::View::ViewQuery.new(db, 'design', :view8 => {:map => '<map_code>', :reduce => '<new reduce_code>'}).query_view!
8388
end
84-
end
89+
90+
it 'adds a digest of all views to the design document if single_design_doc is true' do
91+
CouchPotato::Config.single_design_document = true
92+
CouchPotato::Config.digest_view_names = true
93+
94+
allow(db).to receive(:get).and_return(nil)
95+
allow(db).to receive(:save_doc).and_return(true)
96+
view_class = double('view_class',
97+
views: {view: {map: '<map_code>'}},
98+
execute_view: double('view_spec', view_name: 'view', map_function: '<map_code>', reduce_function: nil))
99+
allow(CouchPotato).to receive(:views).and_return([view_class])
100+
101+
CouchPotato::View::ViewQuery.new(
102+
db,
103+
'couch_potato',
104+
{:view => {:map => '<map_code>'}}).query_view!
105+
106+
expect(db).to have_received(:save_doc).with(hash_including({"_id" => "_design/couch_potato-56d286b4f0cd3a50fdd2ad428034d08a6483311539f7e138c45e781611b9dbbc"}))
107+
end
108+
end

0 commit comments

Comments
 (0)