class Bleib::Migrations

Finds out if all migrations are up.

Knows how to handle multitenancy with Apartment, if used.

Public Class Methods

new(configuration) click to toggle source
# File lib/bleib/migrations.rb, line 6
def initialize(configuration)
  @configuration = configuration
end

Public Instance Methods

wait_until_done() click to toggle source
# File lib/bleib/migrations.rb, line 10
def wait_until_done
  logger.info('Waiting for migrations' \
               ' (Also checking apartment tenants:' \
               " #{apartment_gem? ? 'yes' : 'no'})")

  wait while pending_migrations?

  logger.info('All migrations are up')
end

Private Instance Methods

apartment_gem?() click to toggle source
# File lib/bleib/migrations.rb, line 48
def apartment_gem?
  defined?(Apartment::Tenant)
end
check_migrations!() click to toggle source
# File lib/bleib/migrations.rb, line 52
def check_migrations!
  ActiveRecord::Migration.check_pending!
end
in_all_tenant_contexts() { || ... } click to toggle source
# File lib/bleib/migrations.rb, line 56
def in_all_tenant_contexts
  tenants = [ENV.fetch('BLEIB_DEFAULT_TENANT', 'public')] +
            Apartment.tenant_names

  tenants.uniq.each do |tenant|
    Apartment::Tenant.switch(tenant) do
      yield
    end
  end
end
logger() click to toggle source
# File lib/bleib/migrations.rb, line 67
def logger
  @configuration.logger
end
pending_migrations?() click to toggle source
# File lib/bleib/migrations.rb, line 30
def pending_migrations?
  logger.debug('Checking migrations')

  if apartment_gem?
    in_all_tenant_contexts { check_migrations! }
  else
    check_migrations!
  end

  logger.debug('Migrations check succeeded.')

  false
rescue ActiveRecord::PendingMigrationError
  logger.debug('Migrations pending, check failed')

  true
end
wait() click to toggle source
# File lib/bleib/migrations.rb, line 22
def wait
  duration = @configuration.check_migrations_interval

  logger.debug "Waiting for #{@configuration.check_migrations_interval} seconds"

  sleep(duration)
end