Module: ActiveRecord::Materialized::WriteOutbox::Triggers::SQLite

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

Overview

SQLite: one trigger per operation; json_object; a BEGIN ... END body.

Class Method Summary collapse

Class Method Details

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



247
248
249
250
251
252
# File 'lib/activerecord/materialized/write_outbox.rb', line 247

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



261
262
263
264
265
266
267
# File 'lib/activerecord/materialized/write_outbox.rb', line 261

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)}
    BEGIN #{SharedSql.insert_values(connection, table, op_spec)}; END
  SQL
end

.uninstall!(connection, table) ⇒ Object



254
255
256
257
258
259
# File 'lib/activerecord/materialized/write_outbox.rb', line 254

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