forked from pact-foundation/pact_broker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(verifications): create and link provider version resource when v…
…erification is published
- Loading branch information
Showing
17 changed files
with
162 additions
and
22 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,12 @@ | ||
Sequel.migration do | ||
change do | ||
alter_table(:verifications) do | ||
add_foreign_key(:provider_version_id, :versions, foreign_key_constraint_name: 'fk_verifications_versions') | ||
end | ||
end | ||
|
||
# TODO | ||
# alter_table(:verifications) do | ||
# set_column_not_null(:provider_version_id) | ||
# end | ||
end |
25 changes: 25 additions & 0 deletions
25
db/migrations/44_populate_verifications_provider_version_id.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,25 @@ | ||
Sequel.migration do | ||
up do | ||
from(:verifications) | ||
.select(Sequel[:verifications][:id], :provider_version, :provider_id, Sequel[:verifications][:created_at]) | ||
.join(:pact_versions, {id: :pact_version_id}) | ||
.each do | line | | ||
version = from(:versions) | ||
.where(number: line[:provider_version], pacticipant_id: line[:provider_id]).single_record | ||
version_id = if version | ||
version[:id] | ||
else | ||
from(:versions).insert( | ||
number: line[:provider_version], | ||
pacticipant_id: line[:provider_id], | ||
created_at: line[:created_at], | ||
updated_at: line[:created_at] | ||
) | ||
end | ||
from(:verifications).where(id: line[:id]).update(provider_version_id: version_id) | ||
end | ||
end | ||
|
||
down do | ||
end | ||
end |
7 changes: 7 additions & 0 deletions
7
db/migrations/45_set_verification_provider_number_nullable.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,7 @@ | ||
Sequel.migration do | ||
change do | ||
alter_table(:verifications) do | ||
set_column_allow_null(:provider_version) | ||
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,18 @@ | ||
Sequel.migration do | ||
up do | ||
create_or_replace_view(:latest_verification_numbers, | ||
"SELECT pact_version_id, MAX(number) latest_number | ||
FROM verifications | ||
GROUP BY pact_version_id") | ||
|
||
# The most recent verification for each pact version | ||
# provider_version is DEPRECATED, use provider_version_number | ||
create_or_replace_view(:latest_verifications, | ||
"SELECT v.id, v.number, v.success, s.number as provider_version, v.build_url, v.pact_version_id, v.execution_date, v.created_at, v.provider_version_id, s.number as provider_version_number | ||
FROM verifications v | ||
INNER JOIN latest_verification_numbers lv | ||
ON v.pact_version_id = lv.pact_version_id AND v.number = lv.latest_number | ||
INNER JOIN versions s on v.provider_version_id = s.id" | ||
) | ||
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
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
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
60 changes: 60 additions & 0 deletions
60
spec/migrations/44_add_provider_version_to_verification_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,60 @@ | ||
describe 'add provider version relationship to verification (migrate 42-44)', migration: true do | ||
before do | ||
PactBroker::Database.migrate(42) | ||
end | ||
|
||
let(:now) { DateTime.new(2018, 2, 2) } | ||
let!(:consumer) { create(:pacticipants, {name: 'Consumer', created_at: now, updated_at: now}) } | ||
let!(:provider) { create(:pacticipants, {name: 'Provider', created_at: now, updated_at: now}) } | ||
let!(:provider) { create(:pacticipants, {name: 'Provider', 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!(:pact_version) { create(:pact_versions, {content: {some: 'json'}.to_json, sha: '1234', consumer_id: consumer[:id], provider_id: provider[:id], created_at: now}) } | ||
let!(:pact_publication) do | ||
create(:pact_publications, { | ||
consumer_version_id: consumer_version[:id], | ||
provider_id: provider[:id], | ||
revision_number: 1, | ||
pact_version_id: pact_version[:id], | ||
created_at: (now - 1) | ||
}) | ||
end | ||
let!(:verification) do | ||
create(:verifications, { | ||
number: 1, | ||
success: true, | ||
provider_version: '1.2.3', | ||
pact_version_id: pact_version[:id], | ||
execution_date: now, | ||
created_at: now | ||
}) | ||
end | ||
|
||
subject { PactBroker::Database.migrate(46) } | ||
|
||
it "creates a version object" do | ||
expect { subject }.to change { PactBroker::Domain::Version.count }.by(1) | ||
end | ||
|
||
it "sets the foreign key to the new version" do | ||
subject | ||
expect(PactBroker::Domain::Verification.first.provider_version.number).to eq '1.2.3' | ||
end | ||
|
||
it "sets the created_at date of the new version to the created_at of the verification" do | ||
subject | ||
expect(PactBroker::Domain::Verification.first.provider_version.created_at).to eq now | ||
end | ||
|
||
context "when the version already exists" do | ||
let!(:provider_version) { create(:versions, {number: '1.2.3', order: 1, pacticipant_id: provider[:id], created_at: now, updated_at: now}) } | ||
|
||
it "does not create a version object" do | ||
expect { subject }.to_not change { PactBroker::Domain::Version.count } | ||
end | ||
|
||
it "sets the foreign key to the existing version" do | ||
subject | ||
expect(PactBroker::Domain::Verification.first.provider_version.number).to eq '1.2.3' | ||
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