-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure publishing a verification does not cause a unique constra…
…int violation
- Loading branch information
Showing
9 changed files
with
108 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Sequel.migration do | ||
up do | ||
create_table(:verification_sequence_number) do | ||
Integer :value, null: false | ||
end | ||
|
||
start = (from(:verifications).max(:number) || 0) + 100 | ||
from(:verification_sequence_number).insert(value: start) | ||
end | ||
|
||
down do | ||
drop_table(:verification_sequence_number) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
require 'sequel' | ||
|
||
module PactBroker | ||
module Verifications | ||
class Sequence < Sequel::Model(:verification_sequence_number) | ||
|
||
dataset_module do | ||
# The easiest way to implement a cross database compatible sequence. | ||
# Sad, I know. | ||
def next_val | ||
db.transaction do | ||
for_update.first | ||
select_all.update(value: Sequel[:value]+1) | ||
row = first | ||
if row | ||
row.value | ||
else | ||
# The first row should have been created in the migration, so this code | ||
# should only ever be executed in a test context. | ||
# There would be a risk of a race condition creating two rows if this | ||
# code executed in prod, as I don't think you can lock an empty table | ||
# to prevent another record being inserted. | ||
max_verification_number = PactBroker::Domain::Verification.max(:number) | ||
value = max_verification_number ? max_verification_number + 100 : 1 | ||
insert(value: value) | ||
value | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'pact_broker/verifications/sequence' | ||
|
||
module PactBroker | ||
module Verifications | ||
describe Sequence do | ||
describe "#next_val", migration: true do | ||
|
||
before do | ||
PactBroker::Database.migrate | ||
end | ||
|
||
context "when there is a row in the verification_sequence_number table" do | ||
before do | ||
Sequence.select_all.delete | ||
Sequence.insert(value: 1) | ||
end | ||
|
||
it "increments the value and returns it" do | ||
expect(Sequence.next_val).to eq 2 | ||
end | ||
end | ||
|
||
context "when there is no row in the verification_sequence_number table and no existing verifications" do | ||
before do | ||
Sequence.select_all.delete | ||
end | ||
|
||
it "inserts and returns the value 1" do | ||
expect(Sequence.next_val).to eq 1 | ||
end | ||
end | ||
|
||
context "when there is no row in the verification_sequence_number table and there are existing verifications" do | ||
before do | ||
Sequence.select_all.delete | ||
TestDataBuilder.new.create_pact_with_hierarchy("A", "1", "B") | ||
.create_verification(provider_version: "2") | ||
end | ||
|
||
it "inserts a number that is guaranteed to be higher than any of the existing verification numbers from when we tried to do this without a sequence" do | ||
expect(Sequence.next_val).to eq 101 | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters