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.configure. = false
In environment.rb.
Constant Summary collapse
- @@verbose =
true
Class Method Summary collapse
- .announce(message) ⇒ Object
- .connection ⇒ Object
-
.down_with_benchmarks ⇒ Object
:nodoc:.
-
.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
125 126 127 128 129 130 131 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 125 def announce() version = defined?(@version) ? @version : nil text = "#{version} #{name}: #{}" length = [0, 75 - text.length].max write "== %s %s" % [text, "=" * length] end |
.connection ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 153 def connection if ENV['MONGOID_CLIENT_NAME'] Mongoid.client(ENV['MONGOID_CLIENT_NAME']) else Mongoid.default_client 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 |
.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
133 134 135 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 133 def say(, subitem=false) write "#{subitem ? " ->" : "--"} #{}" end |
.say_with_time(message) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 137 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 |
# 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
146 147 148 149 150 151 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 146 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
119 120 121 122 123 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 119 def write(text="") @@buffer_output ||= "" @@buffer_output += text + "\n" puts(text) if verbose end |