From ab1cd0163fa5a2c467c5e73ebd817ba91194002e Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sun, 30 May 2021 15:49:04 +1000 Subject: [PATCH] feat: set main branch for pacticipant --- .../set_pacticipant_main_branch.rb | 53 +++++++++++++++++++ lib/pact_broker/db/migrate_data.rb | 1 + .../set_pacticipant_main_branch_spec.rb | 41 ++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 lib/pact_broker/db/data_migrations/set_pacticipant_main_branch.rb create mode 100644 spec/lib/pact_broker/db/data_migrations/set_pacticipant_main_branch_spec.rb diff --git a/lib/pact_broker/db/data_migrations/set_pacticipant_main_branch.rb b/lib/pact_broker/db/data_migrations/set_pacticipant_main_branch.rb new file mode 100644 index 000000000..ff54b45fb --- /dev/null +++ b/lib/pact_broker/db/data_migrations/set_pacticipant_main_branch.rb @@ -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 diff --git a/lib/pact_broker/db/migrate_data.rb b/lib/pact_broker/db/migrate_data.rb index 224835dcc..4f53e9ff9 100644 --- a/lib/pact_broker/db/migrate_data.rb +++ b/lib/pact_broker/db/migrate_data.rb @@ -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 diff --git a/spec/lib/pact_broker/db/data_migrations/set_pacticipant_main_branch_spec.rb b/spec/lib/pact_broker/db/data_migrations/set_pacticipant_main_branch_spec.rb new file mode 100644 index 000000000..5ab39d2ff --- /dev/null +++ b/spec/lib/pact_broker/db/data_migrations/set_pacticipant_main_branch_spec.rb @@ -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