-
-
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.
feat: add contract_data_updated_at to integrations table to speed up …
…dashboard query (#617) PACT-1070
- Loading branch information
Showing
18 changed files
with
321 additions
and
23 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
db/migrations/20230615_add_integrations_contract_data_updated_at.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,13 @@ | ||
Sequel.migration do | ||
up do | ||
alter_table(:integrations) do | ||
add_column(:contract_data_updated_at, DateTime) | ||
end | ||
end | ||
|
||
down do | ||
alter_table(:integrations) do | ||
drop_column(:contract_data_updated_at) | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
db/migrations/20230616_set_integrations_contract_data_updated_at.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,11 @@ | ||
require "pact_broker/db/data_migrations/set_contract_data_updated_at_for_integrations" | ||
|
||
Sequel.migration do | ||
up do | ||
PactBroker::DB::DataMigrations::SetContractDataUpdatedAtForIntegrations.call(self) | ||
end | ||
|
||
down do | ||
|
||
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
47 changes: 47 additions & 0 deletions
47
lib/pact_broker/db/data_migrations/set_contract_data_updated_at_for_integrations.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,47 @@ | ||
# Populate the newly created contract_data_updated_at date in the integrations table | ||
# using the latest created_at date from the pact_publications or verifications tables. | ||
module PactBroker | ||
module DB | ||
module DataMigrations | ||
class SetContractDataUpdatedAtForIntegrations | ||
def self.call(connection) | ||
join = { | ||
Sequel[:integrations][:consumer_id] => Sequel[:target][:consumer_id], | ||
Sequel[:integrations][:provider_id] => Sequel[:target][:provider_id] | ||
} | ||
|
||
max_created_at_for_each_integration = integrations_max_created_at(connection).from_self(alias: :target).select(:created_at).where(join) | ||
|
||
connection[:integrations] | ||
.where(contract_data_updated_at: nil) | ||
.update(contract_data_updated_at: max_created_at_for_each_integration) | ||
end | ||
|
||
# @return [Sequel::Dataset] the overall max created_at from the union of the pact_publications and verifications tables, | ||
# for each integration keyed by consumer_id/provider_id | ||
def self.integrations_max_created_at(connection) | ||
pact_publication_max_created_at(connection) | ||
.union(verification_max_created_at(connection)) | ||
.select_group(:consumer_id, :provider_id) | ||
.select_append{ max(:created_at).as(:created_at) } | ||
end | ||
|
||
# @return [Sequel::Dataset] the max created_at from the pact_publications table | ||
# for each integration keyed by consumer_id/provider_id | ||
def self.pact_publication_max_created_at(connection) | ||
connection[:pact_publications] | ||
.select_group(:consumer_id, :provider_id) | ||
.select_append{ max(:created_at).as(:created_at) } | ||
end | ||
|
||
# @return [Sequel::Dataset] the max created_at from the verifications table | ||
# for each integration keyed by consumer_id/provider_id | ||
def self.verification_max_created_at(connection) | ||
connection[:verifications] | ||
.select_group(:consumer_id, :provider_id) | ||
.select_append{ max(:created_at).as(:created_at) } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require "pact_broker/events/subscriber" | ||
require "pact_broker/integrations/event_listener" | ||
|
||
PactBroker::Events.subscribe(PactBroker::Integrations::EventListener.new) |
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,23 @@ | ||
require "pact_broker/services" | ||
|
||
# Listens for events that happen in the Pact Broker that are relevant to the Integrations objects. | ||
|
||
module PactBroker | ||
module Integrations | ||
class EventListener | ||
include PactBroker::Services | ||
|
||
# @param [Hash] params the params from the broadcast event | ||
# @option params [PactBroker::Domain::Pact] :pact the newly published pact | ||
def contract_published(params) | ||
integration_service.handle_contract_data_published(params.fetch(:pact).consumer, params.fetch(:pact).provider) | ||
end | ||
|
||
# @param [Hash] params the params from the broadcast event | ||
# @option params [PactBroker::Domain::Verification] :verification the newly published verification | ||
def provider_verification_published(params) | ||
integration_service.handle_contract_data_published(params.fetch(:verification).consumer, params.fetch(:verification).provider) | ||
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
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
52 changes: 52 additions & 0 deletions
52
spec/integration/endpoints/publish_verification_results_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,52 @@ | ||
require "pact_broker/domain/verification" | ||
require "timecop" | ||
|
||
describe "Publishing a pact verification" do | ||
let(:path) { "/pacts/provider/Provider/consumer/Consumer/pact-version/#{pact.pact_version_sha}/verification-results" } | ||
let(:verification_content) { load_fixture("verification.json") } | ||
let(:parsed_response_body) { JSON.parse(subject.body) } | ||
let(:pact) { td.pact } | ||
let(:rack_env) do | ||
{ | ||
"CONTENT_TYPE" => "application/json", | ||
"HTTP_ACCEPT" => "application/hal+json", | ||
"pactbroker.database_connector" => lambda { |&block| block.call } | ||
} | ||
end | ||
|
||
subject { post(path, verification_content, rack_env) } | ||
|
||
before do | ||
Timecop.freeze(Date.today - 2) do | ||
td.create_provider("Provider") | ||
.create_consumer("Consumer") | ||
.create_consumer_version("1.0.0") | ||
.create_pact | ||
.create_consumer_version("1.2.3") | ||
.create_pact | ||
.revise_pact | ||
end | ||
end | ||
|
||
it "updates the contract_data_updated_at on the integration" do | ||
expect { subject }.to change { PactBroker::Integrations::Integration.last.contract_data_updated_at } | ||
end | ||
|
||
context "with a webhook configured", job: true do | ||
before do | ||
td.create_webhook( | ||
method: "POST", | ||
url: "http://example.org", | ||
events: [{ name: PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED }] | ||
) | ||
end | ||
let!(:request) do | ||
stub_request(:post, "http://example.org").to_return(:status => 200) | ||
end | ||
|
||
it "executes the webhook" do | ||
subject | ||
expect(request).to have_been_made | ||
end | ||
end | ||
end |
49 changes: 49 additions & 0 deletions
49
.../lib/pact_broker/db/data_migrations/set_contract_data_updated_at_for_integrations_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,49 @@ | ||
require "pact_broker/db/data_migrations/set_contract_data_updated_at_for_integrations" | ||
require "timecop" | ||
require "tzinfo" | ||
|
||
module PactBroker | ||
module DB | ||
module DataMigrations | ||
describe SetContractDataUpdatedAtForIntegrations do | ||
before do | ||
td.clear_now # use timecop instead of the TestDataBuilder @now | ||
|
||
Timecop.freeze(day_1) do | ||
td.publish_pact(consumer_name: "Foo", provider_name: "Bar", consumer_version_number: "1") | ||
end | ||
|
||
Timecop.freeze(day_2) do | ||
td.publish_pact(consumer_name: "Foo", provider_name: "Bar", consumer_version_number: "2") | ||
end | ||
|
||
Timecop.freeze(day_3) do | ||
td.create_verification(provider_version: "2") | ||
end | ||
|
||
Timecop.freeze(day_4) do | ||
td.publish_pact(consumer_name: "Cat", provider_name: "Dog", consumer_version_number: "2") | ||
end | ||
|
||
db[:integrations].update(contract_data_updated_at: nil) | ||
end | ||
|
||
let(:day_1) { td.in_utc{ DateTime.new(2023, 6, 11) } } | ||
let(:day_2) { td.in_utc{ DateTime.new(2023, 6, 12) } } | ||
let(:day_3) { td.in_utc{ DateTime.new(2023, 6, 13) } } | ||
let(:day_4) { td.in_utc{ DateTime.new(2023, 6, 14) } } | ||
|
||
let(:db) { PactBroker::Domain::Version.db } | ||
|
||
subject { SetContractDataUpdatedAtForIntegrations.call(db) } | ||
|
||
it "sets the contract_data_updated_at to the latest of the pact publication and verification publication dates for that integration" do | ||
subject | ||
integrations = db[:integrations].order(:id) | ||
expect(integrations.first[:contract_data_updated_at]).to be_date_time(day_3) | ||
expect(integrations.last[:contract_data_updated_at]).to be_date_time(day_4) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.