-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set main branch for pacticipant
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
lib/pact_broker/db/data_migrations/set_pacticipant_main_branch.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
spec/lib/pact_broker/db/data_migrations/set_pacticipant_main_branch_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |