Skip to content

Instantly share code, notes, and snippets.

@davidjrice
Created May 16, 2012 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidjrice/2713316 to your computer and use it in GitHub Desktop.
Save davidjrice/2713316 to your computer and use it in GitHub Desktop.
function(doc) {
emit(doc.content_type, doc);
}
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
class CouchMigrate
@queue = :migrations
def self.perform(db)
Schema.migrate CouchRest::database!(db)
end
end
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
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