diff --git a/lib/outboxer/messages.rb b/lib/outboxer/messages.rb index b72db357..3b59be80 100644 --- a/lib/outboxer/messages.rb +++ b/lib/outboxer/messages.rb @@ -43,5 +43,17 @@ def unpublished!(limit: 1, order: :asc) .to_a end end + + def delete_all! + ActiveRecord::Base.connection_pool.with_connection do + ActiveRecord::Base.transaction do + Models::Frame.delete_all + Models::Exception.delete_all + Models::Message.delete_all + end + end + + nil + end end end diff --git a/spec/lib/outboxer/messages/delete_all_spec.rb b/spec/lib/outboxer/messages/delete_all_spec.rb new file mode 100644 index 00000000..61979d9a --- /dev/null +++ b/spec/lib/outboxer/messages/delete_all_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +module Outboxer + RSpec.describe Messages do + describe '.delete_all!' do + let!(:message_1) { create(:outboxer_message, :failed) } + let!(:exception_1) { create(:outboxer_exception, message: message_1) } + let!(:frame_1) { create(:outboxer_frame, exception: exception_1) } + + let!(:message_2) { create(:outboxer_message, :failed) } + let!(:exception_2) { create(:outboxer_exception, message: message_2) } + let!(:frame) { create(:outboxer_frame, exception: exception_2) } + + let!(:result) { Messages.delete_all! } + + it 'deletes all messages' do + expect(Models::Message.count).to eq(0) + end + + it 'deletes all exceptions' do + expect(Models::Exception.count).to eq(0) + end + + it 'deletes all frames' do + expect(Models::Frame.count).to eq(0) + end + + it 'returns nil' do + expect(result).to be_nil + end + end + end +end