class TestProf::AnyFixture::Dump::Subscriber

Attributes

adapter[R]
path[R]
reset_pk[R]
tmp_path[R]

Public Class Methods

new(path, adapter) click to toggle source
# File lib/test_prof/any_fixture/dump.rb, line 43
def initialize(path, adapter)
  @path = path
  @adapter = adapter
  @tmp_path = path + ".tmp"
  @reset_pk = Set.new
end

Public Instance Methods

commit() click to toggle source
# File lib/test_prof/any_fixture/dump.rb, line 71
def commit
  return unless defined?(:@file)

  file.close

  FileUtils.mv(tmp_path, path)
end
finish(_event, _id, payload) click to toggle source
# File lib/test_prof/any_fixture/dump.rb, line 60
def finish(_event, _id, payload)
  sql = payload.fetch(:sql)
  return unless trackable_sql?(sql)

  sql = payload[:binds].any? ? adapter.compile_sql(sql, quoted(payload[:binds])) : +sql

  sql.tr!("\n", " ")

  file.write(sql + ";\n")
end
start(_event, _id, payload) click to toggle source
# File lib/test_prof/any_fixture/dump.rb, line 50
def start(_event, _id, payload)
  sql = payload.fetch(:sql)
  return if sql.match?(ANY_FIXTURE_IGNORE_RXP)

  matches = sql.match(MODIFY_RXP)
  return unless matches

  reset_pk!(matches[2]) if /insert/i.match?(matches[1])
end

Private Instance Methods

file() click to toggle source
# File lib/test_prof/any_fixture/dump.rb, line 83
def file
  @file ||= File.open(tmp_path, "w")
end
quoted(val) click to toggle source
# File lib/test_prof/any_fixture/dump.rb, line 102
def quoted(val)
  if val.is_a?(Array)
    val.map { |v| quoted(v) }
  elsif val.is_a?(ActiveModel::Attribute)
    quoted(val.value_for_database)
  else
    ActiveRecord::Base.connection.quote(val)
  end
end
reset_pk!(table_name) click to toggle source
# File lib/test_prof/any_fixture/dump.rb, line 87
def reset_pk!(table_name)
  return if /sqlite_sequence/.match?(table_name)

  return if reset_pk.include?(table_name)

  adapter.reset_sequence!(table_name, AnyFixture.config.dump_sequence_random_start)
  reset_pk << table_name
end
trackable_sql?(sql) click to toggle source
# File lib/test_prof/any_fixture/dump.rb, line 96
def trackable_sql?(sql)
  return false if sql.match?(ANY_FIXTURE_IGNORE_RXP)

  sql.match?(MODIFY_RXP) || sql.match?(ANY_FIXTURE_RXP) || sql.match?(AnyFixture.config.dump_matching_queries)
end