Module: ActiveRecord::Materialized::WriteOutbox::Triggers::SharedSql

Defined in:
lib/activerecord/materialized/write_outbox.rb

Overview

Dialect-agnostic SQL fragments: identifier naming, the scoped-key JSON expression, the per-operation descriptors, and the shared INSERT INTO outbox ... VALUES every body wraps.

Class Method Summary collapse

Class Method Details

.insert_values(connection, table, op_spec) ⇒ Object

The INSERT INTO outbox ... VALUES (...) fragment shared by every dialect's trigger body. CURRENT_TIMESTAMP is portable across all three adapters.



234
235
236
237
238
239
240
# File 'lib/activerecord/materialized/write_outbox.rb', line 234

def insert_values(connection, table, op_spec)
  outbox = connection.quote_table_name(WriteOutbox.table_name)
  <<~SQL.squish
    INSERT INTO #{outbox} (source_table, operation, key_before, key_after, created_at)
    VALUES (#{connection.quote(table)}, #{connection.quote(op_spec.operation)}, #{op_spec.before}, #{op_spec.after}, CURRENT_TIMESTAMP)
  SQL
end

.json_object(connection, json_fn, row_alias, key_columns) ⇒ Object

A JSON object of the key columns read from the row_alias pseudo-record (NEW or OLD), e.g. json_object('category', NEW."category"). An empty key_columns yields an empty object, which widens maintenance to a full recompute (correct for an un-grouped view).

Parameters:

  • json_fn (String)

    the dialect's JSON-object constructor

  • row_alias (String)

    "NEW" or "OLD"



215
216
217
218
219
220
# File 'lib/activerecord/materialized/write_outbox.rb', line 215

def json_object(connection, json_fn, row_alias, key_columns)
  pairs = key_columns.flat_map do |col|
    [connection.quote(col), "#{row_alias}.#{connection.quote_column_name(col)}"]
  end
  "#{json_fn}(#{pairs.join(', ')})"
end

.ops(before, after) ⇒ Object

The three per-operation descriptors for a table, given its +before+/+after+ key-image expressions: a create captures only after, a destroy only before, an update both.



224
225
226
227
228
229
230
# File 'lib/activerecord/materialized/write_outbox.rb', line 224

def ops(before, after)
  [
    Op.new(suffix: "ins", event: "INSERT", operation: "create", before: "NULL", after: after),
    Op.new(suffix: "upd", event: "UPDATE", operation: "update", before: before, after: after),
    Op.new(suffix: "del", event: "DELETE", operation: "destroy", before: before, after: "NULL")
  ]
end

.trigger_base(table) ⇒ Object

Base identifier for a table's triggers/function. "arm_wob" = activerecord-materialized write-outbox; the +_ins+/+_upd+/+_del+/+_fn+ suffixes stay within adapter identifier limits.



205
206
207
# File 'lib/activerecord/materialized/write_outbox.rb', line 205

def trigger_base(table)
  "#{table}_arm_wob"
end