-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support multiple branches per version (#495)
* chore: add branches tables * chore: keep track of the latest version for a branch * chore: expose branches in version resource * test: remove branch expectations for creating versions * test: remove tests for updating versions with a branch * chore: update branch head when a version is deleted * chore: update code to find versions by branch using the matrix selector * chore: update latest_for_branch? * refactor: remove latest_version_for_branch code * chore: update PactPublication.latest_for_consumer_branch * test: update expectations * chore: update PactPublication.latest_by_consumer_branch * docs: update sequel annotations * chore: update overall_latest test * chore: use more efficient query for selecting latest for branches * chore: display multiple branches on matrix and index pages * feat: support adding a version to a branch * chore: add branch-version relation to pacticipant * chore: remove references to version.branch * chore: update index and dashboard * chore: remove domain object usage from migration specs * chore: pass in branch for version creation * refactor: remove unused param * style: rubocop * chore: remove unused require * chore: add latest property to branch version decorator * chore: update indexes * chore: update latest by branch query * chore: refactor * chore: migrate version branch column to branch_version object * chore: fix ordering of deployed/released selectors * chore: sort environment selectors by non prod first * test: include provider in latest_by_consumer_branch * chore: remove unnecessary index * chore: add index * chore: update annotations * chore: update logging * chore: fix latest_for_consumer_branch * chore: update hal docs
- Loading branch information
Showing
86 changed files
with
1,529 additions
and
531 deletions.
There are no files selected for viewing
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 @@ | ||
Sequel.migration do | ||
change do | ||
create_table(:branches, charset: "utf8") do | ||
primary_key :id | ||
String :name | ||
foreign_key :pacticipant_id, :pacticipants, null: false, on_delete: :cascade | ||
DateTime :created_at, null: false | ||
DateTime :updated_at, null: false | ||
index [:pacticipant_id, :name], unique: true, name: :branches_pacticipant_id_name_index | ||
end | ||
|
||
create_table(:branch_versions, charset: "utf8") do | ||
primary_key :id | ||
foreign_key :branch_id, :branches, null: false, foreign_key_constraint_name: :branch_versions_branches_fk, on_delete: :cascade | ||
foreign_key :version_id, :versions, null: false, foreign_key_constraint_name: :branch_versions_versions_fk, on_delete: :cascade | ||
Integer :version_order, null: false | ||
Integer :pacticipant_id, null: false | ||
String :branch_name, null: false | ||
DateTime :created_at, null: false | ||
DateTime :updated_at, null: false | ||
index [:branch_id, :version_id], unique: true, name: :branch_versions_branch_id_version_id_index | ||
index [:version_id], name: :branch_versions_version_id_index | ||
index [:branch_name], name: :branch_versions_branch_name_index | ||
# Can probably drop this index when the "latest pact" logic changes | ||
index [:pacticipant_id, :branch_id, :version_order], name: :branch_versions_pacticipant_id_branch_id_version_order_index | ||
end | ||
|
||
create_table(:branch_heads) do | ||
primary_key :id | ||
foreign_key :branch_id, :branches, null: false, on_delete: :cascade | ||
foreign_key :branch_version_id, :branch_versions, null: false, on_delete: :cascade | ||
Integer :version_id, null: false | ||
Integer :pacticipant_id, null: false | ||
String :branch_name, null: false | ||
index [:branch_id], unique: true, name: :branch_heads_branch_id_index | ||
index [:branch_name], name: :branch_heads_branch_name_index | ||
index [:pacticipant_id], name: :branch_heads_pacticipant_id_index | ||
index [:version_id], name: :branch_heads_version_id_index | ||
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
20 changes: 20 additions & 0 deletions
20
lib/pact_broker/api/decorators/branch_version_decorator.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,20 @@ | ||
require "pact_broker/api/decorators/base_decorator" | ||
require "pact_broker/api/decorators/timestamps" | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class BranchVersionDecorator < BaseDecorator | ||
|
||
link :self do | user_options | | ||
{ | ||
title: "Branch version", | ||
href: branch_version_url(represented, user_options.fetch(:base_url)) | ||
} | ||
end | ||
|
||
include Timestamps | ||
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
21 changes: 21 additions & 0 deletions
21
lib/pact_broker/api/decorators/embedded_branch_version_decorator.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,21 @@ | ||
require_relative "base_decorator" | ||
require_relative "timestamps" | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class EmbeddedBranchVersionDecorator < BaseDecorator | ||
property :branch_name, as: :name | ||
property :latest?, as: :latest | ||
|
||
link :self do | options | | ||
{ | ||
title: "Version branch", | ||
name: represented.branch_name, | ||
href: branch_version_url(represented, options[:base_url]) | ||
} | ||
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
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
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
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
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
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,48 @@ | ||
require "pact_broker/api/resources/base_resource" | ||
require "pact_broker/api/decorators/branch_version_decorator" | ||
|
||
module PactBroker | ||
module Api | ||
module Resources | ||
class BranchVersion < BaseResource | ||
def content_types_provided | ||
[["application/hal+json", :to_json]] | ||
end | ||
|
||
def content_types_accepted | ||
[["application/json", :from_json]] | ||
end | ||
|
||
def allowed_methods | ||
["GET", "PUT", "OPTIONS"] | ||
end | ||
|
||
def resource_exists? | ||
!!branch_version | ||
end | ||
|
||
def to_json | ||
decorator_class(:branch_version_decorator).new(branch_version).to_json(decorator_options) | ||
end | ||
|
||
def from_json | ||
already_existed = !!branch_version | ||
@branch_version = branch_service.find_or_create_branch_version(identifier_from_path) | ||
# Make it return a 201 by setting the Location header | ||
response.headers["Location"] = branch_version_url(branch_version, base_url) unless already_existed | ||
response.body = to_json | ||
end | ||
|
||
def policy_name | ||
:'versions::branch_version' | ||
end | ||
|
||
private | ||
|
||
def branch_version | ||
@branch_version ||= branch_service.find_branch_version(identifier_from_path) | ||
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
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
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,97 @@ | ||
require "pact_broker/db/data_migrations/helpers" | ||
|
||
module PactBroker | ||
module DB | ||
module DataMigrations | ||
class CreateBranches | ||
extend Helpers | ||
|
||
def self.call connection | ||
if required_columns_exist?(connection) | ||
branch_ids = create_branch_versions(connection) | ||
upsert_branch_heads(connection, branch_ids) | ||
end | ||
end | ||
|
||
def self.required_columns_exist?(connection) | ||
column_exists?(connection, :versions, :branch) && | ||
connection.table_exists?(:branches) && | ||
connection.table_exists?(:branch_versions) && | ||
connection.table_exists?(:branch_heads) | ||
end | ||
|
||
def self.create_branch_versions(connection) | ||
versions_without_a_branch_version(connection).collect do | version | | ||
create_branch_version(connection, version) | ||
end.uniq | ||
end | ||
|
||
def self.upsert_branch_heads(connection, branch_ids) | ||
branch_ids.each do | branch_id | | ||
upsert_branch_head(connection, branch_id) | ||
end | ||
end | ||
|
||
def self.versions_without_a_branch_version(connection) | ||
branch_versions_join = { | ||
Sequel[:versions][:id] => Sequel[:branch_versions][:version_id], | ||
Sequel[:branch_versions][:branch_name] => Sequel[:versions][:branch] | ||
} | ||
|
||
connection[:versions] | ||
.select(Sequel[:versions].*) | ||
.exclude(branch: nil) | ||
.left_outer_join(:branch_versions, branch_versions_join) | ||
.where(Sequel[:branch_versions][:branch_name] => nil) | ||
.order(:pacticipant_id, :order) | ||
end | ||
|
||
def self.create_branch_version(connection, version) | ||
branch_values = { | ||
name: version[:branch], | ||
pacticipant_id: version[:pacticipant_id], | ||
created_at: version[:created_at], | ||
updated_at: version[:created_at] | ||
} | ||
connection[:branches].insert_ignore.insert(branch_values) | ||
branch_id = connection[:branches].select(:id).where(pacticipant_id: version[:pacticipant_id], name: version[:branch]).single_record[:id] | ||
|
||
branch_version_values = { | ||
pacticipant_id: version[:pacticipant_id], | ||
version_id: version[:id], | ||
version_order: version[:order], | ||
branch_id: branch_id, | ||
branch_name: version[:branch], | ||
created_at: version[:created_at], | ||
updated_at: version[:created_at] | ||
} | ||
|
||
connection[:branch_versions].insert_ignore.insert(branch_version_values) | ||
branch_id | ||
end | ||
|
||
def self.upsert_branch_head(connection, branch_id) | ||
latest_branch_version = connection[:branch_versions].where(branch_id: branch_id).order(:version_order).last | ||
|
||
if connection[:branch_heads].where(branch_id: branch_id).empty? | ||
branch_head_values = { | ||
pacticipant_id: latest_branch_version[:pacticipant_id], | ||
branch_id: branch_id, | ||
branch_version_id: latest_branch_version[:id], | ||
version_id: latest_branch_version[:version_id], | ||
branch_name: latest_branch_version[:branch_name] | ||
} | ||
connection[:branch_heads].insert(branch_head_values) | ||
else | ||
connection[:branch_heads] | ||
.where(branch_id: branch_id) | ||
.update( | ||
branch_version_id: latest_branch_version[:id], | ||
version_id: latest_branch_version[:version_id] | ||
) | ||
end | ||
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
Oops, something went wrong.