class Mongoid::Migration

Data migrations can manage the modification of data. It’s a solution to the common problem of modifying data between code revisions within a document oriented database.

Example of simple migration for a system dependency:

class AddBaselineSurveySchema < Mongoid::Migration
  def self.up
    SurveySchema.create(:label => 'Baseline Survey')
  end

  def self.down
    SurveySchema.where(:label => 'Baseline Survey').first.destroy
  end
end

Timestamped Migrations

By default, generates migrations that look like:

20080717013526_your_migration_name.rb

The prefix is a generation timestamp (in UTC).

If you’d prefer to use numeric prefixes, you can turn timestamped migrations off by setting:

Mongoid.config.timestamped_migrations = false

In environment.rb.

Public Class Methods

announce(message) click to toggle source
# File lib/mongoid_migrations/active_record_ext/migrations.rb, line 119
def announce(message)
  version = defined?(@version) ? @version : nil

  text = "#{version} #{name}: #{message}"
  length = [0, 75 - text.length].max
  write "== %s %s" % [text, "=" * length]
end
connection() click to toggle source
# File lib/mongoid_migrations/active_record_ext/migrations.rb, line 147
def connection
  # ActiveRecord::Base.connection
  if ::Mongoid.respond_to?(:default_client)
    ::Mongoid.default_client
  else
    ::Mongoid.default_session
  end
end
method_missing(method, *arguments, &block) click to toggle source
# File lib/mongoid_migrations/active_record_ext/migrations.rb, line 156
def method_missing(method, *arguments, &block)
  arg_list = arguments.map(&:inspect) * ', '

  say_with_time "#{method}(#{arg_list})" do
    # unless arguments.empty? || method == :execute
    #   arguments[0] = Migrator.proper_table_name(arguments.first)
    # end
    connection.send(method, *arguments, &block)
  end
end
migrate(direction) click to toggle source

Execute this migration in the named direction

# File lib/mongoid_migrations/active_record_ext/migrations.rb, line 76
def migrate(direction)
  return unless respond_to?(direction)

  case direction
    when :up   then announce "migrating"
    when :down then announce "reverting"
  end

  result = nil
  time = Benchmark.measure { result = send("#{direction}_without_benchmarks") }

  case direction
    when :up   then announce "migrated (%.4fs)" % time.real; write
    when :down then announce "reverted (%.4fs)" % time.real; write
  end

  result
end
say(message, subitem=false) click to toggle source
# File lib/mongoid_migrations/active_record_ext/migrations.rb, line 127
def say(message, subitem=false)
  write "#{subitem ? "   ->" : "--"} #{message}"
end
say_with_time(message) { || ... } click to toggle source
# File lib/mongoid_migrations/active_record_ext/migrations.rb, line 131
def say_with_time(message)
  say(message)
  result = nil
  time = Benchmark.measure { result = yield }
  say "%.4fs" % time.real, :subitem
  say("#{result} rows", :subitem) if result.is_a?(Integer)
  result
end
suppress_messages() { || ... } click to toggle source
# File lib/mongoid_migrations/active_record_ext/migrations.rb, line 140
def suppress_messages
  save, self.verbose = verbose, false
  yield
ensure
  self.verbose = save
end
write(text="") click to toggle source
# File lib/mongoid_migrations/active_record_ext/migrations.rb, line 115
def write(text="")
  puts(text) if verbose
end