def migrate(db = nil, &block)
db ||= database
doc = load_from_database(db)
cleanup = nil
id = self['_id']
if !doc
new_doc = to_hash.dup
db.save_doc(new_doc)
result = :created
elsif doc['couchrest-hash'] != checksum
id += "_migration"
old_migration = load_from_database(db, id)
db.delete_doc(old_migration) if old_migration
new_doc = doc.merge(to_hash)
new_doc['_id'] = id
new_doc.delete('_rev')
db.save_doc(new_doc)
cleanup = Proc.new do
db.copy_doc(new_doc, doc)
db.delete_doc(new_doc)
self
end
result = :migrated
else
result = :no_change
end
wait_for_view_update_completion(db, new_doc)
yield result if block_given?
cleanup
end