-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: duplicate key value violates unique constraint uq_ver_ppt_ord
- Loading branch information
Showing
11 changed files
with
180 additions
and
9 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,8 @@ | ||
require 'pact_broker/db/data_migrations/set_latest_version_sequence_value' | ||
Sequel.migration do | ||
change do | ||
create_table(:version_sequence_number) do | ||
Integer :value, null: false | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'pact_broker/db/data_migrations/set_latest_version_sequence_value' | ||
Sequel.migration do | ||
up do | ||
PactBroker::DB::DataMigrations::SetLatestVersionSequenceValue.call(self) | ||
end | ||
|
||
down do | ||
end | ||
end |
29 changes: 29 additions & 0 deletions
29
lib/pact_broker/db/data_migrations/set_latest_version_sequence_value.rb
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,29 @@ | ||
module PactBroker | ||
module DB | ||
module DataMigrations | ||
class SetLatestVersionSequenceValue | ||
def self.call connection | ||
if columns_exist?(connection) | ||
max_order = connection[:versions].max(:order) || 0 | ||
sequence_row = connection[:version_sequence_number].first | ||
if sequence_row.nil? || sequence_row[:value] <= max_order | ||
new_value = max_order + 100 | ||
connection[:version_sequence_number].insert(value: new_value) | ||
# Make sure there is only ever one row in case there is a race condition | ||
connection[:version_sequence_number].exclude(value: new_value).delete | ||
end | ||
end | ||
end | ||
|
||
def self.columns_exist?(connection) | ||
column_exists?(connection, :versions, :order) && | ||
column_exists?(connection, :version_sequence_number, :value) | ||
end | ||
|
||
def self.column_exists?(connection, table, column) | ||
connection.table_exists?(table) && connection.schema(table).find{|col| col.first == column } | ||
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,38 @@ | ||
require 'sequel' | ||
|
||
module PactBroker | ||
module Versions | ||
class Sequence < Sequel::Model(:version_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, after we've truncated all the | ||
# tables after a test. | ||
# 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_version_order = PactBroker::Domain::Version.max(:order) | ||
value = max_version_order ? max_version_order + 100 : 1 | ||
insert(value: value) | ||
value | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
# Table: version_sequence_number | ||
# Columns: | ||
# value | integer | NOT NULL |
68 changes: 68 additions & 0 deletions
68
spec/lib/pact_broker/db/data_migrations/set_latest_version_sequence_value_spec.rb
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,68 @@ | ||
require 'pact_broker/db/data_migrations/set_latest_version_sequence_value' | ||
|
||
module PactBroker | ||
module DB | ||
module DataMigrations | ||
describe SetLatestVersionSequenceValue, data_migration: true do | ||
include MigrationHelpers | ||
|
||
describe ".call" do | ||
before (:all) do | ||
PactBroker::Database.migrate(20190509) | ||
end | ||
|
||
let(:now) { DateTime.new(2018, 2, 2) } | ||
|
||
subject { SetLatestVersionSequenceValue.call(database) } | ||
|
||
context "when there is no sequence value set" do | ||
context "when there are no versions" do | ||
it "initializes the sequence value - this is required at start up each time in case someone has changed the ordering configuration (date vs semantic)" do | ||
subject | ||
expect(database[:version_sequence_number].first[:value]).to eq 100 | ||
end | ||
end | ||
|
||
context "when there are pre-existing versions" do | ||
let!(:consumer) { create(:pacticipants, {name: 'Consumer', created_at: now, updated_at: now}) } | ||
let!(:consumer_version) { create(:versions, {number: '1.2.3', order: 1, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) } | ||
let!(:consumer_version) { create(:versions, {number: '1.2.5', order: 3, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) } | ||
|
||
it "initializes the sequence value to the max version order with a margin of error" do | ||
subject | ||
expect(database[:version_sequence_number].first[:value]).to eq 103 | ||
end | ||
end | ||
end | ||
|
||
context "when a value already exists and it is already higher than the max_order" do | ||
before do | ||
database[:version_sequence_number].insert(value: 5) | ||
end | ||
|
||
it "does not update the value" do | ||
subject | ||
expect(database[:version_sequence_number].first[:value]).to eq 5 | ||
expect(database[:version_sequence_number].count).to eq 1 | ||
end | ||
end | ||
|
||
context "when a value already exists and it not higher than the max_order" do | ||
before do | ||
database[:version_sequence_number].insert(value: 3) | ||
end | ||
|
||
let!(:consumer) { create(:pacticipants, {name: 'Consumer', created_at: now, updated_at: now}) } | ||
let!(:consumer_version) { create(:versions, {number: '1.2.3', order: 1, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) } | ||
let!(:consumer_version) { create(:versions, {number: '1.2.5', order: 3, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) } | ||
|
||
it "updates the value" do | ||
subject | ||
expect(database[:version_sequence_number].first[:value]).to eq 103 | ||
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