diff --git a/db/ddl_statements.rb b/db/ddl_statements.rb new file mode 100644 index 000000000..e9325dc9a --- /dev/null +++ b/db/ddl_statements.rb @@ -0,0 +1,3 @@ +Dir.glob(File.expand_path(File.join(__FILE__, "..", "ddl_statements", "*.rb"))).sort.each do | path | + require path +end diff --git a/db/ddl_statements/head_matrix_v001.rb b/db/ddl_statements/head_matrix_v001.rb new file mode 100644 index 000000000..4468ac8bc --- /dev/null +++ b/db/ddl_statements/head_matrix_v001.rb @@ -0,0 +1,31 @@ +HEAD_MATRIX_V1 = " +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, + p.created_at as pact_created_at, + p.provider_id, p.provider_name, lv.provider_version_id, lv.provider_version_number, lv.provider_version_order, + lv.id as verification_id, lv.success, lv.number as verification_number, lv.execution_date as verification_executed_at, + lv.build_url as verification_build_url, + null as consumer_version_tag_name +from latest_pact_publications p +left outer join latest_verifications lv + on p.pact_version_id = lv.pact_version_id + +union all + +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, + p.created_at as pact_created_at, + p.provider_id, p.provider_name, lv.provider_version_id, lv.provider_version_number, lv.provider_version_order, + lv.id as verification_id, lv.success, lv.number as verification_number, lv.execution_date as verification_executed_at, + lv.build_url as verification_build_url, + lt.tag_name as consumer_version_tag_name +from latest_tagged_pact_consumer_version_orders lt +inner join latest_pact_publications_by_consumer_versions p + on lt.consumer_id = p.consumer_id + and lt.provider_id = p.provider_id + 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 +" diff --git a/db/ddl_statements/head_matrix_v002.rb b/db/ddl_statements/head_matrix_v002.rb new file mode 100644 index 000000000..804dda13c --- /dev/null +++ b/db/ddl_statements/head_matrix_v002.rb @@ -0,0 +1,31 @@ +HEAD_MATRIX_V2 = " +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, + p.created_at as pact_created_at, + p.provider_id, p.provider_name, lv.provider_version_id, lv.provider_version_number, lv.provider_version_order, + lv.id as verification_id, lv.success, lv.number as verification_number, lv.execution_date as verification_executed_at, + lv.build_url as verification_build_url, + null as consumer_version_tag_name +from latest_pact_publications p +left outer join latest_verifications_for_provider_versions lv + on p.pact_version_id = lv.pact_version_id + +union all + +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, + p.created_at as pact_created_at, + p.provider_id, p.provider_name, lv.provider_version_id, lv.provider_version_number, lv.provider_version_order, + lv.id as verification_id, lv.success, lv.number as verification_number, lv.execution_date as verification_executed_at, + lv.build_url as verification_build_url, + lt.tag_name as consumer_version_tag_name +from latest_tagged_pact_consumer_version_orders lt +inner join latest_pact_publications_by_consumer_versions p + on lt.consumer_id = p.consumer_id + and lt.provider_id = p.provider_id + and lt.latest_consumer_version_order = p.consumer_version_order +left outer join latest_verifications_for_provider_versions lv + on p.pact_version_id = lv.pact_version_id +" diff --git a/db/ddl_statements/latest_verification_ids_for_provider_versions_v001.rb b/db/ddl_statements/latest_verification_ids_for_provider_versions_v001.rb new file mode 100644 index 000000000..78644bc8d --- /dev/null +++ b/db/ddl_statements/latest_verification_ids_for_provider_versions_v001.rb @@ -0,0 +1,4 @@ +LATEST_VERIFICATION_IDS_FOR_PROVIDER_VERSIONS_V1 = + "select pact_version_id, MAX(verification_id) latest_verification_id + FROM latest_verification_id_for_pact_version_and_provider_version + GROUP BY pact_version_id" diff --git a/db/migrations/20180311_optimise_head_matrix.rb b/db/migrations/20180311_optimise_head_matrix.rb index fc3db6d59..aafc3df30 100644 --- a/db/migrations/20180311_optimise_head_matrix.rb +++ b/db/migrations/20180311_optimise_head_matrix.rb @@ -1,4 +1,5 @@ require_relative 'migration_helper' +require_relative '../ddl_statements' Sequel.migration do up do @@ -46,38 +47,6 @@ ) - 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, - p.created_at as pact_created_at, - p.provider_id, p.provider_name, lv.provider_version_id, lv.provider_version_number, lv.provider_version_order, - lv.id as verification_id, lv.success, lv.number as verification_number, lv.execution_date as verification_executed_at, - lv.build_url as verification_build_url, - null as consumer_version_tag_name - from latest_pact_publications p - left outer join latest_verifications lv - on p.pact_version_id = lv.pact_version_id - - union all - - 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, - p.created_at as pact_created_at, - p.provider_id, p.provider_name, lv.provider_version_id, lv.provider_version_number, lv.provider_version_order, - lv.id as verification_id, lv.success, lv.number as verification_number, lv.execution_date as verification_executed_at, - lv.build_url as verification_build_url, - lt.tag_name as consumer_version_tag_name - from latest_tagged_pact_consumer_version_orders lt - inner join latest_pact_publications_by_consumer_versions p - on lt.consumer_id = p.consumer_id - and lt.provider_id = p.provider_id - 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 - " - ) + create_or_replace_view(:head_matrix, HEAD_MATRIX_V1) end end diff --git a/db/migrations/20180723_create_latest_verification_ids.rb b/db/migrations/20180723_create_latest_verification_ids.rb index ba8289dda..1b3e78135 100644 --- a/db/migrations/20180723_create_latest_verification_ids.rb +++ b/db/migrations/20180723_create_latest_verification_ids.rb @@ -12,7 +12,8 @@ foreign_key :provider_version_id, :versions, null: false, on_delete: :cascade, foreign_key_constraint_name: 'latest_v_id_for_pv_and_pv_provider_version_id_fk' foreign_key :verification_id, :verifications, null: false, on_delete: :cascade, foreign_key_constraint_name: 'latest_v_id_for_pv_and_pv_verification_id_fk' index [:verification_id], unique: true, name: "latest_v_id_for_pv_and_pv_v_id_unq" - index [:pact_version_id, :provider_version_id], unique: true, name: "unq_latest_verifid_pvid_provid" + index [:pact_version_id, :provider_version_id], unique: true, name: "latest_v_id_for_pv_and_pv_pv_id_pv_id_unq" + index [:pact_version_id, :verification_id], name: "latest_v_id_for_pv_and_pv_pv_id_v_id" end end diff --git a/db/migrations/20180726_recreate_views.rb b/db/migrations/20180726_recreate_views.rb index 32266bf56..a754ad67f 100644 --- a/db/migrations/20180726_recreate_views.rb +++ b/db/migrations/20180726_recreate_views.rb @@ -11,13 +11,6 @@ left outer join latest_verification_id_for_pact_version_and_provider_version lv on lv.pact_version_id = pp.pact_version_id" ) - - # TODO - #drop_view(:latest_matrix) - #drop_view(:latest_verification_id_for_consumer_version_and_provider) - #drop_view(:latest_matrix_for_consumer_version_and_provider_version) - #drop_table(:materialized_matrix) - #drop_table(:materialized_head_matrix) end down do diff --git a/db/migrations/20180728_truncate_materialized_matrix_tables.rb b/db/migrations/20180728_truncate_materialized_matrix_tables.rb new file mode 100644 index 000000000..cb64349c1 --- /dev/null +++ b/db/migrations/20180728_truncate_materialized_matrix_tables.rb @@ -0,0 +1,20 @@ +Sequel.migration do + up do + from(:materialized_matrix).delete + from(:materialized_head_matrix).delete + + # TODO + # drop_view(:latest_matrix) + # drop_view(:latest_verification_id_for_consumer_version_and_provider) + # drop_view(:latest_matrix_for_consumer_version_and_provider_version) + # drop_table(:materialized_matrix) + # drop_table(:materialized_head_matrix) + end + + down do + from(:materialized_matrix).delete + from(:materialized_matrix).insert(from(:matrix).select_all) + from(:materialized_head_matrix).delete + from(:materialized_head_matrix).insert(from(:head_matrix).select_all) + end +end diff --git a/db/migrations/20180729_create_latest_verification_ids_for_provider_versions.rb b/db/migrations/20180729_create_latest_verification_ids_for_provider_versions.rb new file mode 100644 index 000000000..2a4c3ce3a --- /dev/null +++ b/db/migrations/20180729_create_latest_verification_ids_for_provider_versions.rb @@ -0,0 +1,8 @@ +require_relative '../ddl_statements' + +Sequel.migration do + change do + create_view(:latest_verification_ids_for_provider_versions, + LATEST_VERIFICATION_IDS_FOR_PROVIDER_VERSIONS_V1) + end +end diff --git a/db/migrations/20180730_create_latest_verifications_for_provider_versions.rb b/db/migrations/20180730_create_latest_verifications_for_provider_versions.rb new file mode 100644 index 000000000..3ea8a0aaa --- /dev/null +++ b/db/migrations/20180730_create_latest_verifications_for_provider_versions.rb @@ -0,0 +1,29 @@ +Sequel.migration do + change do + # The most recent verification for each pact_version + v = :verifications + create_view(:latest_verifications_for_provider_versions, + from(v) + .select( + Sequel[v][:id], + Sequel[v][:number], + Sequel[v][:success], + 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_ids_for_provider_versions, + { + Sequel[v][:pact_version_id] => Sequel[:lv][:pact_version_id], + Sequel[v][:id] => Sequel[:lv][:latest_verification_id] + }, { table_alias: :lv }) + .join(:versions, + { + Sequel[v][:provider_version_id] => Sequel[:s][:id] + }, { table_alias: :s }) + ) + end +end diff --git a/db/migrations/20180731_update_head_matrix.rb b/db/migrations/20180731_update_head_matrix.rb new file mode 100644 index 000000000..2aa15d5ea --- /dev/null +++ b/db/migrations/20180731_update_head_matrix.rb @@ -0,0 +1,11 @@ +Sequel.migration do + up do + create_or_replace_view(:head_matrix, HEAD_MATRIX_V2) + #TODO + #drop_view(:latest_verifications) + end + + down do + create_or_replace_view(:head_matrix, HEAD_MATRIX_V1) + end +end diff --git a/db/pact_broker_database.sqlite3 b/db/pact_broker_database.sqlite3 index 856f6f89f..dd84baee9 100644 Binary files a/db/pact_broker_database.sqlite3 and b/db/pact_broker_database.sqlite3 differ