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, Rails
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_rails_migrations/active_record_ext/migrations.rb, line 126 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_rails_migrations/active_record_ext/migrations.rb, line 154 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_rails_migrations/active_record_ext/migrations.rb, line 163 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_rails_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 begin @@after_migrate.call(@@buffer_output, name, direction, false) if @@after_migrate @@buffer_output = nil rescue => e say("Error in after_migrate hook: #{e}") end result end
say(message, subitem=false)
click to toggle source
# File lib/mongoid_rails_migrations/active_record_ext/migrations.rb, line 134 def say(message, subitem=false) write "#{subitem ? " ->" : "--"} #{message}" end
say_with_time(message) { || ... }
click to toggle source
# File lib/mongoid_rails_migrations/active_record_ext/migrations.rb, line 138 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_rails_migrations/active_record_ext/migrations.rb, line 147 def suppress_messages save, self.verbose = verbose, false yield ensure self.verbose = save end
write(text="")
click to toggle source
# File lib/mongoid_rails_migrations/active_record_ext/migrations.rb, line 120 def write(text="") @@buffer_output ||= "" @@buffer_output += text + "\n" puts(text) if verbose end