Class: PactBroker::DB::DataMigrations::SetConsumerIdsForPactPublications

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.rb

Class Method Summary collapse

Class Method Details

.call(connection) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.rb', line 7

def self.call connection
  if columns_exist?(connection)
    query = "UPDATE pact_publications
            SET consumer_id = (SELECT pacticipant_id
              FROM versions
              WHERE id = pact_publications.consumer_version_id)
            WHERE consumer_id is null"
    connection.run(query)
  end
end

.column_exists?(connection, table, column) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.rb', line 25

def self.column_exists?(connection, table, column)
  connection.table_exists?(table) && connection.schema(table).find{|col| col.first == column }
end

.columns_exist?(connection) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# File 'lib/pact_broker/db/data_migrations/set_consumer_ids_for_pact_publications.rb', line 18

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