Sha256: 13507548d2493626e5d30e61d25cf4e36e2681f506f0b2d6ae2345f2fd760d1d

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

namespace :handcuffs do
  task :migrate, [:phase] => :environment do |t,args|
    phase = setup(args, 'handcuffs:migrate')
    patch_migrator!(phase)
    run_task('db:migrate')
  end

  task :rollback, [:phase] => :environment do |t,args|
    phase = setup(args, 'handcuffs:rollback')
    patch_migrator!(phase)
    run_task('db:rollback')
  end

  def setup(args, task)
    phase = args.phase
    raise RequiresPhaseArgumentError.new(task) unless phase.present?
    raise HandcuffsNotConfiguredError.new unless Handcuffs.config
    phase = phase.to_sym
    unless Handcuffs.config.phases.include?(phase) || phase == :all
      raise HandcuffsUnknownPhaseError.new(phase, Handcuffs.config.phases)
    end
    phase
  end

  def patch_migrator!(phase)
    ActiveRecord::Migrator.prepend(PendingFilter)
    ActiveRecord::Migrator.extend(PhaseAccessor)
    ActiveRecord::Migrator.handcuffs_phase = phase
  end

  def run_task(name)
    Rake::Task.clear # necessary to avoid tasks being loaded several times in dev mode
    Rails.application.load_tasks 
    Rake::Task[name].reenable # in case you're going to invoke the same task second time.
    Rake::Task[name].invoke
  end

  module PendingFilter
    def runnable
      attempted_phase = self.class.handcuffs_phase
      if(@direction == :up)
        Handcuffs::PhaseFilter.new(attempted_phase, @direction).filter(super)
      else
        phase_migrations = Handcuffs::PhaseFilter.new(attempted_phase, @direction).filter(migrations)
        runnable = phase_migrations[start..finish]
        runnable.pop if target
        runnable.find_all { |m| ran?(m) }
      end
    end
  end

  module PhaseAccessor
    attr_accessor :handcuffs_phase
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
handcuffs-2.0.0 lib/tasks/handcuffs.rake