Skip to content

Commit

Permalink
feat: ensure migrations in optimise_head_matrix work on mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Mar 29, 2018
1 parent 164d738 commit 35874a9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
59 changes: 37 additions & 22 deletions db/migrations/20180311_optimise_head_matrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion db/migrations/migration_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 35874a9

Please sign in to comment.