-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add consumer id and provider id to verifications table to speed…
… up queries
- Loading branch information
Showing
8 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
db/migrations/20180612_add_pacticipant_ids_to_verifications.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,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 |
7 changes: 7 additions & 0 deletions
7
db/migrations/20180613_migrate_pacticipant_ids_for_verifications.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 @@ | ||
require 'pact_broker/db/data_migrations/set_pacticipant_ids_for_verifications' | ||
|
||
Sequel.migration do | ||
up do | ||
PactBroker::DB::DataMigrations::SetPacticipantIdsForVerifications.call(self) | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
db/migrations/20180614_update_latest_verification_ids_for_consumer_and_provider.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,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 |
34 changes: 34 additions & 0 deletions
34
lib/pact_broker/db/data_migrations/set_pacticipant_ids_for_verifications.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,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 |
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