Sha256: c74056f245b3eee348ed6712b14f0b1fe5e0f09b9d943176b7e8409a83df04b2

Contents?: true

Size: 1.3 KB

Versions: 16

Compression:

Stored size: 1.3 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::SelfServeSubscription.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

16 entries across 16 versions & 1 rubygems

Version Path
nulogy_message_bus_producer-5.0.8 spec/support/sql_helpers.rb
nulogy_message_bus_producer-5.0.7 spec/support/sql_helpers.rb
nulogy_message_bus_producer-5.0.6 spec/support/sql_helpers.rb
nulogy_message_bus_producer-5.0.5 spec/support/sql_helpers.rb
nulogy_message_bus_producer-5.0.4 spec/support/sql_helpers.rb
nulogy_message_bus_producer-5.0.3 spec/support/sql_helpers.rb
nulogy_message_bus_producer-5.0.2 spec/support/sql_helpers.rb
nulogy_message_bus_producer-5.0.1 spec/support/sql_helpers.rb
nulogy_message_bus_producer-5.0.1.alpha spec/support/sql_helpers.rb
nulogy_message_bus_producer-5.0.0 spec/support/sql_helpers.rb
nulogy_message_bus_producer-4.0.0 spec/support/sql_helpers.rb
nulogy_message_bus_producer-3.7.0 spec/support/sql_helpers.rb
nulogy_message_bus_producer-3.6.0 spec/support/sql_helpers.rb
nulogy_message_bus_producer-3.5.0 spec/support/sql_helpers.rb
nulogy_message_bus_producer-4.0.0.alpha spec/support/sql_helpers.rb
nulogy_message_bus_producer-3.4.1 spec/support/sql_helpers.rb