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.config.timestamped_migrations = false

In environment.rb.

Constant Summary collapse

@@verbose =
true

Class Method Summary collapse

Class Method Details

.announce(message) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 117

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



145
146
147
148
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 145

def connection
  # ActiveRecord::Base.connection
  ::Mongoid.default_session
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

.method_missing(method, *arguments, &block) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 150

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



125
126
127
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 125

def say(message, subitem=false)
  write "#{subitem ? "   ->" : "--"} #{message}"
end

.say_with_time(message) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 129

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.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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_chain, sym, "benchmarks")
    end
  ensure
    @ignore_new_methods = false
  end
end

.suppress_messagesObject



138
139
140
141
142
143
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 138

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



113
114
115
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 113

def write(text="")
  puts(text) if verbose
end