Sha256: 1730fde81397605b61fc0809ae17b99a1228399eeb59388eebac5b8b7cdc0e97

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

require 'active_record'
require 'timers'
require 'border_patrol/configuration'

module BorderPatrol
  extend self

  attr_accessor :configuration

  def configure
    self.configuration ||= Configuration.new
    yield configuration if block_given?

    # Don't perform the tests in console mode, unless requested.
    return if console? && configuration.ignore_console

    # Perform initial test.
    abort_if_pending

    # Unless requested otherwise, start polling for new migrations and if new migrations were pended
    # indeed - terminate the server.
    start if configuration.terminate
  end

  at_exit do
    @thread.exit if @thread
  end

  private
  def pending_migrations
    ActiveRecord::Migrator.new(:up, ActiveRecord::Migrator.migrations_paths).pending_migrations
  end

  def abort_if_pending
    if pending_migrations.any?
      puts 'Border Patrol:'
      puts "You have #{pending_migrations.size} pending migrations:"
      pending_migrations.each do |pending_migration|
        puts '  %4d %s' % [pending_migration.version, pending_migration.name]
      end
      puts 'Run `rake db:migrate` to update your database then try again.'

      stop
    end
  end

  def console?
    defined?(Rails::Console)
  end

  def start
    @thread = Thread.new do
      @timers = Timers.new
      @timers.every(configuration.polling_period) { abort_if_pending }

      loop do
        @timers.wait
      end
    end
  end

  def stop
    abort
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
border-patrol-0.0.6 lib/border-patrol.rb
border-patrol-0.0.5 lib/border-patrol.rb
border-patrol-0.0.4 lib/border-patrol.rb