Class: MongoidMigration::Migration

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/mongoid_migration/migration.rb

Constant Summary collapse

@@verbose =
true

Class Method Summary collapse

Class Method Details

.announce(message) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/mongoid_migration/migration.rb', line 163

def announce(message)
  version = defined?(@version) ? @version : nil

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

.copy(destination, sources, options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mongoid_migration/migration.rb', line 74

def copy(destination, sources, options = {})
  copied = []
  FileUtils.mkdir_p(destination) unless File.exists?(destination)

  destination_migrations = MongoidMigration::Migrator.new(:up, destination).migrations
  last = destination_migrations.last
  sources.each do |scope, path|
    source_migrations = MongoidMigration::Migrator.new(:up, path).migrations

    source_migrations.each do |migration|
      source = File.read(migration.filename)
      source = "# This migration comes from #{scope} (originally #{migration.version})\n#{source}"

      if duplicate = destination_migrations.detect { |m| m.name == migration.name }
        if options[:on_skip] && duplicate.scope != scope.to_s
          options[:on_skip].call(scope, migration)
        end
        next
      end

      migration.version = next_migration_number(last ? last.version + 1 : 0).to_i
      new_path = File.join(destination, "#{migration.version}_#{migration.name.underscore}.#{scope}.rb")
      old_path, migration.filename = migration.filename, new_path
      last = migration

      File.open(migration.filename, "w") { |f| f.write source }
      copied << migration
      options[:on_copy].call(scope, migration, old_path) if options[:on_copy]
      destination_migrations << migration
    end
  end

  copied
end

.down_with_benchmarksObject

:nodoc:



117
118
119
# File 'lib/mongoid_migration/migration.rb', line 117

def down_with_benchmarks #:nodoc:
  migrate(:down)
end

.migrate(direction) ⇒ Object

Execute this migration in the named direction



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mongoid_migration/migration.rb', line 122

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

.next_migration_number(number) ⇒ Object



109
110
111
# File 'lib/mongoid_migration/migration.rb', line 109

def next_migration_number(number)
  [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % number].max
end

.say(message, subitem = false) ⇒ Object



171
172
173
# File 'lib/mongoid_migration/migration.rb', line 171

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

.say_with_time(message) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/mongoid_migration/migration.rb', line 175

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.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/mongoid_migration/migration.rb', line 144

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



184
185
186
187
188
189
# File 'lib/mongoid_migration/migration.rb', line 184

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

.up_with_benchmarksObject

:nodoc:



113
114
115
# File 'lib/mongoid_migration/migration.rb', line 113

def up_with_benchmarks #:nodoc:
  migrate(:up)
end

.write(text = "") ⇒ Object



159
160
161
# File 'lib/mongoid_migration/migration.rb', line 159

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