Skip to content

Commit

Permalink
bug - Order fees by created_at, id to ensure consistent ordering (#2560)
Browse files Browse the repository at this point in the history
Postgres does not guarantee that rows with the same created_at will
always be returned in the same order. In order to guarantee a consistent
ordering, we also have to order by id.
  • Loading branch information
nudded authored Sep 10, 2024
1 parent 9a0a1ef commit 30b31d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/queries/fees_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class FeesQuery < BaseQuery
def call
fees = paginate(Fee.from_organization(organization))
fees = fees.order(created_at: :asc)
fees = fees.order(created_at: :asc, id: :asc)

fees = with_external_subscription(fees) if filters.external_subscription_id
fees = with_external_customer(fees) if filters.external_customer_id
Expand Down
19 changes: 19 additions & 0 deletions spec/queries/fees_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@
end
end

context 'with multiple fees' do
let(:fee2) { create(:fee, subscription:, invoice: nil, created_at: fee.created_at) }

before do
fee2
fee2.update! id: '00000000-0000-0000-0000-000000000000'
end

it 'returns a consistent list when 2 fees have the same created_at' do
result = fees_query.call

aggregate_failures do
expect(result).to be_success
expect(result.fees.count).to eq(2)
expect(result.fees).to eq([fee2, fee])
end
end
end

context 'with pagination' do
let(:pagination) { {page: 2, limit: 10} }

Expand Down

0 comments on commit 30b31d4

Please sign in to comment.