From b89d327a613d0d35025f4f5abd6019682635104f Mon Sep 17 00:00:00 2001 From: Matt Aimonetti Date: Sun, 17 May 2009 19:34:02 -0700 Subject: [PATCH] cleaned up the design view names since we don't need to use md5 view names anymore. (we are using different revision numbers) --- lib/couchrest/mixins/class_proxy.rb | 4 ++ lib/couchrest/mixins/design_doc.rb | 3 +- lib/couchrest/mixins/views.rb | 36 +++++++++++------- spec/couchrest/more/extended_doc_view_spec.rb | 37 +++++-------------- 4 files changed, 37 insertions(+), 43 deletions(-) diff --git a/lib/couchrest/mixins/class_proxy.rb b/lib/couchrest/mixins/class_proxy.rb index 17d6adf..6b800bc 100644 --- a/lib/couchrest/mixins/class_proxy.rb +++ b/lib/couchrest/mixins/class_proxy.rb @@ -81,6 +81,10 @@ module CouchRest @klass.all_design_doc_versions(@database) end + def model_design_doc + @klass.model_design_doc(@database) + end + def cleanup_design_docs! @klass.cleanup_design_docs!(@database) end diff --git a/lib/couchrest/mixins/design_doc.rb b/lib/couchrest/mixins/design_doc.rb index 0f9abcf..a635638 100644 --- a/lib/couchrest/mixins/design_doc.rb +++ b/lib/couchrest/mixins/design_doc.rb @@ -25,8 +25,7 @@ module CouchRest design_doc['views'].each do |name, view| funcs << "#{name}/#{view['map']}#{view['reduce']}" end - md5 = Digest::MD5.hexdigest(funcs.sort.join('')) - self.design_doc_slug_cache = "#{self.to_s}-#{md5}" + self.design_doc_slug_cache = self.to_s end def default_design_doc diff --git a/lib/couchrest/mixins/views.rb b/lib/couchrest/mixins/views.rb index 9d02024..55c1613 100644 --- a/lib/couchrest/mixins/views.rb +++ b/lib/couchrest/mixins/views.rb @@ -104,26 +104,34 @@ module CouchRest fetch_view_with_docs(db, name, query, raw, &block) end + # DEPRECATED + # user model_design_doc to retrieve the current design doc def all_design_doc_versions(db = database) - db.documents :startkey => "_design/#{self.to_s}-", + db.documents :startkey => "_design/#{self.to_s}", :endkey => "_design/#{self.to_s}-\u9999" end + + def model_design_doc(db = database) + begin + @model_design_doc = db.get("_design/#{self.to_s}") + rescue + nil + end + end - # Deletes any non-current design docs that were created by this class. - # Running this when you're deployed version of your application is steadily - # and consistently using the latest code, is the way to clear out old design - # docs. Running it to early could mean that live code has to regenerate + # Deletes the current design doc for the current class. + # Running it to early could mean that live code has to regenerate # potentially large indexes. def cleanup_design_docs!(db = database) - ddocs = all_design_doc_versions(db) - ddocs["rows"].each do |row| - if (row['id'] != design_doc_id) - db.delete_doc({ - "_id" => row['id'], - "_rev" => row['value']['rev'] - }) - end - end + save_design_doc_on(db) + # db.refresh_design_doc + # db.save_design_doc + # design_doc = model_design_doc(db) + # if design_doc + # db.delete_doc(design_doc) + # else + # false + # end end private diff --git a/spec/couchrest/more/extended_doc_view_spec.rb b/spec/couchrest/more/extended_doc_view_spec.rb index e4aff6b..4530227 100644 --- a/spec/couchrest/more/extended_doc_view_spec.rb +++ b/spec/couchrest/more/extended_doc_view_spec.rb @@ -183,14 +183,13 @@ describe "ExtendedDocument views" do it "should barf on all_design_doc_versions if no database given" do lambda{Unattached.all_design_doc_versions}.should raise_error end - it "should clean up design docs left around on specific database" do - Unattached.by_title :database=>@db - Unattached.all_design_doc_versions(@db)["rows"].length.should == 1 + it "should be able to cleanup the db/bump the revision number" do + # if the previous specs were not run, the model_design_doc will be blank Unattached.view_by :questions - Unattached.by_questions :database=>@db - Unattached.all_design_doc_versions(@db)["rows"].length.should == 2 + Unattached.by_questions(:database => @db) + original_revision = Unattached.model_design_doc(@db)['_rev'] Unattached.cleanup_design_docs!(@db) - Unattached.all_design_doc_versions(@db)["rows"].length.should == 1 + Unattached.model_design_doc(@db)['_rev'].should_not == original_revision end end @@ -246,12 +245,10 @@ describe "ExtendedDocument views" do end it "should clean up design docs left around on specific database" do @us.by_title - @us.all_design_doc_versions["rows"].length.should == 1 + original_id = @us.model_design_doc['_rev'] Unattached.view_by :professor @us.by_professor - @us.all_design_doc_versions["rows"].length.should == 2 - @us.cleanup_design_docs! - @us.all_design_doc_versions["rows"].length.should == 1 + @us.model_design_doc['_rev'].should_not == original_id end end @@ -321,6 +318,7 @@ describe "ExtendedDocument views" do before(:each) do reset_test_db! Article.by_date + @original_doc_rev = Article.model_design_doc['_rev'] @design_docs = Article.database.documents :startkey => "_design/", :endkey => "_design/\u9999" end it "should not create a design doc on view definition" do @@ -332,24 +330,9 @@ describe "ExtendedDocument views" do ddocs = Article.all_design_doc_versions["rows"].length Article.view_by :updated_at Article.by_updated_at - Article.all_design_doc_versions["rows"].length.should == ddocs + 1 + @original_doc_rev.should_not == Article.model_design_doc['_rev'] Article.design_doc["views"].keys.should include("by_updated_at") end end - - describe "with a lot of designs left around" do - before(:each) do - reset_test_db! - Article.by_date - Article.view_by :field - Article.by_field - end - it "should clean them up" do - Article.view_by :stream - Article.by_stream - Article.all_design_doc_versions["rows"].length.should > 1 - Article.cleanup_design_docs! - Article.all_design_doc_versions["rows"].length.should == 1 - end - end + end