Module: Exodus

Defined in:
lib/exodus.rb,
lib/exodus/version.rb,
lib/exodus/helpers/rake_helper.rb,
lib/exodus/migrations/migration.rb,
lib/exodus/config/migration_info.rb,
lib/exodus/helpers/text_formatter.rb,
lib/exodus/migrations/migration_error.rb,
lib/exodus/migrations/migration_status.rb

Defined Under Namespace

Modules: RakeHelper, TextFormatter Classes: Migration, MigrationError, MigrationInfo, MigrationStatus

Constant Summary collapse

VERSION =
"1.1.4"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.migrations_infoObject (readonly)

Returns the value of attribute migrations_info.



10
11
12
# File 'lib/exodus.rb', line 10

def migrations_info
  @migrations_info
end

Class Method Details

.configurationObject



12
13
14
# File 'lib/exodus.rb', line 12

def configuration
  @migrations_info ||= MigrationInfo.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



16
17
18
# File 'lib/exodus.rb', line 16

def configure
  yield(configuration) if block_given?
end

.find_existing_migration(migration_class, args) ⇒ Object

Looks up in the database if a migration with the same class and same arguments already exists Returns nil or the migration if one is found



99
100
101
102
103
104
105
106
# File 'lib/exodus.rb', line 99

def find_existing_migration(migration_class, args)
  existing_migrations = migration_class.collection.find('status.arguments' => args)
  existing_migrations.detect do |migration|
    existing_migration = migration_class.load(migration)

  return existing_migration if existing_migration.is_a?(migration_class)
  end
end

.find_runable_migrations(direction, migrations_info, step) ⇒ Object

Instanciates all of the migrations and returns the ones that are runnable



48
49
50
51
52
53
54
55
# File 'lib/exodus.rb', line 48

def find_runable_migrations(direction, migrations_info, step)
  runnable_migrations = migrations_info.map do |migration_class, args| 
    migration = instanciate_migration(migration_class, args)
    migration if migration.is_runnable?(direction)
  end.compact.uniq

  step ? runnable_migrations.shift(step.to_i) : runnable_migrations
end

.instanciate_migration(migration_class, args) ⇒ Object

Database lookup to find a migration given its class and its arguments Instanciates it if the migration is not present in the database



92
93
94
95
# File 'lib/exodus.rb', line 92

def instanciate_migration(migration_class, args)
  args ||= {}
  find_existing_migration(migration_class, args) || migration_class.new(:status => {:arguments => args})
end

.load_migrationsObject

Loads existing migrations into memory

Raises:

  • (StandardError)


21
22
23
24
# File 'lib/exodus.rb', line 21

def load_migrations
  raise StandardError, 'A migrations directory is needed in order to load migrations.' unless migrations_info.migrations_directory
  Dir[migrations_info.migrations_directory + '/*.rb'].each { |file|  require file}
end

.order_with_direction(migrations_info, direction) ⇒ Object

Migrations order need to be reverted if the direction is down (we want the latest executed migration to be the first reverted)



59
60
61
62
# File 'lib/exodus.rb', line 59

def order_with_direction(migrations_info, direction)
  sorted_migrations = sort_migrations(migrations_info)
  direction == Migration::UP ? sorted_migrations : sorted_migrations.reverse 
end

.run_migrations(direction, migrations) ⇒ Object

Runs each migration separately, migration’s arguments default value is set to an empty hash



70
71
72
73
74
# File 'lib/exodus.rb', line 70

def run_migrations(direction, migrations)
  migrations.each do |migration|
  print_tabulation { run_one_migration(migration, direction) }
 end
end

.run_one_migration(migration, direction) ⇒ Object

Runs the migration and save the current status into mongo



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/exodus.rb', line 77

def run_one_migration(migration, direction)
  begin
    migration.run(direction)
    migration.status.error = nil
  rescue Exception => e
    migration.failure = e
    migration.save!
    raise
  end

  migration.save!
end

.sort_and_run_migrations(direction, migrations_info, step = nil, show_characteristic = false) ⇒ Object

Sorts and executes a number of migrations equal to step (or all of them if step is nil)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/exodus.rb', line 32

def sort_and_run_migrations(direction, migrations_info, step = nil, show_characteristic = false)      
  if migrations_info
    sorted_migrations_info = order_with_direction(migrations_info, direction)
    runnable_migrations = find_runable_migrations(direction, sorted_migrations_info, step)

    if show_characteristic 
      runnable_migrations.map(&:characteristic)
    else
      run_migrations(direction, runnable_migrations)
    end
  else
    raise StandardError, "no migrations given in argument!"
  end
end

.sort_migrations(migrations_info) ⇒ Object

Sorts migrations using the migration number



65
66
67
# File 'lib/exodus.rb', line 65

def sort_migrations(migrations_info)
  migrations_info.sort_by {|migration,args| migration.migration_number }
end

.tasksObject

Returns the path of the rake file



27
28
29
# File 'lib/exodus.rb', line 27

def tasks
  File.dirname(__FILE__) + '/../tasks/exodus.rake'
end