Class: PactBroker::DB::DataMigrations::MigrateIntegrations

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/pact_broker/db/data_migrations/migrate_integrations.rb

Class Method Summary collapse

Methods included from Helpers

column_exists?, columns_exist?

Class Method Details

.call(connection) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pact_broker/db/data_migrations/migrate_integrations.rb', line 9

def self.call(connection)
  self_join = {
    Sequel[:pact_publications][:consumer_id] => Sequel[:existing_integrations][:consumer_id],
    Sequel[:pact_publications][:provider_id] => Sequel[:existing_integrations][:provider_id]
  }

  missing_integrations = connection
                          .from(:pact_publications)
                            .select(
                              Sequel[:pact_publications][:consumer_id],
                              Sequel[:consumer][:name].as(:consumer_name),
                              Sequel[:pact_publications][:provider_id],
                              Sequel[:provider][:name].as(:provider_name),
                              Sequel[:consumer][:created_at]
                            )
                            .distinct
                            .left_outer_join(:integrations, self_join, { :table_alias => :existing_integrations })
                            .join(:pacticipants, { :id => :consumer_id }, { :table_alias => :consumer, implicit_qualifier: :pact_publications })
                            .join(:pacticipants, { :id => :provider_id }, { :table_alias => :provider, implicit_qualifier: :pact_publications })
                            .where(Sequel[:existing_integrations][:provider_id] => nil)

  connection
    .from(:integrations)
    .insert(
    [:consumer_id, :consumer_name, :provider_id, :provider_name, :created_at],
    missing_integrations
  )
end