Class: Rubyists::PgtOutbox::Function

Inherits:
Object
  • Object
show all
Includes:
Rubyists::PgtOutbox
Defined in:
lib/sequel/pgt_outbox/function.rb

Overview

The Outbox Function

Constant Summary collapse

DEFAULT_OPTS =
{ language: :plpgsql, returns: :trigger, replace: true }.freeze

Constants included from Rubyists::PgtOutbox

DEFINITION, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rubyists::PgtOutbox

#depth_guard_clause, #mangled_table_name, outbox_table

Constructor Details

#initialize(outbox, opts: {}) ⇒ Function

Returns a new instance of Function.



19
20
21
22
23
# File 'lib/sequel/pgt_outbox/function.rb', line 19

def initialize(outbox, opts: {})
  @outbox = outbox
  @db = outbox.db
  @opts = opts
end

Class Method Details

.create!(outbox, opts: {}) ⇒ Object



15
16
17
# File 'lib/sequel/pgt_outbox/function.rb', line 15

def self.create!(outbox, opts: {})
  new(outbox, opts: opts).create!
end

Instance Method Details

#create!Object



33
34
35
36
# File 'lib/sequel/pgt_outbox/function.rb', line 33

def create!
  db.create_function(name, function_sql, function_opts)
  self
end

#function_sqlObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sequel/pgt_outbox/function.rb', line 38

def function_sql
  "    BEGIN\n      \#{depth_guard_clause}\n      IF (TG_OP = 'INSERT') THEN\n          INSERT INTO \#{quoted_name} (\"\#{event_type_column}\", \"\#{data_after_column}\") VALUES\n          ('\#{event_prefix}_created', to_jsonb(NEW));\n          RETURN NEW;\n      ELSIF (TG_OP = 'UPDATE') THEN\n          INSERT INTO \#{quoted_name} (\"\#{event_type_column}\", \"\#{data_before_column}\", \"\#{data_after_column}\") VALUES\n          ('\#{event_prefix}_updated', to_jsonb(OLD), to_jsonb(NEW));\n          RETURN NEW;\n      ELSIF (TG_OP = 'DELETE') THEN\n          INSERT INTO \#{quoted_name} (\"\#{event_type_column}\", \"\#{data_before_column}\") VALUES\n          ('\#{event_prefix}_deleted', to_jsonb(OLD));\n          RETURN OLD;\n      END IF;\n    END;\n  SQL\nend\n"

#nameObject



25
26
27
# File 'lib/sequel/pgt_outbox/function.rb', line 25

def name
  @name = opts.fetch(:function_name, "pgt_outbox_#{mangled_table_name(db, outbox.name)}")
end