Class: ActiverecordMaterialized::OutboxGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/activerecord_materialized/outbox_generator.rb

Overview

Generates a migration that installs write-outbox triggers on a dependency table, so writes that bypass ActiveRecord (raw SQL, another service, a backfill) are still captured and can be relayed into view maintenance via ActiveRecord::Materialized.drain_write_outbox!:

bin/rails generate activerecord_materialized:outbox line_items category region

The trailing arguments are the GROUP BY key columns to capture (empty for an un-grouped view). The migration emits the correct trigger DDL for the connection's adapter at migrate time. See docs/out-of-band-writes.md.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_migration_number(dirname) ⇒ Object



25
26
27
# File 'lib/generators/activerecord_materialized/outbox_generator.rb', line 25

def self.next_migration_number(dirname)
  ::ActiveRecord::Migration.next_migration_number(current_migration_number(dirname) + 1)
end

Instance Method Details

#create_migration_fileObject



29
30
31
32
33
34
# File 'lib/generators/activerecord_materialized/outbox_generator.rb', line 29

def create_migration_file
  # +file_name+, not NamedBase#table_name: NAME is a raw dependency table, and table_name would
  # inflect it (pluralize a singular/irregular table — person -> people), targeting the wrong table.
  migration_template "write_outbox_migration.rb.erb",
                     File.join("db", "migrate", "install_write_outbox_triggers_on_#{file_name}.rb")
end