Class: PactBroker::Versions::Repository

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

Instance Method Summary collapse

Methods included from Repositories

#label_repository, #matrix_repository, #pact_repository, #pacticipant_repository, #tag_repository, #verification_repository, #version_repository, #webhook_repository

Methods included from Repositories::Helpers

#mysql?, #name_like, #order_append_ignore_case, #order_ignore_case, #pacticipant_id_for_name, #postgres?, #select_all_qualified, #select_append_all_qualified, #select_for_subquery

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



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pact_broker/versions/repository.rb', line 52

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
  }

  PactBroker::Domain::Version.new(version_params).upsert
end

#create_or_overwrite(pacticipant, version_number, open_struct_version) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pact_broker/versions/repository.rb', line 64

def create_or_overwrite(pacticipant, version_number, open_struct_version)
  saved_version = PactBroker::Domain::Version.new(
    number: version_number,
    pacticipant: pacticipant,
    build_url: open_struct_version.build_url,
    branch: open_struct_version.branch
  ).upsert

  if open_struct_version.tags
    tag_repository.delete_by_version_id(saved_version.id)
    open_struct_version.tags.collect do | open_struct_tag |
      tag_repository.create(version: saved_version, name: open_struct_tag.name)
    end
    saved_version.refresh
  end

  saved_version
end

#delete_by_id(version_ids) ⇒ Object



91
92
93
# File 'lib/pact_broker/versions/repository.rb', line 91

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

#delete_orphan_versions(consumer, provider) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pact_broker/versions/repository.rb', line 95

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(Sequel[:versions][: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



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

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



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

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



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

def find_by_pacticipant_name_and_latest_tag pacticipant_name, tag
  PactBroker::Domain::Version
    .select_all_qualified
    .where_tag(tag)
    .where_pacticipant_name(pacticipant_name)
    .reverse_order(:order)
    .first
end

#find_by_pacticipant_name_and_number(pacticipant_name, number) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/pact_broker/versions/repository.rb', line 43

def find_by_pacticipant_name_and_number pacticipant_name, number
  PactBroker::Domain::Version
    .select_all_qualified
    .where_pacticipant_name(pacticipant_name)
    .where_number(number)
    .single_record
end

#find_by_pacticipant_name_and_tag(pacticipant_name, tag) ⇒ Object



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

def find_by_pacticipant_name_and_tag pacticipant_name, tag
  PactBroker::Domain::Version
    .select_all_qualified
    .where_pacticipant_name(pacticipant_name)
    .where_tag(tag)
    .all
end

#find_latest_by_pacticpant_name(pacticipant_name) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/pact_broker/versions/repository.rb', line 35

def find_latest_by_pacticpant_name pacticipant_name
  PactBroker::Domain::Version
    .select_all_qualified
    .where_pacticipant_name(pacticipant_name)
    .reverse_order(:order)
    .first
end

#find_versions_for_selector(selector) ⇒ Object



107
108
109
# File 'lib/pact_broker/versions/repository.rb', line 107

def find_versions_for_selector(selector)
  PactBroker::Domain::Version.select_all_qualified.for_selector(selector).all
end