Created
May 16, 2012 19:34
-
-
Save davidjrice/2713316 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(doc) { | |
emit(doc.content_type, doc); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :couchdb do | |
task :migrate => :environment do | |
databases = Database.all | |
if (Rails.env.development? || Rails.env.test?) && ENV['DELAY_COUCH_MIGRATIONS'].nil? | |
puts "-> Migrating CouchDB" | |
databases.each do |d| | |
puts "-> Migrating #{d.couch_db_name}" | |
Schema.migrate( CouchRest::database!(d.couch_url)) | |
end | |
else | |
puts "-> Delayed Migrating CouchDB" | |
databases.each do |d| | |
puts "-> Storing Migrate Job for #{d.couch_db_name}" | |
Resque.enqueue CouchMigrate, d.couch_url | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CouchMigrate | |
@queue = :migrations | |
def self.perform(db) | |
Schema.migrate CouchRest::database!(db) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Schema | |
class << self | |
@@views = [ | |
"resources" | |
] | |
@@db = nil | |
def migrate(db=nil) | |
raise "ERROR: You need to provide a CouchRest::Database to migrate." unless (db.present? || !db.kind_of?(::CouchRest::Database)) | |
@@db = db | |
@@views.each do |view| | |
klass = "Schema::#{view.camelcase}".constantize.new(db) | |
klass.save | |
end | |
end | |
end | |
end | |
class Schema::View | |
attr_accessor :document, :version, :db | |
def view_name | |
self.class.to_s.split("::").last.downcase | |
end | |
def initialize(db) | |
@db ||= db | |
@views ||= self.class.views | |
@version ||= self.class.version | |
end | |
@@base_path = "#{Rails.root}/app/couch/schema/" | |
def views | |
tmp = {} | |
@views.each do |key, values| | |
map_view_path = File.join(@@base_path, view_name, "#{key.to_s}.map.js") | |
reduce_view_path = File.join(@@base_path, view_name, "#{key.to_s}.reduce.js") | |
puts "Couchdb WARNING: #{map_view_path} not present" unless File.exists?(map_view_path) | |
if File.exists?(map_view_path) | |
values[:map] = File.open(map_view_path).read | |
end | |
if File.exists?(reduce_view_path) && values[:reduce] | |
values[:reduce] = File.open(reduce_view_path).read | |
else | |
values.delete(:reduce) | |
end | |
tmp[key] = values | |
end | |
tmp | |
end | |
def design | |
d = { | |
_id: "_design/#{view_name}", | |
version: version, | |
views: views | |
} | |
end | |
def get_document | |
@db.get(design[:_id]) | |
end | |
def document | |
begin | |
@document ||= get_document | |
rescue RestClient::ResourceNotFound | |
return nil | |
end | |
@document | |
end | |
def needs_updating? | |
current_version = document["version"] | |
current_version.nil? || current_version < version | |
end | |
def save | |
if document && needs_updating? | |
updated = document.merge(design.stringify_keys) | |
puts "Document #{design[:_id]} version:#{design[:version]} updating from version #{document['version']}" | |
@db.save_doc( updated ) | |
elsif !document | |
puts "Document #{design[:_id]} version:#{design[:version]} importing new view" | |
@db.save_doc( design.stringify_keys ) | |
else | |
puts "Document #{design[:_id]} version:#{design[:version]} no update required" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Schema::Resources < Schema::View | |
def self.version | |
14 | |
end | |
def self.views | |
{ | |
by_content_type: { | |
map: true | |
}, | |
by_content_type_and_code: { | |
map: true, | |
reduce: true | |
}, | |
by_content_type_and_error_count: { | |
map: true, | |
reduce: true | |
}, | |
by_run_id: { | |
map: true, | |
reduce: true | |
} | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment