Module: Exodus
- Defined in:
- lib/exodus.rb,
lib/exodus/version.rb,
lib/exodus/migrations/migration.rb,
lib/exodus/config/migration_info.rb,
lib/exodus/migrations/migration_error.rb,
lib/exodus/migrations/migration_status.rb
Defined Under Namespace
Classes: Migration, MigrationError, MigrationInfo, MigrationStatus
Constant Summary collapse
- VERSION =
"1.0.2"
Class Attribute Summary collapse
-
.migrations_info ⇒ Object
readonly
Returns the value of attribute migrations_info.
Class Method Summary collapse
- .configuration ⇒ Object
- .configure {|configuration| ... } ⇒ Object
-
.load_migrations ⇒ Object
Loads existing migrations into memory.
-
.order_with_direction(migrations, direction) ⇒ Object
Migrations order need to be reverted if the direction is down (we want the latest executed migration to be the first reverted).
-
.run_each(direction, migrations) ⇒ Object
Runs each migration separately, migration’s arguments default value is set to an empty hash.
-
.run_migrations(direction, migrations, step = nil) ⇒ Object
Executes a number of migrations equal to step (or all of them if step is nil).
-
.run_one_migration(migration_class, direction, args) ⇒ Object
Looks up in the database if a migration with the same class and same arguments already exists Otherwise instanciate a new one Runs the migration if it is runnable.
- .sort_migrations(migrations) ⇒ Object
-
.tasks ⇒ Object
Returns the path of the rake file.
Class Attribute Details
.migrations_info ⇒ Object (readonly)
Returns the value of attribute migrations_info.
9 10 11 |
# File 'lib/exodus.rb', line 9 def migrations_info @migrations_info end |
Class Method Details
.configuration ⇒ Object
11 12 13 |
# File 'lib/exodus.rb', line 11 def configuration @migrations_info ||= MigrationInfo.new end |
.configure {|configuration| ... } ⇒ Object
15 16 17 |
# File 'lib/exodus.rb', line 15 def configure yield(configuration) if block_given? end |
.load_migrations ⇒ Object
Loads existing migrations into memory
20 21 22 23 |
# File 'lib/exodus.rb', line 20 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, direction) ⇒ Object
Migrations order need to be reverted if the direction is down (we want the latest executed migration to be the first reverted)
44 45 46 47 |
# File 'lib/exodus.rb', line 44 def order_with_direction(migrations, direction) sorted_migrations = sort_migrations(migrations) direction == Migration::UP ? sorted_migrations : sorted_migrations.reverse end |
.run_each(direction, migrations) ⇒ Object
Runs each migration separately, migration’s arguments default value is set to an empty hash
54 55 56 57 58 |
# File 'lib/exodus.rb', line 54 def run_each(direction, migrations) migrations.each do |migration_class, args| print_tabulation { run_one_migration(migration_class, direction, args || {}) } end end |
.run_migrations(direction, migrations, step = nil) ⇒ Object
Executes a number of migrations equal to step (or all of them if step is nil)
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/exodus.rb', line 31 def run_migrations(direction, migrations, step = nil) if migrations sorted_migrations = order_with_direction(migrations, direction) sorted_migrations = sorted_migrations.shift(step.to_i) if step run_each(direction, sorted_migrations) else puts "no migrations given in argument!" end end |
.run_one_migration(migration_class, direction, args) ⇒ Object
Looks up in the database if a migration with the same class and same arguments already exists Otherwise instanciate a new one Runs the migration if it is runnable
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/exodus.rb', line 63 def run_one_migration(migration_class, direction, args) # Going throught MRD because MM request returns nil for some reason current_migration = migration_class.load(migration_class.collection.find('status.arguments' => args).first) current_migration ||= migration_class.new(:status => {:arguments => args}) if current_migration.is_runnable?(direction) # Make sure we save all info in case of a failure begin current_migration.run(direction) current_migration.status.error = nil rescue Exception => e current_migration.failure = e current_migration.save! raise end current_migration.save! else puts "#{current_migration.class}#{current_migration.status.arguments}(#{direction}) as Already been run (or is not runnable)." end end |
.sort_migrations(migrations) ⇒ Object
49 50 51 |
# File 'lib/exodus.rb', line 49 def sort_migrations(migrations) migrations.sort_by {|migration,args| migration.migration_number } end |
.tasks ⇒ Object
Returns the path of the rake file
26 27 28 |
# File 'lib/exodus.rb', line 26 def tasks File.dirname(__FILE__) + '/../tasks/exodus.rake' end |