Class: Mongoid::Migration
- Inherits:
-
Object
- Object
- Mongoid::Migration
- Defined in:
- lib/mongoid_rails_migrations/active_record_ext/migrations.rb
Overview
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. = false
In environment.rb.
Constant Summary collapse
- @@verbose =
true
Class Method Summary collapse
- .announce(message) ⇒ Object
- .connection ⇒ Object
-
.down_with_benchmarks ⇒ Object
:nodoc:.
- .method_missing(method, *arguments, &block) ⇒ Object
-
.migrate(direction) ⇒ Object
Execute this migration in the named direction.
- .say(message, subitem = false) ⇒ Object
- .say_with_time(message) ⇒ Object
-
.singleton_method_added(sym) ⇒ Object
Because the method added may do an alias_method, it can be invoked recursively.
- .suppress_messages ⇒ Object
-
.up_with_benchmarks ⇒ Object
:nodoc:.
- .write(text = "") ⇒ Object
Class Method Details
.announce(message) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 118 def announce() version = defined?(@version) ? @version : nil text = "#{version} #{name}: #{}" length = [0, 75 - text.length].max write "== %s %s" % [text, "=" * length] end |
.connection ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 146 def connection # ActiveRecord::Base.connection if ::Mongoid.respond_to?(:default_client) ::Mongoid.default_client else ::Mongoid.default_session end end |
.down_with_benchmarks ⇒ Object
:nodoc:
71 72 73 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 71 def down_with_benchmarks #:nodoc: migrate(:down) end |
.method_missing(method, *arguments, &block) ⇒ Object
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 155 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) ⇒ Object
Execute this migration in the named direction
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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 result end |
.say(message, subitem = false) ⇒ Object
126 127 128 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 126 def say(, subitem=false) write "#{subitem ? " ->" : "--"} #{}" end |
.say_with_time(message) ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 130 def say_with_time() say() result = nil time = Benchmark.measure { result = yield } say "%.4fs" % time.real, :subitem say("#{result} rows", :subitem) if result.is_a?(Integer) result end |
.singleton_method_added(sym) ⇒ Object
Because the method added may do an alias_method, it can be invoked recursively. We use @ignore_new_methods as a guard to indicate whether it is safe for the call to proceed.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 98 def singleton_method_added(sym) #:nodoc: return if defined?(@ignore_new_methods) && @ignore_new_methods begin @ignore_new_methods = true case sym when :up, :down singleton_class.send(:alias_method, "#{sym}_without_benchmarks".to_sym, sym) singleton_class.send(:alias_method, sym, "#{sym}_with_benchmarks".to_sym) end ensure @ignore_new_methods = false end end |
.suppress_messages ⇒ Object
139 140 141 142 143 144 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 139 def save, self.verbose = verbose, false yield ensure self.verbose = save end |
.up_with_benchmarks ⇒ Object
:nodoc:
67 68 69 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 67 def up_with_benchmarks #:nodoc: migrate(:up) end |
.write(text = "") ⇒ Object
114 115 116 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 114 def write(text="") puts(text) if verbose end |