From 9a148b80157552b61957e09805e689c27765cd5d Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sat, 4 Aug 2018 16:51:17 +1000 Subject: [PATCH] feat: optimise data migrations that set pacticipant ids --- .../set_consumer_ids_for_pact_publications.rb | 14 ++++++-------- .../set_pacticipant_ids_for_verifications.rb | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.rb b/lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.rb index a15c493d6..e0af2f760 100644 --- a/lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.rb +++ b/lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.rb @@ -4,14 +4,12 @@ module DataMigrations class SetConsumerIdsForPactPublications def self.call connection if columns_exist?(connection) - ids = connection.from(:pact_publications) - .select(Sequel[:pact_publications][:id], Sequel[:versions][:pacticipant_id].as(:consumer_id)) - .join(:versions, {id: :consumer_version_id}) - .where(Sequel[:pact_publications][:consumer_id] => nil) - - ids.each do | id | - connection.from(:pact_publications).where(id: id[:id]).update(consumer_id: id[:consumer_id]) - end + query = "UPDATE pact_publications + SET consumer_id = (SELECT consumer_id + FROM versions + WHERE id = pact_publications.consumer_version_id) + WHERE consumer_id is null" + connection.run(query) end end diff --git a/lib/pact_broker/db/data_migrations/set_pacticipant_ids_for_verifications.rb b/lib/pact_broker/db/data_migrations/set_pacticipant_ids_for_verifications.rb index 64066e5e5..dd52ca397 100644 --- a/lib/pact_broker/db/data_migrations/set_pacticipant_ids_for_verifications.rb +++ b/lib/pact_broker/db/data_migrations/set_pacticipant_ids_for_verifications.rb @@ -4,15 +4,16 @@ 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 + query = "UPDATE verifications + SET consumer_id = (SELECT consumer_id + FROM pact_versions + WHERE id = verifications.pact_version_id), + provider_id = (SELECT provider_id + FROM pact_versions + WHERE id = verifications.pact_version_id) + WHERE consumer_id is null + OR provider_id is null" + connection.run(query) end end