Class: IdempotencyCheck::SqlTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/idempotency_check/sql_tracker.rb

Overview

Collects which tables received a write (INSERT/UPDATE/DELETE) inside a block, by subscribing to ActiveSupport::Notifications "sql.active_record".

This only narrows down which tables are worth snapshotting — it never decides idempotency on its own. That matters: if the SQL parsing below reports a table that wasn't really written to, the cost is one extra table snapshotted, not a wrong answer.

Constant Summary collapse

IGNORED_TABLES =
%w[schema_migrations ar_internal_metadata].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.track(&block) ⇒ Object



15
16
17
# File 'lib/idempotency_check/sql_tracker.rb', line 15

def track(&block)
  new.track(&block)
end

Instance Method Details

#trackObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/idempotency_check/sql_tracker.rb', line 20

def track
  tables = []

  subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    table = written_table(event.payload[:sql].to_s)
    tables << table if table
  end

  yield

  tables.uniq
ensure
  ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end