class Logidze::Utils::CheckPending

This Rack middleware is used to verify that all functions are up to date

Public Class Methods

new(app) click to toggle source
# File lib/logidze/utils/check_pending.rb, line 10
def initialize(app)
  @app = app
  @needs_check = true
  @mutex = Mutex.new
end

Public Instance Methods

call(env) click to toggle source
# File lib/logidze/utils/check_pending.rb, line 18
def call(env)
  @mutex.synchronize do
    if @needs_check
      notify_or_raise! if needs_migration?
    end
    @needs_check = false
  end

  @app.call(env)
end

Private Instance Methods

library_function_versions() click to toggle source
# File lib/logidze/utils/check_pending.rb, line 52
def library_function_versions
  @library_function_versions ||= Logidze::Utils::FunctionDefinitions.from_fs.map { |func| [func.name, func.version] }
end
needs_migration?() click to toggle source
# File lib/logidze/utils/check_pending.rb, line 44
def needs_migration?
  (library_function_versions - pg_function_versions).any?
end
notify_or_raise!() click to toggle source
# File lib/logidze/utils/check_pending.rb, line 31
def notify_or_raise!
  case Logidze.on_pending_upgrade
  when :warn
    warn "\n**************************************************\n" \
         "⛔️  WARNING: Logidze needs an upgrade and might not work correctly.\n" \
         "Please, make sure to run `bundle exec rails generate logidze:install --update` " \
         "and apply generated migration." \
         "\n**************************************************\n\n"
  when :raise
    raise Logidze::Utils::PendingMigrationError, "Logidze needs upgrade. Run `bundle exec rails generate logidze:install --update` and apply generated migration."
  end
end
pg_function_versions() click to toggle source
# File lib/logidze/utils/check_pending.rb, line 48
def pg_function_versions
  Logidze::Utils::FunctionDefinitions.from_db.map { |func| [func.name, func.version] }
end