Permalink
Browse files

Regenerate views after making fixtures modifications.

  • Loading branch information...
1 parent e213eed commit bd3d2df24af7535b911c683020afac38b8d043dc @dougbarth dougbarth committed Nov 30, 2009
Showing with 33 additions and 1 deletion.
  1. +32 −0 lib/couch_rest_rails/fixtures.rb
  2. +1 −1 lib/couch_rest_rails/tests.rb
@@ -11,13 +11,15 @@ def load(database)
db_name =COUCHDB_CONFIG[:db_prefix] + File.basename( db) +
COUCHDB_CONFIG[:db_suffix]
res = CouchRest.get("#{COUCHDB_CONFIG[:host_path]}/#{db_name}") rescue nil
+ db_con = nil
if res
db_con = CouchRest.database("#{COUCHDB_CONFIG[:host_path]}/#{db_name}")
Dir.glob(File.join(RAILS_ROOT, CouchRestRails.fixture_path, "#{database}.yml")).each do |file|
db_con.bulk_save(YAML::load(ERB.new(IO.read(file)).result).map {|f| f[1]})
fixture_files << File.basename(file)
end
end
+ db_con && regenerate_all_views(db_con)
if fixture_files.empty?
return "No fixtures found in #{CouchRestRails.fixture_path}"
else
@@ -46,6 +48,7 @@ def clear(database)
end
end
db_con.bulk_delete(docs) unless docs.empty?
+ regenerate_all_views(db_con)
return "All non design documents from '#{database}' deleted successfully"
else
return "Unable to connect to database '#{database}'"
@@ -55,6 +58,35 @@ def clear(database)
return "Unable to clear fixtures from '#{database}"
end
+ MAX_RUNNING_TASKS = 2
+ ACTIVE_TASK_WAIT_TIME = 60
+
+ def regenerate_all_views(database)
+ # rebuild the all views...
+ rows = database.documents(:startkey=>"_design/",
+ :endkey=>"_design0/",
+ :include_docs => true)['rows']
+ rows.each { |design_doc_hash|
+ doc = design_doc_hash['doc']
+ design_doc = doc['_id'].sub('_design/','')
+ doc_views = doc['views']
+ unless doc_views.nil? || doc_views.empty?
+ # just hit the first view for each one...
+ view_name = doc_views.keys[0]
+ begin
+ database.view("#{design_doc}/#{view_name}", :limit =>0)
+ rescue Exception => e
+ # check active tasks - wait unil there are no running
+ # tasks per db before starting a new one
+ # we timed out on the view...
+ while active_task_count >= MAX_RUNNING_TASKS
+ sleep ACTIVE_TASK_WAIT_TIME
+ end
+ end
+ end
+ }
+ end
+
def dump(database)
return "Database '#{database}' doesn't exists" unless (database == "*" ||
File.exist?(File.join(RAILS_ROOT, CouchRestRails.setup_path, database)))
@@ -95,7 +95,7 @@ class ReadOnlyActionControllerTest < ActionController::TestCase
end
end
-class ActiveSupport::TestCase
+class ActiveSupport::TestCase #:nodoc:
setup :setup_couchdb_fixtures
teardown :teardown_couchdb_fixtures

0 comments on commit bd3d2df

Please sign in to comment.