added an automated way to mark design docs as dirty after the db was reset
This commit is contained in:
parent
e48a6c8866
commit
c35c35157a
|
@ -236,6 +236,16 @@ module CouchRest
|
||||||
copy_doc(doc, dest)
|
copy_doc(doc, dest)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def extended_document_classes
|
||||||
|
@extended_document_classes ||= []
|
||||||
|
end
|
||||||
|
|
||||||
|
# store extended document classes so we can clear
|
||||||
|
# their freshness when we reset the DB
|
||||||
|
def register_extended_document_class(klass)
|
||||||
|
extended_document_classes << klass
|
||||||
|
end
|
||||||
|
|
||||||
# Compact the database, removing old document revisions and optimizing space use.
|
# Compact the database, removing old document revisions and optimizing space use.
|
||||||
def compact!
|
def compact!
|
||||||
CouchRest.post "#{@uri}/_compact"
|
CouchRest.post "#{@uri}/_compact"
|
||||||
|
@ -271,11 +281,16 @@ module CouchRest
|
||||||
# DELETE the database itself. This is not undoable and could be rather
|
# DELETE the database itself. This is not undoable and could be rather
|
||||||
# catastrophic. Use with care!
|
# catastrophic. Use with care!
|
||||||
def delete!
|
def delete!
|
||||||
|
clear_extended_doc_fresh_cache
|
||||||
CouchRest.delete @uri
|
CouchRest.delete @uri
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def clear_extended_doc_fresh_cache
|
||||||
|
extended_document_classes.each{|klass| klass.design_doc_fresh = false if klass.respond_to?(:design_doc_fresh=) }
|
||||||
|
end
|
||||||
|
|
||||||
def uri_for_attachment(doc, name)
|
def uri_for_attachment(doc, name)
|
||||||
if doc.is_a?(String)
|
if doc.is_a?(String)
|
||||||
puts "CouchRest::Database#fetch_attachment will eventually require a doc as the first argument, not a doc.id"
|
puts "CouchRest::Database#fetch_attachment will eventually require a doc as the first argument, not a doc.id"
|
||||||
|
|
|
@ -20,6 +20,15 @@ module CouchRest
|
||||||
subklass.properties = self.properties.dup
|
subklass.properties = self.properties.dup
|
||||||
end
|
end
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
# re opening the use_database method so we can register our class
|
||||||
|
subklass.class_eval <<-EOS, __FILE__, __LINE__
|
||||||
|
def self.use_database(db)
|
||||||
|
super
|
||||||
|
db.register_extended_document_class(self) if db.respond_to?(:register_extended_document_class) && !db.extended_document_classes.include?(self)
|
||||||
|
end
|
||||||
|
EOS
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Accessors
|
# Accessors
|
||||||
|
@ -46,6 +55,8 @@ module CouchRest
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Automatically set <tt>updated_at</tt> and <tt>created_at</tt> fields
|
# Automatically set <tt>updated_at</tt> and <tt>created_at</tt> fields
|
||||||
# on the document whenever saving occurs. CouchRest uses a pretty
|
# on the document whenever saving occurs. CouchRest uses a pretty
|
||||||
# decent time format by default. See Time#to_json
|
# decent time format by default. See Time#to_json
|
||||||
|
|
|
@ -61,12 +61,12 @@ describe "ExtendedDocument views" do
|
||||||
describe "another model with a simple view" do
|
describe "another model with a simple view" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
reset_test_db!
|
reset_test_db!
|
||||||
|
Course.design_doc_fresh = false
|
||||||
%w{aaa bbb ddd eee}.each do |title|
|
%w{aaa bbb ddd eee}.each do |title|
|
||||||
Course.new(:title => title).save
|
Course.new(:title => title).save
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
it "should make the design doc upon first query" do
|
it "should make the design doc upon first query" do
|
||||||
Course.design_doc_fresh = false
|
|
||||||
Course.by_title
|
Course.by_title
|
||||||
doc = Course.design_doc
|
doc = Course.design_doc
|
||||||
doc['views']['all']['map'].should include('Course')
|
doc['views']['all']['map'].should include('Course')
|
||||||
|
@ -74,13 +74,11 @@ describe "ExtendedDocument views" do
|
||||||
it "should can query via view" do
|
it "should can query via view" do
|
||||||
# register methods with method-missing, for local dispatch. method
|
# register methods with method-missing, for local dispatch. method
|
||||||
# missing lookup table, no heuristics.
|
# missing lookup table, no heuristics.
|
||||||
Course.design_doc_fresh = false
|
|
||||||
view = Course.view :by_title
|
view = Course.view :by_title
|
||||||
designed = Course.by_title
|
designed = Course.by_title
|
||||||
view.should == designed
|
view.should == designed
|
||||||
end
|
end
|
||||||
it "should get them" do
|
it "should get them" do
|
||||||
Course.design_doc_fresh = false
|
|
||||||
rs = Course.by_title
|
rs = Course.by_title
|
||||||
rs.length.should == 4
|
rs.length.should == 4
|
||||||
end
|
end
|
||||||
|
@ -104,7 +102,7 @@ describe "ExtendedDocument views" do
|
||||||
describe "a ducktype view" do
|
describe "a ducktype view" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
reset_test_db!
|
reset_test_db!
|
||||||
@id = TEST_SERVER.default_database.save_doc({:dept => true})['id']
|
@id = DB.save_doc({:dept => true})['id']
|
||||||
end
|
end
|
||||||
it "should setup" do
|
it "should setup" do
|
||||||
duck = Course.get(@id) # from a different db
|
duck = Course.get(@id) # from a different db
|
||||||
|
@ -126,7 +124,7 @@ describe "ExtendedDocument views" do
|
||||||
describe "a model class not tied to a database" do
|
describe "a model class not tied to a database" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
reset_test_db!
|
reset_test_db!
|
||||||
@db = TEST_SERVER.default_database
|
@db = DB
|
||||||
%w{aaa bbb ddd eee}.each do |title|
|
%w{aaa bbb ddd eee}.each do |title|
|
||||||
u = Unattached.new(:title => title)
|
u = Unattached.new(:title => title)
|
||||||
u.database = @db
|
u.database = @db
|
||||||
|
@ -191,7 +189,7 @@ describe "ExtendedDocument views" do
|
||||||
end
|
end
|
||||||
it "should be able to cleanup the db/bump the revision number" do
|
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
|
# if the previous specs were not run, the model_design_doc will be blank
|
||||||
Unattached.use_database TEST_SERVER.default_database
|
Unattached.use_database DB
|
||||||
Unattached.view_by :questions
|
Unattached.view_by :questions
|
||||||
Unattached.by_questions(:database => @db)
|
Unattached.by_questions(:database => @db)
|
||||||
original_revision = Unattached.model_design_doc(@db)['_rev']
|
original_revision = Unattached.model_design_doc(@db)['_rev']
|
||||||
|
@ -204,8 +202,8 @@ describe "ExtendedDocument views" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
reset_test_db!
|
reset_test_db!
|
||||||
# setup the class default doc to save the design doc
|
# setup the class default doc to save the design doc
|
||||||
Unattached.use_database TEST_SERVER.default_database
|
Unattached.use_database DB
|
||||||
@us = Unattached.on(TEST_SERVER.default_database)
|
@us = Unattached.on(DB)
|
||||||
%w{aaa bbb ddd eee}.each do |title|
|
%w{aaa bbb ddd eee}.each do |title|
|
||||||
u = @us.new(:title => title)
|
u = @us.new(:title => title)
|
||||||
u.save
|
u.save
|
||||||
|
@ -213,12 +211,10 @@ describe "ExtendedDocument views" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
it "should query all" do
|
it "should query all" do
|
||||||
Unattached.design_doc_fresh = false
|
|
||||||
rs = @us.all
|
rs = @us.all
|
||||||
rs.length.should == 4
|
rs.length.should == 4
|
||||||
end
|
end
|
||||||
it "should make the design doc upon first query" do
|
it "should make the design doc upon first query" do
|
||||||
Unattached.design_doc_fresh = false
|
|
||||||
@us.by_title
|
@us.by_title
|
||||||
doc = @us.design_doc
|
doc = @us.design_doc
|
||||||
doc['views']['all']['map'].should include('Unattached')
|
doc['views']['all']['map'].should include('Unattached')
|
||||||
|
@ -265,7 +261,6 @@ describe "ExtendedDocument views" do
|
||||||
|
|
||||||
describe "a model with a compound key view" do
|
describe "a model with a compound key view" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
Article.design_doc_fresh = false
|
|
||||||
Article.by_user_id_and_date.each{|a| a.destroy(true)}
|
Article.by_user_id_and_date.each{|a| a.destroy(true)}
|
||||||
Article.database.bulk_delete
|
Article.database.bulk_delete
|
||||||
written_at = Time.now - 24 * 3600 * 7
|
written_at = Time.now - 24 * 3600 * 7
|
||||||
|
@ -309,19 +304,16 @@ describe "ExtendedDocument views" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
it "should be available raw" do
|
it "should be available raw" do
|
||||||
Article.design_doc_fresh = false
|
|
||||||
view = Article.by_tags :raw => true
|
view = Article.by_tags :raw => true
|
||||||
view['rows'].length.should == 5
|
view['rows'].length.should == 5
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be default to :reduce => false" do
|
it "should be default to :reduce => false" do
|
||||||
Article.design_doc_fresh = false
|
|
||||||
ars = Article.by_tags
|
ars = Article.by_tags
|
||||||
ars.first.tags.first.should == 'cool'
|
ars.first.tags.first.should == 'cool'
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be raw when reduce is true" do
|
it "should be raw when reduce is true" do
|
||||||
Article.design_doc_fresh = false
|
|
||||||
view = Article.by_tags :reduce => true, :group => true
|
view = Article.by_tags :reduce => true, :group => true
|
||||||
view['rows'].find{|r|r['key'] == 'cool'}['value'].should == 3
|
view['rows'].find{|r|r['key'] == 'cool'}['value'].should == 3
|
||||||
end
|
end
|
||||||
|
@ -331,7 +323,6 @@ describe "ExtendedDocument views" do
|
||||||
describe "adding a view" do
|
describe "adding a view" do
|
||||||
before(:each) do
|
before(:each) do
|
||||||
reset_test_db!
|
reset_test_db!
|
||||||
Article.design_doc_fresh = false
|
|
||||||
Article.by_date
|
Article.by_date
|
||||||
@original_doc_rev = Article.model_design_doc['_rev']
|
@original_doc_rev = Article.model_design_doc['_rev']
|
||||||
@design_docs = Article.database.documents :startkey => "_design/", :endkey => "_design/\u9999"
|
@design_docs = Article.database.documents :startkey => "_design/", :endkey => "_design/\u9999"
|
||||||
|
|
2
spec/fixtures/more/article.rb
vendored
2
spec/fixtures/more/article.rb
vendored
|
@ -1,5 +1,5 @@
|
||||||
class Article < CouchRest::ExtendedDocument
|
class Article < CouchRest::ExtendedDocument
|
||||||
use_database TEST_SERVER.default_database
|
use_database DB
|
||||||
unique_id :slug
|
unique_id :slug
|
||||||
|
|
||||||
view_by :date, :descending => true
|
view_by :date, :descending => true
|
||||||
|
|
2
spec/fixtures/more/card.rb
vendored
2
spec/fixtures/more/card.rb
vendored
|
@ -5,7 +5,7 @@ class Card < CouchRest::ExtendedDocument
|
||||||
auto_validate!
|
auto_validate!
|
||||||
|
|
||||||
# Set the default database to use
|
# Set the default database to use
|
||||||
use_database TEST_SERVER.default_database
|
use_database DB
|
||||||
|
|
||||||
# Official Schema
|
# Official Schema
|
||||||
property :first_name
|
property :first_name
|
||||||
|
|
2
spec/fixtures/more/cat.rb
vendored
2
spec/fixtures/more/cat.rb
vendored
|
@ -2,7 +2,7 @@ class Cat < CouchRest::ExtendedDocument
|
||||||
include ::CouchRest::Validation
|
include ::CouchRest::Validation
|
||||||
|
|
||||||
# Set the default database to use
|
# Set the default database to use
|
||||||
use_database TEST_SERVER.default_database
|
use_database DB
|
||||||
|
|
||||||
property :name
|
property :name
|
||||||
property :toys, :cast_as => ['CatToy'], :default => []
|
property :toys, :cast_as => ['CatToy'], :default => []
|
||||||
|
|
2
spec/fixtures/more/event.rb
vendored
2
spec/fixtures/more/event.rb
vendored
|
@ -1,5 +1,5 @@
|
||||||
class Event < CouchRest::ExtendedDocument
|
class Event < CouchRest::ExtendedDocument
|
||||||
use_database TEST_SERVER.default_database
|
use_database DB
|
||||||
|
|
||||||
property :subject
|
property :subject
|
||||||
property :occurs_at, :cast_as => 'Time', :send => 'parse'
|
property :occurs_at, :cast_as => 'Time', :send => 'parse'
|
||||||
|
|
2
spec/fixtures/more/invoice.rb
vendored
2
spec/fixtures/more/invoice.rb
vendored
|
@ -3,7 +3,7 @@ class Invoice < CouchRest::ExtendedDocument
|
||||||
include CouchRest::Validation
|
include CouchRest::Validation
|
||||||
|
|
||||||
# Set the default database to use
|
# Set the default database to use
|
||||||
use_database TEST_SERVER.default_database
|
use_database DB
|
||||||
|
|
||||||
# Official Schema
|
# Official Schema
|
||||||
property :client_name
|
property :client_name
|
||||||
|
|
2
spec/fixtures/more/service.rb
vendored
2
spec/fixtures/more/service.rb
vendored
|
@ -3,7 +3,7 @@ class Service < CouchRest::ExtendedDocument
|
||||||
include CouchRest::Validation
|
include CouchRest::Validation
|
||||||
auto_validate!
|
auto_validate!
|
||||||
# Set the default database to use
|
# Set the default database to use
|
||||||
use_database TEST_SERVER.default_database
|
use_database DB
|
||||||
|
|
||||||
# Official Schema
|
# Official Schema
|
||||||
property :name, :length => 4...20
|
property :name, :length => 4...20
|
||||||
|
|
|
@ -12,6 +12,7 @@ unless defined?(FIXTURE_PATH)
|
||||||
TESTDB = 'couchrest-test'
|
TESTDB = 'couchrest-test'
|
||||||
TEST_SERVER = CouchRest.new
|
TEST_SERVER = CouchRest.new
|
||||||
TEST_SERVER.default_database = TESTDB
|
TEST_SERVER.default_database = TESTDB
|
||||||
|
DB = TEST_SERVER.database(TESTDB)
|
||||||
end
|
end
|
||||||
|
|
||||||
class Basic < CouchRest::ExtendedDocument
|
class Basic < CouchRest::ExtendedDocument
|
||||||
|
@ -19,10 +20,8 @@ class Basic < CouchRest::ExtendedDocument
|
||||||
end
|
end
|
||||||
|
|
||||||
def reset_test_db!
|
def reset_test_db!
|
||||||
cr = TEST_SERVER
|
DB.recreate! rescue nil
|
||||||
db = cr.database(TESTDB)
|
DB
|
||||||
db.recreate! rescue nil
|
|
||||||
db
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Spec::Runner.configure do |config|
|
Spec::Runner.configure do |config|
|
||||||
|
|
Loading…
Reference in a new issue