Module: ActiveRecord::Materialized::WriteOutbox::Triggers::MySQL

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

Overview

MySQL/MariaDB/trilogy: one trigger per operation; JSON_OBJECT; a single-statement body (no BEGIN/END, so no DELIMITER handling needed).

Class Method Summary collapse

Class Method Details

.install!(connection, table, key_columns) ⇒ Object



275
276
277
278
279
280
# File 'lib/activerecord/materialized/write_outbox.rb', line 275

def install!(connection, table, key_columns)
  uninstall!(connection, table)
  before = SharedSql.json_object(connection, "JSON_OBJECT", "OLD", key_columns)
  after = SharedSql.json_object(connection, "JSON_OBJECT", "NEW", key_columns)
  SharedSql.ops(before, after).each { |op_spec| connection.execute(trigger(connection, table, op_spec)) }
end

.trigger(connection, table, op_spec) ⇒ Object



289
290
291
292
293
294
295
# File 'lib/activerecord/materialized/write_outbox.rb', line 289

def trigger(connection, table, op_spec)
  name = connection.quote_column_name("#{SharedSql.trigger_base(table)}_#{op_spec.suffix}")
  <<~SQL.squish
    CREATE TRIGGER #{name} AFTER #{op_spec.event} ON #{connection.quote_table_name(table)}
    FOR EACH ROW #{SharedSql.insert_values(connection, table, op_spec)}
  SQL
end

.uninstall!(connection, table) ⇒ Object



282
283
284
285
286
287
# File 'lib/activerecord/materialized/write_outbox.rb', line 282

def uninstall!(connection, table)
  base = SharedSql.trigger_base(table)
  %w[ins upd del].each do |suffix|
    connection.execute("DROP TRIGGER IF EXISTS #{connection.quote_column_name("#{base}_#{suffix}")}")
  end
end