class Exodus::Migration

Constants

DOWN
UP

Attributes

migration_number[RW]

Public Class Methods

db_status() click to toggle source

Prints in the console all migrations that has been ran at least once with their name and description

# File lib/exodus/migrations/migration.rb, line 83
def db_status
  status_info = [["Migration n#:", "Name:", "Direction:", "Current Status:", "Arguments:", "Last completion Date:", "Current Message:"]]

  status_info |= Migration.all.map do|migration|
    [migration.class.migration_number, migration.class.name, *migration.status.to_a_string]
  end

  super_print(status_info)
end
format(migration, args = {}) click to toggle source

Formats a given migration making sure the first argument is a class and the second one -if it exists- is a none empty hash

# File lib/exodus/migrations/migration.rb, line 66
def format(migration, args = {})
  migration_klass = migration.is_a?(String) ? migration.constantize : migration
  args.is_a?(Hash) && args.empty? ? [migration_klass] : [migration_klass, args]
end
inherited(klass) click to toggle source

Overides inherited to have an easy and reliable way to find all migrations Migrations need to have embedded callbacks on depending on the MM’s version

Calls superclass method
# File lib/exodus/migrations/migration.rb, line 23
def inherited(klass)
  klass.embedded_callbacks_on if defined?(MongoMapper::Plugins::EmbeddedCallbacks::ClassMethods) #MongoMapper version compatibility
  klass.migration_number = 0
  @migrations << [klass]
  super(klass)
end
list() click to toggle source

Prints in the console all migrations class with their name and description

# File lib/exodus/migrations/migration.rb, line 72
def list
  last_info = [["Migration n#:", "Name:", "Description:"]]

  last_info |= Migration.sort_all.map do|migration, args|
    [migration.migration_number, migration.name, migration.new.description]
  end

  super_print(last_info, 70)
end
load_all(migrations) click to toggle source

Using a list of migrations Formats and overrides migrations without arguments using ones that have given arguments Removes duplicates migrations: list of migrations => [[MyMigration, {:my_args => ‘some_args’}]]

# File lib/exodus/migrations/migration.rb, line 39
def load_all(migrations)
  if migrations
    migrations.each do |migration, args|
      if migration
        formated_migration = format(migration, args || {})
        migration, args = formated_migration

        unless @migrations.include?(formated_migration)
          @migrations.delete_if {|loaded_migration, loaded_args| migration == loaded_migration && (loaded_args.nil? || loaded_args.empty?) }
          @migrations << formated_migration
        end
      end
    end 
  end

  @migrations
end
load_custom(migrations) click to toggle source

Using a list of migrations formats them and removes duplicates migrations: list of migrations => [[MyMigration, {:my_args => ‘some_args’}]]

# File lib/exodus/migrations/migration.rb, line 59
def load_custom(migrations)
  migrations ||= []
  migrations.map {|migration_str, args| format(migration_str, args) }.uniq
end
new(args = {}) click to toggle source

Makes sure status get instanciated on migration’s instanciation

Calls superclass method
# File lib/exodus/migrations/migration.rb, line 107
def initialize(args = {})
  self.build_status(args[:status])
  super(args)
end
rerunnable_safe=(safe) click to toggle source
# File lib/exodus/migrations/migration.rb, line 93
def rerunnable_safe=(safe)
  migrations = Migration.instance_variable_get("@migrations")
  deletion = migrations.delete([self])
  Migration.instance_variable_set("@migrations", migrations) if deletion

  @rerunnable_safe = safe
end
rerunnable_safe?() click to toggle source
# File lib/exodus/migrations/migration.rb, line 101
def rerunnable_safe?
  @rerunnable_safe == true
end
sort_all() click to toggle source

Sorts all migrations by migration number

# File lib/exodus/migrations/migration.rb, line 31
def sort_all
  @migrations.sort_by {|migration,args| migration.migration_number }
end

Public Instance Methods

characteristic() click to toggle source
# File lib/exodus/migrations/migration.rb, line 145
def characteristic
  "#{self.class}: #{self.status.arguments}"
end
completed?(direction) click to toggle source

Checks if a migration as been completed

# File lib/exodus/migrations/migration.rb, line 139
def completed?(direction) 
  return false if self.status.execution_time == 0
  (direction == UP && self.status.current_status == self.status_complete) || 
  (direction == DOWN && self.status.current_status == 0)
end
eql?(other_migration) click to toggle source
# File lib/exodus/migrations/migration.rb, line 149
def eql?(other_migration)
  self.class == other_migration.class && self.status.arguments == other_migration.status.arguments
end
failure=(exception) click to toggle source

Sets an error to migration status

# File lib/exodus/migrations/migration.rb, line 124
def failure=(exception)
  self.status.error = MigrationError.new(
    :error_message => exception.message, 
    :error_class => exception.class, 
    :error_backtrace => exception.backtrace)
end
hash() click to toggle source
# File lib/exodus/migrations/migration.rb, line 153
def hash
  self.class.hash ^ self.status.arguments.hash
end
is_runnable?(direction) click to toggle source

Checks if a migration can be run

# File lib/exodus/migrations/migration.rb, line 132
def is_runnable?(direction)
  self.class.rerunnable_safe? || 
  (direction == UP && status.current_status < status_complete) || 
  (direction == DOWN && status.current_status > 0)
end
run(direction) click to toggle source

Runs the migration following the direction sets the status, the execution time and the last succesful_completion date

# File lib/exodus/migrations/migration.rb, line 114
def run(direction)
  self.status.direction = direction

  # reset the status if the job is rerunnable and has already be completed
  self.status.reset! if self.class.rerunnable_safe? && completed?(direction) 
  self.status.execution_time = time_it { self.send(direction) }
  self.status.last_succesful_completion = Time.now
end

Protected Instance Methods

down() click to toggle source

contains the code that will be executed when run(down) will be called

# File lib/exodus/migrations/migration.rb, line 194
def down
  raise StandardError, 'Needs to be implemented in child class.'
end
step(step_message = nil, step_status = 1) { || ... } click to toggle source

Executes a given block if the status has not being processed Then update the status

# File lib/exodus/migrations/migration.rb, line 161
def step(step_message = nil, step_status = 1)
  unless status.status_processed?(status.direction, step_status)
    self.status.message = step_message
    puts "\t #{step_message}" 

    yield if block_given?
    self.status.current_status += status.direction_to_i
  end
end
tick(msg) click to toggle source

Prints a given message with the current time

# File lib/exodus/migrations/migration.rb, line 172
def tick(msg)
  puts "#{Time.now}: #{msg}"
end
time_it() { || ... } click to toggle source

Executes a block and returns the time it took to be executed

# File lib/exodus/migrations/migration.rb, line 177
def time_it
  puts "Running #{self.class}[#{self.status.arguments}](#{self.status.direction})"

  start = Time.now
  yield if block_given?
  end_time = Time.now - start
  
  puts "Tasks #{self.class} executed in #{end_time} seconds. \n\n"
  end_time
end
up() click to toggle source

contains the code that will be executed when run(up) will be called

# File lib/exodus/migrations/migration.rb, line 189
def up
  raise StandardError, 'Needs to be implemented in child class.'
end