Class: Exodus::Migration
- Inherits:
-
Object
- Object
- Exodus::Migration
- Includes:
- MongoMapper::Document
- Defined in:
- lib/exodus/migrations/migration.rb
Constant Summary collapse
- UP =
'up'
- DOWN =
'down'
Class Attribute Summary collapse
-
.migration_number ⇒ Object
Returns the value of attribute migration_number.
Class Method Summary collapse
-
.db_status ⇒ Object
Prints in the console all migrations that has been ran at least once with their name and description.
-
.format(migration, args = {}) ⇒ Object
Formats a given migration making sure the first argument is a class and the second one -if it exists- is a none empty hash.
-
.inherited(klass) ⇒ Object
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.
-
.list ⇒ Object
Prints in the console all migrations class with their name and description.
-
.load_all(migrations) ⇒ Object
Using a list of migrations Formats and overrides migrations without arguments using ones that have given arguments Removes duplicates migrations: list of migrations => [[MyMigration, => ‘some_args’]].
-
.load_custom(migrations) ⇒ Object
Using a list of migrations formats them and removes duplicates migrations: list of migrations => [[MyMigration, => ‘some_args’]].
-
.sort_all ⇒ Object
Sorts all migrations by migration number.
Instance Method Summary collapse
-
#completed?(direction) ⇒ Boolean
Checks if a migration as been completed.
-
#failure=(exception) ⇒ Object
Sets an error to migration status.
-
#initialize(args = {}) ⇒ Migration
constructor
Makes sure status get instanciated on migration’s instanciation.
-
#is_runnable?(direction) ⇒ Boolean
Checks if a migration can be run.
-
#run(direction) ⇒ Object
Runs the migration following the direction sets the status, the execution time and the last succesful_completion date.
Constructor Details
#initialize(args = {}) ⇒ Migration
Makes sure status get instanciated on migration’s instanciation
97 98 99 100 |
# File 'lib/exodus/migrations/migration.rb', line 97 def initialize(args = {}) self.build_status(args[:status]) super(args) end |
Class Attribute Details
.migration_number ⇒ Object
Returns the value of attribute migration_number.
19 20 21 |
# File 'lib/exodus/migrations/migration.rb', line 19 def migration_number @migration_number end |
Class Method Details
.db_status ⇒ Object
Prints in the console all migrations that has been ran at least once with their name and description
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/exodus/migrations/migration.rb', line 84 def db_status puts "\n Migration n#: \t Name: \t\t Direction: Arguments: Current Status: \t Last completion Date: \t\t Current Message:" puts '-' * 175, "\n" Migration.all.each do|migration| puts "\t#{migration.class.migration_number} \t #{migration.class.name} \t #{migration.status.to_string}" end puts "\n\n" end |
.format(migration, args = {}) ⇒ Object
Formats a given migration making sure the first argument is a class and the second one -if it exists- is a none empty hash
65 66 67 68 |
# File 'lib/exodus/migrations/migration.rb', line 65 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) ⇒ Object
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
23 24 25 26 27 28 |
# File 'lib/exodus/migrations/migration.rb', line 23 def inherited(klass) klass. if defined?(MongoMapper::Plugins::EmbeddedCallbacks::ClassMethods) #MongoMapper version compatibility klass.migration_number = 0 @migrations << [klass] super(klass) end |
.list ⇒ Object
Prints in the console all migrations class with their name and description
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/exodus/migrations/migration.rb', line 71 def list puts "\n Migration n#: \t\t Name: \t\t\t\t Description:" puts '-' * 100, "\n" Migration.sort_all.map do|migration, args| m = migration.new puts "\t#{migration.migration_number} \t\t #{migration.name} \t\t #{m.description}" end puts "\n\n" end |
.load_all(migrations) ⇒ Object
Using a list of migrations Formats and overrides migrations without arguments using ones that have given arguments Removes duplicates migrations: list of migrations => [[MyMigration, => ‘some_args’]]
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/exodus/migrations/migration.rb', line 39 def load_all(migrations) if migrations migrations.each do |migration, args| if migration && args 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) ⇒ Object
Using a list of migrations formats them and removes duplicates migrations: list of migrations => [[MyMigration, => ‘some_args’]]
59 60 61 |
# File 'lib/exodus/migrations/migration.rb', line 59 def load_custom(migrations) migrations.map {|migration_str, args| format(migration_str, args) }.uniq end |
.sort_all ⇒ Object
Sorts all migrations by migration number
31 32 33 |
# File 'lib/exodus/migrations/migration.rb', line 31 def sort_all @migrations.sort_by {|migration,args| migration.migration_number } end |
Instance Method Details
#completed?(direction) ⇒ Boolean
Checks if a migration as been completed
124 125 126 127 |
# File 'lib/exodus/migrations/migration.rb', line 124 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 |
#failure=(exception) ⇒ Object
Sets an error to migration status
114 115 116 |
# File 'lib/exodus/migrations/migration.rb', line 114 def failure=(exception) self.status.error = MigrationError.new(:error_message => exception., :error_class => exception.class, :error_backtrace => exception.backtrace) end |
#is_runnable?(direction) ⇒ Boolean
Checks if a migration can be run
119 120 121 |
# File 'lib/exodus/migrations/migration.rb', line 119 def is_runnable?(direction) rerunnable_safe || (direction == UP && status.current_status < status_complete) || (direction == DOWN && status.current_status > 0) end |
#run(direction) ⇒ Object
Runs the migration following the direction sets the status, the execution time and the last succesful_completion date
104 105 106 107 108 109 110 111 |
# File 'lib/exodus/migrations/migration.rb', line 104 def run(direction) self.status.direction = direction # reset the status if the job is rerunnable and has already be completed self.status = self.status.reset if self.rerunnable_safe && completed?(direction) self.status.execution_time = time_it { self.send(direction) } self.status.last_succesful_completion = Time.now end |