Class: Mongoid::Migration

Inherits:
Object
  • Object
show all
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.timestamped_migrations = false

In environment.rb.

Constant Summary collapse

@@verbose =
true

Class Method Summary collapse

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(message)
  version = defined?(@version) ? @version : nil

  text = "#{version} #{name}: #{message}"
  length = [0, 75 - text.length].max
  write "== %s %s" % [text, "=" * length]
end

.connectionObject



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_benchmarksObject

: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(message, subitem=false)
  write "#{subitem ? "   ->" : "--"} #{message}"
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(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

.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_messagesObject



146
147
148
149
150
151
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 146

def suppress_messages
  save, self.verbose = verbose, false
  yield
ensure
  self.verbose = save
end

.up_with_benchmarksObject

: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