# File lib/couchrest/model/designs/migrations.rb, line 42
        def migrate(db = nil, &block)
          db    ||= database
          doc     = load_from_database(db)
          cleanup = nil
          id      = self['_id']

          if !doc
            # no need to migrate, just save it
            new_doc = to_hash.dup
            db.save_doc(new_doc)

            result = :created
          elsif doc['couchrest-hash'] != checksum
            id += "_migration"

            # Delete current migration if there is one
            old_migration = load_from_database(db, id)
            db.delete_doc(old_migration) if old_migration

            # Save new design doc
            new_doc = doc.merge(to_hash)
            new_doc['_id'] = id
            new_doc.delete('_rev')
            db.save_doc(new_doc)

            # Proc definition to copy the migration doc over the original
            cleanup = Proc.new do
              db.copy_doc(new_doc, doc)
              db.delete_doc(new_doc)
              self
            end

            result = :migrated
          else
            # Already up to date
            result = :no_change
          end

          wait_for_view_update_completion(db, new_doc)

          yield result if block_given?

          cleanup
        end