Class: Exodus::Migration
- Inherits:
-
Object
- Object
- Exodus::Migration
- Extended by:
- TextFormatter
- 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
- #characteristic ⇒ Object
-
#completed?(direction) ⇒ Boolean
Checks if a migration as been completed.
- #eql?(other_migration) ⇒ Boolean
-
#failure=(exception) ⇒ Object
Sets an error to migration status.
- #hash ⇒ Object
-
#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.
Methods included from TextFormatter
Constructor Details
#initialize(args = {}) ⇒ Migration
Makes sure status get instanciated on migration’s instanciation
96 97 98 99 |
# File 'lib/exodus/migrations/migration.rb', line 96 def initialize(args = {}) self.build_status(args[:status]) super(args) end |
Class Attribute Details
.migration_number ⇒ Object
Returns the value of attribute migration_number.
20 21 22 |
# File 'lib/exodus/migrations/migration.rb', line 20 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 |
# File 'lib/exodus/migrations/migration.rb', line 84 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 = {}) ⇒ 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
67 68 69 70 |
# File 'lib/exodus/migrations/migration.rb', line 67 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
24 25 26 27 28 29 |
# File 'lib/exodus/migrations/migration.rb', line 24 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
73 74 75 76 77 78 79 80 81 |
# File 'lib/exodus/migrations/migration.rb', line 73 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) ⇒ 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’]]
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/exodus/migrations/migration.rb', line 40 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’]]
60 61 62 63 |
# File 'lib/exodus/migrations/migration.rb', line 60 def load_custom(migrations) migrations = migrations || [] migrations.map {|migration_str, args| format(migration_str, args) }.uniq end |
.sort_all ⇒ Object
Sorts all migrations by migration number
32 33 34 |
# File 'lib/exodus/migrations/migration.rb', line 32 def sort_all @migrations.sort_by {|migration,args| migration.migration_number } end |
Instance Method Details
#characteristic ⇒ Object
128 129 130 |
# File 'lib/exodus/migrations/migration.rb', line 128 def characteristic "#{self.class}: #{self.status.arguments}" end |
#completed?(direction) ⇒ Boolean
Checks if a migration as been completed
123 124 125 126 |
# File 'lib/exodus/migrations/migration.rb', line 123 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) ⇒ Boolean
132 133 134 |
# File 'lib/exodus/migrations/migration.rb', line 132 def eql?(other_migration) self.class == other_migration.class && self.status.arguments == other_migration.status.arguments end |
#failure=(exception) ⇒ Object
Sets an error to migration status
113 114 115 |
# File 'lib/exodus/migrations/migration.rb', line 113 def failure=(exception) self.status.error = MigrationError.new(:error_message => exception., :error_class => exception.class, :error_backtrace => exception.backtrace) end |
#hash ⇒ Object
136 137 138 |
# File 'lib/exodus/migrations/migration.rb', line 136 def hash self.class.hash ^ self.status.arguments.hash end |
#is_runnable?(direction) ⇒ Boolean
Checks if a migration can be run
118 119 120 |
# File 'lib/exodus/migrations/migration.rb', line 118 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
103 104 105 106 107 108 109 110 |
# File 'lib/exodus/migrations/migration.rb', line 103 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.rerunnable_safe && completed?(direction) self.status.execution_time = time_it { self.send(direction) } self.status.last_succesful_completion = Time.now end |