Class: PactBroker::DB::DataMigrations::SetPacticipantMainBranch

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Includes:
Logging
Defined in:
lib/pact_broker/db/data_migrations/set_pacticipant_main_branch.rb

Class Method Summary collapse

Methods included from Helpers

column_exists?, columns_exist?

Methods included from Logging

included, #log_error, #log_with_tag, #measure_info

Class Method Details

.calculate_main_branch_name(connection, pacticipant_row) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pact_broker/db/data_migrations/set_pacticipant_main_branch.rb', line 32

def self.calculate_main_branch_name(connection, pacticipant_row)
  candidate_main_branch_query = connection[:tags]
    .select(Sequel[:tags][:name])
    .where(Sequel[:tags][:name] => ["main", "master", "develop"])
    .where(Sequel[:tags][:pacticipant_id] => pacticipant_row[:id])

  candidate_main_branch_query
    .from_self
    .select_group(:name)
    .select_append{ count(1).as(count) }
    .order(Sequel.desc(2))
    .limit(1)
    .collect{ |row| row[:name] }
    .first
end

.call(connection, _options = {}) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/pact_broker/db/data_migrations/set_pacticipant_main_branch.rb', line 13

def self.call(connection, _options = {})
  if required_columns_exist?(connection)
    connection[:pacticipants].select(:id, :name).where(main_branch: nil).each do | pacticipant_row |
      set_main_branch(connection, pacticipant_row)
    end
  end
end

.required_columns_exist?(connection) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/pact_broker/db/data_migrations/set_pacticipant_main_branch.rb', line 48

def self.required_columns_exist?(connection)
  columns_exist?(connection, :pacticipants, [:name, :id, :main_branch]) &&
    columns_exist?(connection, :tags, [:name, :pacticipant_id])
end

.set_main_branch(connection, pacticipant_row) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/pact_broker/db/data_migrations/set_pacticipant_main_branch.rb', line 21

def self.set_main_branch(connection, pacticipant_row)
  main_branch_name = calculate_main_branch_name(connection, pacticipant_row)

  if main_branch_name
    connection[:pacticipants].where(id: pacticipant_row[:id], main_branch: nil).update(main_branch: main_branch_name)
    logger.info("Setting main branch for pacticipant", branch: main_branch_name, pacticipant_name: pacticipant_row[:name])
  else
    logger.info("Cannot determine main branch for pacticipant", branch: nil, pacticipant_name: pacticipant_row[:name])
  end
end