diff --git a/db/migrations/20180311_optimise_head_matrix.rb b/db/migrations/20180311_optimise_head_matrix.rb index 2ef30f0f1..529f347db 100644 --- a/db/migrations/20180311_optimise_head_matrix.rb +++ b/db/migrations/20180311_optimise_head_matrix.rb @@ -2,34 +2,49 @@ Sequel.migration do up do - + pp = :pact_publications # For each consumer_id/provider_id/tag_name, the version order of the latest version that has a pact create_or_replace_view(:latest_tagged_pact_consumer_version_orders, - "select pp.provider_id, cv.pacticipant_id as consumer_id, t.name as tag_name, max(\"order\") as latest_consumer_version_order - from pact_publications pp - inner join versions cv - on pp.consumer_version_id = cv.id - inner join tags t - on t.version_id = pp.consumer_version_id - group by pp.provider_id, cv.pacticipant_id, t.name" + from(:pact_publications) + .select_group( + Sequel[pp][:provider_id], + Sequel[:cv][:pacticipant_id].as(:consumer_id), + Sequel[:t][:name].as(:tag_name)) + .select_append{ max(order).as(latest_consumer_version_order) } + .join(:versions, { Sequel[pp][:consumer_version_id] => Sequel[:cv][:id] }, { table_alias: :cv} ) + .join(:tags, { Sequel[:t][:version_id] => Sequel[pp][:consumer_version_id] }, { table_alias: :t }) ) - # Add provider_version_order + # Add provider_version_order to original definition + v = :verifications create_or_replace_view(:latest_verifications, - PactBroker::MigrationHelper.sqlite_safe("SELECT v.id, v.number, v.success, s.number as provider_version, - v.build_url, v.pact_version_id, v.execution_date, v.created_at, - v.provider_version_id, s.number as provider_version_number, - s.order as provider_version_order - FROM verifications v - INNER JOIN latest_verification_numbers lv - ON v.pact_version_id = lv.pact_version_id - AND v.number = lv.latest_number - INNER JOIN versions s on v.provider_version_id = s.id") - ) + from(v) + .select( + Sequel[v][:id], + Sequel[v][:number], + Sequel[v][:success], + Sequel[:s][:number].as(:provider_version), + Sequel[v][:build_url], + Sequel[v][:pact_version_id], + Sequel[v][:execution_date], + Sequel[v][:created_at], + Sequel[v][:provider_version_id], + Sequel[:s][:number].as(:provider_version_number), + Sequel[:s][:order].as(:provider_version_order)) + .join(:latest_verification_numbers, + { + Sequel[v][:pact_version_id] => Sequel[:lv][:pact_version_id], + Sequel[v][:number] => Sequel[:lv][:latest_number] + }, { table_alias: :lv }) + .join(:versions, + { + Sequel[v][:provider_version_id] => Sequel[:s][:id] + }, { table_alias: :s }) + ) - create_or_replace_view(:head_matrix, - PactBroker::MigrationHelper.sqlite_safe(" + create_or_replace_view(:head_matrix, + " select p.consumer_id, p.consumer_name, p.consumer_version_id, p.consumer_version_number, p.consumer_version_order, p.id as pact_publication_id, p.pact_version_id, p.pact_version_sha, p.revision_number as pact_revision_number, @@ -59,7 +74,7 @@ and lt.latest_consumer_version_order = p.consumer_version_order left outer join latest_verifications lv on p.pact_version_id = lv.pact_version_id - ") + " ) end end diff --git a/db/migrations/migration_helper.rb b/db/migrations/migration_helper.rb index 0436c32b2..4be2c6205 100644 --- a/db/migrations/migration_helper.rb +++ b/db/migrations/migration_helper.rb @@ -23,7 +23,7 @@ def adapter end def sqlite_safe string - if adapter == 'sqlite' || adapter =~ /mysql/ + if adapter == 'sqlite' string.gsub(/(?:\b|")order(?:"|\b)/, '`order`') else string