Skip to content

Commit

Permalink
feat: move "latest id" upsert logic into own class
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Aug 5, 2018
1 parent 19a3355 commit 360d236
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'pact_broker/pacts/all_pact_publications'
require 'pact_broker/repositories/helpers'

module PactBroker
module Pacts
class LatestPactPublicationIdForConsumerVersion < Sequel::Model(:latest_pact_publication_ids_for_consumer_versions)

dataset_module do
include PactBroker::Repositories::Helpers
end

def upsert
self.class.upsert(to_hash, [:provider_id, :consumer_version_id])
end
end
end
end

# Table: latest_pact_publications_by_consumer_versions
# Columns:
# id | integer |
# consumer_id | integer |
# consumer_name | text |
# consumer_version_id | integer |
# consumer_version_number | text |
# consumer_version_order | integer |
# provider_id | integer |
# provider_name | text |
# revision_number | integer |
# pact_version_id | integer |
# pact_version_sha | text |
# created_at | timestamp without time zone |
11 changes: 3 additions & 8 deletions lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'pact_broker/domain'
require 'pact_broker/pacts/parse'
require 'pact_broker/matrix/head_row'
require 'pact_broker/pacts/latest_pact_publication_id_by_consumer_version'

module PactBroker
module Pacts
Expand Down Expand Up @@ -49,21 +50,15 @@ def update id, params
end

def update_latest_pact_publication_ids(pact_publication)
key = {
params = {
consumer_version_id: pact_publication.consumer_version_id,
provider_id: pact_publication.provider_id,
}

other = {
pact_publication_id: pact_publication.id,
consumer_id: pact_publication.consumer_id,
pact_version_id: pact_publication.pact_version_id
}

row = key.merge(other)

table = AllPactPublications.db[:latest_pact_publication_ids_for_consumer_versions]
PactBroker::Repositories::Helpers.upsert(table, key, other)
LatestPactPublicationIdForConsumerVersion.new(params).upsert
end

def delete params
Expand Down
15 changes: 7 additions & 8 deletions lib/pact_broker/repositories/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,18 @@ def select_for_subquery column
end
end

# TODO refactor to use proper dataset module
def upsert table, key, other
row = key.merge(other)
def upsert row, key_names
if postgres?
table.insert_conflict(update: other, target: key.keys).insert(row)
insert_conflict(update: row, target: key_names).insert(row)
elsif mysql?
table.on_duplicate_key_update.insert(row)
on_duplicate_key_update.insert(row)
else
# Sqlite
if table.where(key).count == 0
table.insert(row)
key = row.reject{ |k, v| !key_names.include?(k) }
if where(key).count == 0
insert(row)
else
table.where(key).update(row)
where(key).update(row)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'pact_broker/domain/verification'

module PactBroker
module Verifications
class LatestVerificationIdForPactVersionAndProviderVersion < Sequel::Model(:latest_verification_id_for_pact_version_and_provider_version)
dataset_module do
include PactBroker::Repositories::Helpers
end

def upsert
self.class.upsert(to_hash, [:pact_version_id, :provider_version_id])
end
end
end
end

# Table: latest_verification_id_for_pact_version_and_provider_version
# Columns:
# consumer_id | integer | NOT NULL
# pact_version_id | integer | NOT NULL
# provider_id | integer | NOT NULL
# provider_version_id | integer | NOT NULL
# verification_id | integer | NOT NULL
# Indexes:
# latest_v_id_for_pv_and_pv_pv_id_pv_id_unq | UNIQUE btree (pact_version_id, provider_version_id)
# latest_v_id_for_pv_and_pv_v_id_unq | UNIQUE btree (verification_id)
# latest_v_id_for_pv_and_pv_pv_id_v_id | btree (pact_version_id, verification_id)
# Foreign key constraints:
# latest_v_id_for_pv_and_pv_consumer_id_fk | (consumer_id) REFERENCES pacticipants(id) ON DELETE CASCADE
# latest_v_id_for_pv_and_pv_pact_version_id_fk | (pact_version_id) REFERENCES pact_versions(id) ON DELETE CASCADE
# latest_v_id_for_pv_and_pv_provider_id_fk | (provider_id) REFERENCES pacticipants(id) ON DELETE CASCADE
# latest_v_id_for_pv_and_pv_provider_version_id_fk | (provider_version_id) REFERENCES versions(id) ON DELETE CASCADE
# latest_v_id_for_pv_and_pv_verification_id_fk | (verification_id) REFERENCES verifications(id) ON DELETE CASCADE
13 changes: 5 additions & 8 deletions lib/pact_broker/verifications/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'pact_broker/verifications/latest_verifications_by_consumer_version'
require 'pact_broker/verifications/all_verifications'
require 'pact_broker/verifications/sequence'
require 'pact_broker/verifications/latest_verification_id_for_pact_version_and_provider_version'

module PactBroker
module Verifications
Expand Down Expand Up @@ -33,18 +34,14 @@ def create verification, provider_version_number, pact
end

def update_latest_verification_id verification
key = {
pact_version_id: verification.pact_version_id, provider_version_id: verification.provider_version_id
}

other = {
params = {
pact_version_id: verification.pact_version_id,
provider_version_id: verification.provider_version_id,
provider_id: verification.provider_version.pacticipant_id,
verification_id: verification.id,
consumer_id: verification.consumer_id
}

table = PactBroker::Domain::Verification.db[:latest_verification_id_for_pact_version_and_provider_version]
PactBroker::Repositories::Helpers.upsert(table, key, other)
LatestVerificationIdForPactVersionAndProviderVersion.new(params).upsert
end

def find consumer_name, provider_name, pact_version_sha, verification_number
Expand Down

0 comments on commit 360d236

Please sign in to comment.