Sha256: ed2596478fe69acede8f49cd8c2b3a9d986a4b70bdacea76d09630a06476696b

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module SqlHelpers
  def without_transaction
    ActiveRecord::Base.connection.rollback_transaction
    yield
  ensure
    truncate_db
  end

  def truncate_db
    # Hit any tables that were used via active record
    ActiveRecord::Base.connection.tables.each do |table|
      ActiveRecord::Base.connection.execute("TRUNCATE #{table}")
    end

    # Just incase these models weren't loaded, do them explicitly
    ActiveRecord::Base.connection.execute("TRUNCATE #{NulogyMessageBusProducer::SubscriptionEvent.table_name}")
    ActiveRecord::Base.connection.execute("TRUNCATE #{NulogyMessageBusProducer::Subscription.table_name}")
  end

  def replication_slots
    results = ActiveRecord::Base.connection.exec_query(<<~SQL)
      SELECT slot_name FROM pg_replication_slots
    SQL

    results.to_a.map { |result| result["slot_name"] }
  end

  def drop_replication_slot(slot_name)
    ActiveRecord::Base.connection.execute(<<~SQL)
      SELECT pg_drop_replication_slot('#{slot_name}')
    SQL
  end

  def wait_for_replication_slot(slot_name)
    wait_for do
      replication_slots.any? { |replication_slot| replication_slot == slot_name }
    end
  end

  def wait_for_replication_slot_cleanup(slot_name)
    wait_for do
      replication_slots.none? { |replication_slot| replication_slot == slot_name }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nulogy_message_bus_producer-3.4.0 spec/support/sql_helpers.rb
nulogy_message_bus_producer-3.3.0 spec/support/sql_helpers.rb
nulogy_message_bus_producer-3.2.1 spec/support/sql_helpers.rb