module SqlHelpers def without_transaction begin ActiveRecord::Base.connection.rollback_transaction yield ensure truncate_db end 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 get_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 get_replication_slots.any? { |replication_slot| replication_slot == slot_name } end end def wait_for_replication_slot_cleanup(slot_name) wait_for do get_replication_slots.none? { |replication_slot| replication_slot == slot_name } end end end