Skip to content

Commit

Permalink
feat: add consumer_id to pact_publications table
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 31, 2018
1 parent fa827c4 commit d520e22
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 2 deletions.
13 changes: 13 additions & 0 deletions db/migrations/20180615_add_consumer_id_to_pact_publications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Sequel.migration do
change do
alter_table(:pact_publications) do
add_foreign_key(:consumer_id, :pacticipants)
add_index(:consumer_id, name: "pact_publications_consumer_id_index")
end

# TODO
# alter_table(:pact_publications) do
# set_column_not_null(:consumer_id)
# end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications'

Sequel.migration do
up do
PactBroker::DB::DataMigrations::SetConsumerIdsForPactPublications.call(self)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module PactBroker
module DB
module DataMigrations
class SetConsumerIdsForPactPublications
def self.call connection
if columns_exist?(connection)
ids = connection.from(:pact_publications)
.select(Sequel[:pact_publications][:id], Sequel[:versions][:pacticipant_id].as(:consumer_id))
.join(:versions, {id: :consumer_version_id})
.where(Sequel[:pact_publications][:consumer_id] => nil)

ids.each do | id |
connection.from(:pact_publications).where(id: id[:id]).update(consumer_id: id[:consumer_id])
end
end
end

def self.columns_exist?(connection)
column_exists?(connection, :pact_publications, :consumer_id) &&
column_exists?(connection, :pact_publications, :id) &&
column_exists?(connection, :versions, :id) &&
column_exists?(connection, :versions, :pacticipant_id)
end

def self.column_exists?(connection, table, column)
connection.table_exists?(table) && connection.schema(table).find{|col| col.first == column }
end
end
end
end
end
5 changes: 4 additions & 1 deletion lib/pact_broker/db/migrate_data.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
require 'pact_broker/db/data_migrations/set_pacticipant_ids_for_verifications'
Dir.glob(File.expand_path(File.join(__FILE__, "..", "data_migrations", "*.rb"))).sort.each do | path |
require path
end

module PactBroker
module DB
class MigrateData
def self.call database_connection, options = {}
DataMigrations::SetPacticipantIdsForVerifications.call(database_connection)
DataMigrations::SetConsumerIdsForPactPublications.call(database_connection)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/pact_broker/pacts/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def create params
PactPublication.new(
consumer_version_id: params[:version_id],
provider_id: params[:provider_id],
consumer_id: params[:consumer_id],
pact_version: find_or_create_pact_version(params.fetch(:consumer_id), params.fetch(:provider_id), params[:json_content]),
).save.to_domain
end
Expand All @@ -33,6 +34,7 @@ def update id, params
if existing_model.pact_version_id != pact_version.id
PactPublication.new(
consumer_version_id: existing_model.consumer_version_id,
consumer_id: existing_model.consumer_id,
provider_id: existing_model.provider_id,
revision_number: (existing_model.revision_number + 1),
pact_version: pact_version,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications'

module PactBroker
module DB
module DataMigrations
describe SetConsumerIdsForPactPublications, migration: true do
describe ".call" do
before do
PactBroker::Database.migrate(20180615)
end

let(:now) { DateTime.new(2018, 2, 2) }
let!(:consumer_other) { create(:pacticipants, {name: 'Other consumer', created_at: now, updated_at: now}) }
let!(:consumer) { create(:pacticipants, {name: 'Consumer', created_at: now, updated_at: now}) }
let!(:provider) { create(:pacticipants, {name: 'Provider', created_at: now, updated_at: now}) }
let!(:consumer_version) { create(:versions, {number: '1.2.3', order: 1, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) }
let!(:pact_version) { create(:pact_versions, {content: {some: 'json'}.to_json, sha: '1234', consumer_id: consumer[:id], provider_id: provider[:id], created_at: now}) }
let!(:pact_publication) do
create(:pact_publications, {
consumer_version_id: consumer_version[:id],
provider_id: provider[:id],
revision_number: 1,
pact_version_id: pact_version[:id],
created_at: (now - 1)
})
end

subject { SetConsumerIdsForPactPublications.call(database) }

it "sets the consumer_id" do
expect(database[:pact_publications].first[:consumer_id]).to be nil
subject
expect(database[:pact_publications].first[:consumer_id]).to_not be nil
expect(database[:pact_publications].first[:consumer_id]).to eq consumer[:id]
end
end
end
end
end
end
5 changes: 5 additions & 0 deletions spec/lib/pact_broker/pacts/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ module Pacts
expect{subject}.to change{ PactPublication.count }.by(1)
end

it "sets the consumer_id" do
subject
expect(PactPublication.first.consumer_id).to eq consumer.id
end

it "returns a Pact::Model" do
expect(subject).to be_instance_of(PactBroker::Domain::Pact)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
let(:now) { DateTime.new(2018, 2, 2) }
let!(:consumer) { create(:pacticipants, {name: 'Consumer', created_at: now, updated_at: now}) }
let!(:provider) { create(:pacticipants, {name: 'Provider', created_at: now, updated_at: now}) }
let!(:provider) { create(:pacticipants, {name: 'Provider', created_at: now, updated_at: now}) }
let!(:consumer_version) { create(:versions, {number: '1.2.3', order: 1, pacticipant_id: consumer[:id], created_at: now, updated_at: now}) }
let!(:pact_version) { create(:pact_versions, {content: {some: 'json'}.to_json, sha: '1234', consumer_id: consumer[:id], provider_id: provider[:id], created_at: now}) }
let!(:pact_publication) do
Expand Down

0 comments on commit d520e22

Please sign in to comment.