-
-
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: add consumer_id to pact_publications table
- Loading branch information
Showing
8 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
db/migrations/20180615_add_consumer_id_to_pact_publications.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,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 |
7 changes: 7 additions & 0 deletions
7
db/migrations/20180616_migrate_consumer_ids_for_pact_publications.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,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 |
31 changes: 31 additions & 0 deletions
31
lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.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,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 |
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
40 changes: 40 additions & 0 deletions
40
spec/lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications_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,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 |
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