lib/exodus/migrations/migration.rb in exodus-1.1.5 vs lib/exodus/migrations/migration.rb in exodus-1.1.6

- old
+ new

@@ -10,11 +10,10 @@ timestamps! key :description, String key :status_complete, Integer, :default => 1 - key :rerunnable_safe, Boolean, :default => false # Be careful if the job is rerunnable_safe he will re-run on each db:migrate has_one :status, :class_name => "Exodus::MigrationStatus", :autosave => true class << self attr_accessor :migration_number @@ -38,12 +37,12 @@ # Removes duplicates # migrations: list of migrations => [[MyMigration, {:my_args => 'some_args'}]] def load_all(migrations) if migrations migrations.each do |migration, args| - if migration && args - formated_migration = format(migration, args) + if migration + 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 @@ -56,11 +55,11 @@ end # Using a list of migrations formats them and removes duplicates # migrations: list of migrations => [[MyMigration, {:my_args => 'some_args'}]] def load_custom(migrations) - migrations = migrations || [] + migrations ||= [] migrations.map {|migration_str, args| format(migration_str, args) }.uniq end # Formats a given migration making sure the first argument is a class # and the second one -if it exists- is a none empty hash @@ -88,10 +87,22 @@ [migration.class.migration_number, migration.class.name, *migration.status.to_a_string] end super_print(status_info) end + + def rerunnable_safe=(safe) + migrations = Migration.instance_variable_get("@migrations") + deletion = migrations.delete([self]) + Migration.instance_variable_set("@migrations", migrations) if deletion + + @rerunnable_safe = safe + end + + def rerunnable_safe? + @rerunnable_safe == true + end end # Makes sure status get instanciated on migration's instanciation def initialize(args = {}) self.build_status(args[:status]) @@ -102,28 +113,34 @@ # sets the status, the execution time and the last succesful_completion date 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.reset! if self.class.rerunnable_safe? && completed?(direction) self.status.execution_time = time_it { self.send(direction) } self.status.last_succesful_completion = Time.now end # Sets an error to migration status def failure=(exception) - self.status.error = MigrationError.new(:error_message => exception.message, :error_class => exception.class, :error_backtrace => exception.backtrace) + self.status.error = MigrationError.new( + :error_message => exception.message, + :error_class => exception.class, + :error_backtrace => exception.backtrace) end # Checks if a migration can be run def is_runnable?(direction) - rerunnable_safe || (direction == UP && status.current_status < status_complete) || (direction == DOWN && status.current_status > 0) + self.class.rerunnable_safe? || + (direction == UP && status.current_status < status_complete) || + (direction == DOWN && status.current_status > 0) end # Checks if a migration as been completed 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) + (direction == UP && self.status.current_status == self.status_complete) || + (direction == DOWN && self.status.current_status == 0) end def characteristic "#{self.class}: #{self.status.arguments}" end \ No newline at end of file