Class: PactBroker::Versions::Repository

Inherits:
Object
  • Object
show all
Includes:
Logging, Repositories::Helpers
Defined in:
lib/pact_broker/versions/repository.rb

Instance Method Summary collapse

Methods included from Repositories::Helpers

#case_sensitivity_options, #mysql?, #name_like, #order_ignore_case, #postgres?, #select_all_qualified, #select_for_subquery, #upsert

Methods included from Logging

included, #log_error

Instance Method Details

#create(args) ⇒ Object

There may be a race condition if two simultaneous requests come in to create the same version



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pact_broker/versions/repository.rb', line 57

def create args
  logger.info "Upserting version #{args[:number]} for pacticipant_id=#{args[:pacticipant_id]}"
  version_params = {
    number: args[:number],
    pacticipant_id: args[:pacticipant_id],
    created_at: Sequel.datetime_class.now,
    updated_at: Sequel.datetime_class.now
  }
  id = PactBroker::Domain::Version.dataset.insert_ignore.insert(version_params)
  version = PactBroker::Domain::Version.find(number: args[:number], pacticipant_id: args[:pacticipant_id])
  PactBroker::Domain::OrderVersions.(version)
  version.refresh # reload with the populated order
end

#delete_by_id(version_ids) ⇒ Object



79
80
81
# File 'lib/pact_broker/versions/repository.rb', line 79

def delete_by_id version_ids
  Domain::Version.where(id: version_ids).delete
end

#delete_orphan_versions(consumer, provider) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pact_broker/versions/repository.rb', line 83

def delete_orphan_versions consumer, provider
  version_ids_with_pact_publications = PactBroker::Pacts::PactPublication.where(consumer_id: [consumer.id, provider.id]).select(:consumer_version_id).collect{|r| r[:consumer_version_id]}
  version_ids_with_verifications = PactBroker::Domain::Verification.where(provider_id: [provider.id, consumer.id]).select(:provider_version_id).collect{|r| r[:provider_version_id]}
  # Hope we don't hit max parameter constraints here...
  version_ids_to_keep = (version_ids_with_pact_publications + version_ids_with_verifications).uniq

  PactBroker::Domain::Version
    .where(pacticipant_id: [consumer.id, provider.id])
    .exclude(id: (version_ids_with_pact_publications + version_ids_with_verifications).uniq)
    .delete
end

#find_by_pacticipant_id_and_number(pacticipant_id, number) ⇒ Object



13
14
15
# File 'lib/pact_broker/versions/repository.rb', line 13

def find_by_pacticipant_id_and_number pacticipant_id, number
  PactBroker::Domain::Version.where(number: number, pacticipant_id: pacticipant_id).single_record
end

#find_by_pacticipant_id_and_number_or_create(pacticipant_id, number) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/pact_broker/versions/repository.rb', line 71

def find_by_pacticipant_id_and_number_or_create pacticipant_id, number
  if version = find_by_pacticipant_id_and_number(pacticipant_id, number)
    version
  else
    create(pacticipant_id: pacticipant_id, number: number)
  end
end

#find_by_pacticipant_name_and_latest_tag(pacticipant_name, tag) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/pact_broker/versions/repository.rb', line 17

def find_by_pacticipant_name_and_latest_tag pacticipant_name, tag
  PactBroker::Domain::Version
    .select_all_qualified
    .join(:pacticipants, {id: :pacticipant_id}, {implicit_qualifier: :versions})
    .join(:tags, {version_id: :id}, {implicit_qualifier: :versions})
    .where(name_like(Sequel[:tags][:name], tag))
    .where(name_like(Sequel[:pacticipants][:name], pacticipant_name))
    .reverse_order(:order)
    .first
end

#find_by_pacticipant_name_and_number(pacticipant_name, number) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/pact_broker/versions/repository.rb', line 47

def find_by_pacticipant_name_and_number pacticipant_name, number
  PactBroker::Domain::Version
    .select(Sequel[:versions][:id], Sequel[:versions][:number], Sequel[:versions][:pacticipant_id], Sequel[:versions][:order], Sequel[:versions][:created_at], Sequel[:versions][:updated_at])
    .join(:pacticipants, {id: :pacticipant_id})
    .where(name_like(:number, number))
    .where(name_like(:name, pacticipant_name))
    .single_record
end

#find_by_pacticipant_name_and_tag(pacticipant_name, tag) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/pact_broker/versions/repository.rb', line 28

def find_by_pacticipant_name_and_tag pacticipant_name, tag
  PactBroker::Domain::Version
    .select_all_qualified
    .join(:pacticipants, {id: :pacticipant_id}, {implicit_qualifier: :versions})
    .join(:tags, {version_id: :id}, {implicit_qualifier: :versions})
    .where(name_like(Sequel[:tags][:name], tag))
    .where(name_like(Sequel[:pacticipants][:name], pacticipant_name))
    .all
end

#find_latest_by_pacticpant_name(pacticipant_name) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/pact_broker/versions/repository.rb', line 38

def find_latest_by_pacticpant_name pacticipant_name
  PactBroker::Domain::Version
    .select_all_qualified
    .join(:pacticipants, {id: :pacticipant_id}, {implicit_qualifier: :versions})
    .where(name_like(Sequel[:pacticipants][:name], pacticipant_name))
    .reverse_order(:order)
    .first
end