Module: Webhookdb::Console

Extended by:
MethodUtilities
Defined in:
lib/webhookdb/console.rb

Defined Under Namespace

Modules: MainMethods Classes: Error, ForbiddenOperation, UnsafeOperation

Class Method Summary collapse

Methods included from MethodUtilities

attr_predicate, attr_predicate_accessor, singleton_attr_accessor, singleton_attr_reader, singleton_attr_writer, singleton_method_alias, singleton_predicate_accessor, singleton_predicate_reader

Class Method Details

.console_logger(ev) ⇒ Object



58
59
60
# File 'lib/webhookdb/console.rb', line 58

def self.console_logger(ev)
  Webhookdb.logger.info "ConsoleLogger: [%s] %s %p" % [ev.id, ev.name, ev.payload]
end

.disable_safe_modeObject



44
45
46
# File 'lib/webhookdb/console.rb', line 44

def self.disable_safe_mode
  Webhookdb::Postgres::Model.db.define_singleton_method(:execute, &Webhookdb::Console.original_execute)
end

.enable_safe_modeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/webhookdb/console.rb', line 20

def self.enable_safe_mode
  Webhookdb::Console.original_execute = Webhookdb::Postgres::Model.db.method(:execute)
  Webhookdb::Postgres::Model.db.define_singleton_method(:execute) do |*args, &block|
    sql = args.first

    includes_where = sql.include?(" WHERE ")
    is_update = sql.start_with?("UPDATE ")
    is_delete = sql.start_with?("DELETE FROM")

    raise ForbiddenOperation, "TRUNCATE is forbidden" if sql.start_with?("TRUNCATE TABLE")

    raise ForbiddenOperation, "UPDATE without a WHERE is forbidden" if
      is_update && !includes_where

    raise ForbiddenOperation, "DELETE without a WHERE is forbidden" if
      is_delete && !includes_where

    raise UnsafeOperation, "DELETE is permitted only in an UNSAFE block" if
      is_delete && includes_where && !Webhookdb::Console.unsafe_mode

    Webhookdb::Console.original_execute.call(*args, &block)
  end
end

.unsafe(&block) ⇒ Object

Raises:

  • (LocalJumpError)


48
49
50
51
52
53
54
55
56
# File 'lib/webhookdb/console.rb', line 48

def self.unsafe(&block)
  raise LocalJumpError, "unsafe must be called with a block (do ... end)" unless block
  self.unsafe_mode = true
  begin
    Webhookdb::Postgres::Model.db.transaction(&block)
  ensure
    self.unsafe_mode = false
  end
end