Skip to content

Commit

Permalink
feat: set main branch for pacticipant
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 30, 2021
1 parent 6ba33f9 commit ab1cd01
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
53 changes: 53 additions & 0 deletions lib/pact_broker/db/data_migrations/set_pacticipant_main_branch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'pact_broker/db/data_migrations/helpers'
require 'pact_broker/logging'

module PactBroker
module DB
module DataMigrations
class SetPacticipantMainBranch
include PactBroker::Logging
extend Helpers

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

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", payload: { branch: main_branch_name, pacticipant_name: pacticipant_row[:name] })
else
logger.info("Cannot determine main branch for pacticipant", payload: { branch: nil, pacticipant_name: pacticipant_row[:name] })
end
end

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

def self.required_columns_exist?(connection)
columns_exist?(connection, :pacticipants, [:name, :id, :main_branch]) &&
columns_exist?(connection, :tags, [:name, :pacticipant_id])
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/pact_broker/db/migrate_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def self.call database_connection, options = {}
DataMigrations::SetCreatedAtForLatestVerifications.call(database_connection)
DataMigrations::SetExtraColumnsForTags.call(database_connection)
DataMigrations::SetPacticipantDisplayName.call(database_connection)
DataMigrations::SetPacticipantMainBranch.call(database_connection)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'pact_broker/db/data_migrations/set_pacticipant_main_branch'

module PactBroker
module DB
module DataMigrations
describe SetPacticipantMainBranch, data_migration: true do
describe ".call" do
before (:all) do
PactBroker::Database.migrate(20210529)
end

let(:now) { DateTime.new(2018, 2, 2) }
let!(:pacticipant_1) { create(:pacticipants, { name: "P1", created_at: now, updated_at: now }) }
let!(:pacticipant_2) { create(:pacticipants, { name: "P2", created_at: now, updated_at: now }) }

def create_version_with_tag(version_number, order, tag_name, pacticipant_id)
version = create(:versions, { number: version_number, order: order, pacticipant_id: pacticipant_id, created_at: now, updated_at: now })
create(:tags, { name: tag_name, pacticipant_id: pacticipant_id, version_id: version[:id], created_at: now, updated_at: now }, nil)
end

before do
create_version_with_tag("1", 1, "main", pacticipant_1[:id])
create_version_with_tag("2", 2, "main", pacticipant_1[:id])
create_version_with_tag("3", 3, "develop", pacticipant_1[:id])
create_version_with_tag("4", 4, "feat/x", pacticipant_1[:id])

create_version_with_tag("5", 5, "foo", pacticipant_2[:id])
end

subject { SetPacticipantMainBranch.call(database) }

it "sets the main branch where it can" do
subject
expect(database[:pacticipants].where(id: pacticipant_1[:id]).single_record[:main_branch]).to eq "main"
expect(database[:pacticipants].where(id: pacticipant_2[:id]).single_record[:main_branch]).to eq nil
end
end
end
end
end
end

0 comments on commit ab1cd01

Please sign in to comment.