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
126 127 128 129 130 131 132 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 126 def announce() version = defined?(@version) ? @version : nil text = "#{version} #{name}: #{}" length = [0, 75 - text.length].max write "== %s %s" % [text, "=" * length] end |
.connection ⇒ Object
154 155 156 157 158 159 160 161 |
# 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 |
.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
163 164 165 166 167 168 169 170 171 172 |
# 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) ⇒ 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 94 95 96 97 98 99 |
# 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) ⇒ Object
134 135 136 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 134 def say(, subitem=false) write "#{subitem ? " ->" : "--"} #{}" end |
.say_with_time(message) ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 138 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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 104 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
147 148 149 150 151 152 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 147 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
120 121 122 123 124 |
# 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 |