Skip to content

Commit

Permalink
feat: add consumer id and provider id to verifications table to speed…
Browse files Browse the repository at this point in the history
… up queries
  • Loading branch information
bethesque committed Jul 31, 2018
1 parent ec59ba5 commit d569890
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 3 deletions.
17 changes: 17 additions & 0 deletions db/migrations/20180612_add_pacticipant_ids_to_verifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Sequel.migration do
change do
alter_table(:verifications) do
add_foreign_key(:consumer_id, :pacticipants)
add_foreign_key(:provider_id, :pacticipants)
add_index(:consumer_id, name: "verifications_consumer_id_index")
add_index(:provider_id, name: "verifications_provider_id_index")
add_index([:provider_id, :consumer_id], name: "verifications_provider_id_consumer_id_index")
end

# TODO
# alter_table(:verifications) do
# set_column_not_null(:consumer_id)
# set_column_not_null(:provider_id)
# end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'pact_broker/db/data_migrations/set_pacticipant_ids_for_verifications'

Sequel.migration do
up do
PactBroker::DB::DataMigrations::SetPacticipantIdsForVerifications.call(self)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Sequel.migration do
up do
# The latest verification id for each consumer version tag
create_or_replace_view(:latest_verification_ids_for_consumer_and_provider,
"select
provider_id,
consumer_id,
max(id) as latest_verification_id
from verifications v
group by provider_id, consumer_id")
end

down do
# The latest verification id for each consumer version tag
create_or_replace_view(:latest_verification_ids_for_consumer_and_provider,
"select
pv.pacticipant_id as provider_id,
lpp.consumer_id,
max(v.id) as latest_verification_id
from verifications v
join latest_pact_publications_by_consumer_versions lpp
on v.pact_version_id = lpp.pact_version_id
join versions pv
on v.provider_version_id = pv.id
group by pv.pacticipant_id, lpp.consumer_id")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module PactBroker
module DB
module DataMigrations
class SetPacticipantIdsForVerifications
def self.call connection
if columns_exist?(connection)
ids = connection.from(:verifications)
.select(Sequel[:verifications][:id], Sequel[:pact_versions][:consumer_id], Sequel[:pact_versions][:provider_id])
.join(:pact_versions, {id: :provider_version_id})
.where(Sequel[:verifications][:consumer_id] => nil)
.or(Sequel[:verifications][:provider_id] => nil)

ids.each do | id |
connection.from(:verifications).where(id: id[:id]).update(consumer_id: id[:consumer_id], provider_id: id[:provider_id])
end
end
end

def self.columns_exist?(connection)
column_exists?(connection, :verifications, :provider_id) &&
column_exists?(connection, :verifications, :consumer_id) &&
column_exists?(connection, :verifications, :provider_version_id) &&
column_exists?(connection, :pact_versions, :provider_id) &&
column_exists?(connection, :pact_versions, :consumer_id) &&
column_exists?(connection, :pact_versions, :id)
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
4 changes: 3 additions & 1 deletion lib/pact_broker/db/migrate_data.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'pact_broker/db/data_migrations/set_pacticipant_ids_for_verifications'

module PactBroker
module DB
class MigrateData
def self.call database_connection, options = {}

DataMigrations::SetPacticipantIdsForVerifications.call(database_connection)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/pact_broker/domain/pacticipant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def validate
# pact_versions | pact_versions_consumer_id_fkey | (consumer_id) REFERENCES pacticipants(id)
# pact_versions | pact_versions_provider_id_fkey | (provider_id) REFERENCES pacticipants(id)
# versions | versions_pacticipant_id_fkey | (pacticipant_id) REFERENCES pacticipants(id)
# verifications | verifications_consumer_id_fkey | (consumer_id) REFERENCES pacticipants(id)
# verifications | verifications_provider_id_fkey | (provider_id) REFERENCES pacticipants(id)
# labels | labels_pacticipant_id_fkey | (pacticipant_id) REFERENCES pacticipants(id)
# triggered_webhooks | triggered_webhooks_consumer_id_fkey | (consumer_id) REFERENCES pacticipants(id)
# triggered_webhooks | triggered_webhooks_provider_id_fkey | (provider_id) REFERENCES pacticipants(id)
Expand Down
11 changes: 9 additions & 2 deletions lib/pact_broker/domain/verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,18 @@ def latest_pact_publication
# created_at | timestamp without time zone | NOT NULL
# provider_version_id | integer |
# test_results | text |
# consumer_id | integer |
# provider_id | integer |
# Indexes:
# verifications_pkey | PRIMARY KEY btree (id)
# verifications_pact_version_id_number_index | UNIQUE btree (pact_version_id, number)
# verifications_pkey | PRIMARY KEY btree (id)
# verifications_pact_version_id_number_index | UNIQUE btree (pact_version_id, number)
# verifications_consumer_id_index | btree (consumer_id)
# verifications_provider_id_consumer_id_index | btree (provider_id, consumer_id)
# verifications_provider_id_index | btree (provider_id)
# Foreign key constraints:
# fk_verifications_versions | (provider_version_id) REFERENCES versions(id)
# verifications_consumer_id_fkey | (consumer_id) REFERENCES pacticipants(id)
# verifications_pact_version_id_fkey | (pact_version_id) REFERENCES pact_versions(id)
# verifications_provider_id_fkey | (provider_id) REFERENCES pacticipants(id)
# Referenced By:
# triggered_webhooks | triggered_webhooks_verification_id_fkey | (verification_id) REFERENCES verifications(id)
3 changes: 3 additions & 0 deletions lib/pact_broker/verifications/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ def next_number

def create verification, provider_version_number, pact
provider = pacticipant_repository.find_by_name(pact.provider_name)
consumer = pacticipant_repository.find_by_name(pact.consumer_name)
version = version_repository.find_by_pacticipant_id_and_number_or_create(provider.id, provider_version_number)
verification.pact_version_id = pact_version_id_for(pact)
verification.provider_version = version
verification.provider_id = provider.id
verification.consumer_id = consumer.id
verification.save
end

Expand Down

0 comments on commit d569890

Please sign in to comment.