Skip to content

Commit

Permalink
feat: optimise data migrations that set pacticipant ids
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Aug 4, 2018
1 parent 7dc93d1 commit 9a148b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 9a148b8

Please sign in to comment.