module Exodus
Constants
- VERSION
Attributes
Public Class Methods
# File lib/exodus.rb, line 12 def configuration @migrations_info ||= MigrationInfo.new end
# File lib/exodus.rb, line 16 def configure yield(configuration) if block_given? end
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
# 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.load(migration) return existing_migration if existing_migration.is_a?(migration_class) end end
Instanciates all of the migrations and returns the ones that are runnable
# 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
Database lookup to find a migration given its class and its arguments Instanciates it if the migration is not present in the database
# 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
Loads existing migrations into memory
# 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
Migrations order need to be reverted if the direction is down (we want the latest executed migration to be the first reverted)
# 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
Runs each migration separately, migration’s arguments default value is set to an empty hash
# File lib/exodus.rb, line 70 def run_migrations(direction, migrations) migrations.each do |migration| print_tabulation { run_one_migration(migration, direction) } end end
Runs the migration and save the current status into mongo
# 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
Sorts and executes a number of migrations equal to step (or all of them if step is nil)
# 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
Sorts migrations using the migration number
# File lib/exodus.rb, line 65 def sort_migrations(migrations_info) migrations_info.sort_by {|migration,args| migration.migration_number } end
Returns the path of the rake file
# File lib/exodus.rb, line 27 def tasks File.dirname(__FILE__) + '/../tasks/exodus.rake' end
Private Class Methods
Prints tabulation before execting a given block
# File lib/exodus.rb, line 111 def print_tabulation puts "\n" yield if block_given? puts "\n" end