From d60b5dd406c32f14df8343da1bccbe38fcbd7fb8 Mon Sep 17 00:00:00 2001 From: Ferdinando Primerano Date: Tue, 26 Feb 2019 14:03:35 +0000 Subject: [PATCH 1/7] Create module to rebuild compliance mviews --- .../trade/rebuild_compliance_mviews.rb | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 lib/modules/trade/rebuild_compliance_mviews.rb diff --git a/lib/modules/trade/rebuild_compliance_mviews.rb b/lib/modules/trade/rebuild_compliance_mviews.rb new file mode 100644 index 0000000000..9d2b4eabef --- /dev/null +++ b/lib/modules/trade/rebuild_compliance_mviews.rb @@ -0,0 +1,44 @@ +module Trade::RebuildComplianceMviews + def self.run + [ + :appendix_i, + :mandatory_quotas, + :cites_suspensions + ].each do |p| + puts "Rebuild #{p} mview..." + self.rebuild_compliance_mview(p) + end + end + + def self.rebuild_compliance_mview(type) + views = Dir["db/views/trade_shipments_#{type}_view/*"] + latest_view = views.map { |v| v.split("/").last }.sort.last.split('.').first + self.recreate_mview(type, latest_view) + end + + def self.recreate_mview(type, sql_view) + view_name = "trade_shipments_#{type}_view" + mview_name = "trade_shipments_#{type}_mview" + ActiveRecord::Base.transaction do + command = "DROP MATERIALIZED VIEW IF EXISTS #{mview_name} CASCADE" + puts command + puts db.execute(command) + + command = "DROP VIEW IF EXISTS #{view_name}" + puts command + db.execute(command) + + command = "CREATE VIEW #{view_name} AS #{ActiveRecord::Migration.view_sql(sql_view, view_name)}" + puts command + db.execute(command) + + command = "CREATE MATERIALIZED VIEW #{mview_name} AS SELECT * FROM #{view_name}" + puts command + db.execute(command) + end + end + + def self.db + ActiveRecord::Base.connection + end +end From fcf8672e68b6de4e541f2411e04a55240f3c0924 Mon Sep 17 00:00:00 2001 From: Ferdinando Primerano Date: Tue, 26 Feb 2019 14:04:02 +0000 Subject: [PATCH 2/7] Add rake task to run compliance rebuild script --- lib/tasks/db_compliance.rake | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 lib/tasks/db_compliance.rake diff --git a/lib/tasks/db_compliance.rake b/lib/tasks/db_compliance.rake new file mode 100644 index 0000000000..c640a60a42 --- /dev/null +++ b/lib/tasks/db_compliance.rake @@ -0,0 +1,7 @@ +namespace :db do + namespace :compliance do + task :rebuild => :environment do + Trade::RebuildComplianceMviews.run + end + end +end From dc89d72e86c43144f93f77d837d72c6f6db667fa Mon Sep 17 00:00:00 2001 From: Ferdinando Primerano Date: Tue, 26 Feb 2019 16:49:58 +0000 Subject: [PATCH 3/7] Rebuild also sql scripts --- lib/modules/trade/rebuild_compliance_mviews.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/modules/trade/rebuild_compliance_mviews.rb b/lib/modules/trade/rebuild_compliance_mviews.rb index 9d2b4eabef..ef45b5d1a3 100644 --- a/lib/modules/trade/rebuild_compliance_mviews.rb +++ b/lib/modules/trade/rebuild_compliance_mviews.rb @@ -5,11 +5,25 @@ def self.run :mandatory_quotas, :cites_suspensions ].each do |p| + time = "#{Time.now.hour}#{Time.now.min}#{Time.now.sec}" + timestamp = Date.today.to_s.gsub('-', '') + time + puts "Rebuild #{p} SQL script..." + self.rebuild_sql_views(p, timestamp) puts "Rebuild #{p} mview..." self.rebuild_compliance_mview(p) end end + def self.rebuild_sql_views(type, timestamp) + compliance_type = + case type + when :appendix_i then Trade::AppendixIReservationsShipments + when :mandatory_quotas then Trade::MandatoryQuotasShipments + when :cites_suspensions then Trade::CitesSuspensionsShipments + end + compliance_type.new.generate_view(timestamp) + end + def self.rebuild_compliance_mview(type) views = Dir["db/views/trade_shipments_#{type}_view/*"] latest_view = views.map { |v| v.split("/").last }.sort.last.split('.').first From e0c5e2216fcd6d5ef2fa2421433a9b3da4f96e62 Mon Sep 17 00:00:00 2001 From: Ferdinando Primerano Date: Tue, 26 Feb 2019 16:58:37 +0000 Subject: [PATCH 4/7] Rebuild non compliant view as well --- lib/modules/trade/rebuild_compliance_mviews.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/modules/trade/rebuild_compliance_mviews.rb b/lib/modules/trade/rebuild_compliance_mviews.rb index ef45b5d1a3..5ef4c771d9 100644 --- a/lib/modules/trade/rebuild_compliance_mviews.rb +++ b/lib/modules/trade/rebuild_compliance_mviews.rb @@ -12,6 +12,7 @@ def self.run puts "Rebuild #{p} mview..." self.rebuild_compliance_mview(p) end + recreate_non_compliant_view end def self.rebuild_sql_views(type, timestamp) @@ -52,6 +53,10 @@ def self.recreate_mview(type, sql_view) end end + def recreate_non_compliant_view + db.execute("SELECT rebuild_non_compliant_shipments_view()") + end + def self.db ActiveRecord::Base.connection end From 9994332882fd506aa6c6176cf3133498c036273f Mon Sep 17 00:00:00 2001 From: lucacug Date: Wed, 27 Feb 2019 10:29:12 +0000 Subject: [PATCH 5/7] add missing self to method definition --- lib/modules/trade/rebuild_compliance_mviews.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/trade/rebuild_compliance_mviews.rb b/lib/modules/trade/rebuild_compliance_mviews.rb index 5ef4c771d9..4a07e16565 100644 --- a/lib/modules/trade/rebuild_compliance_mviews.rb +++ b/lib/modules/trade/rebuild_compliance_mviews.rb @@ -53,7 +53,7 @@ def self.recreate_mview(type, sql_view) end end - def recreate_non_compliant_view + def self.recreate_non_compliant_view db.execute("SELECT rebuild_non_compliant_shipments_view()") end From 2f12cfe6a72951c0f9cd204b2b8aa08eff6d8fe3 Mon Sep 17 00:00:00 2001 From: lucacug Date: Thu, 28 Feb 2019 10:29:10 +0000 Subject: [PATCH 6/7] add new exemptions csv file --- lib/data/exemptions.csv | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/data/exemptions.csv b/lib/data/exemptions.csv index 20081b9d17..35c9f15dd2 100644 --- a/lib/data/exemptions.csv +++ b/lib/data/exemptions.csv @@ -345,3 +345,4 @@ F. campani, F. lateralis, F. minor, F. oustaleti, F. pardalis, F. petteri, F. rh 02/02/2012 00:00,09/04/2015 00:00,Mesoplodon traversii,4039,SPECIES,ALL,ALL,ALL,ALL,ALL,,"No issuance of CITES import or export permits, or certificates for introduction from the sea for primarily commercial purposes for any specimen of a species or stock protected from commercial whaling by the IWC. See Schedule on http://cites.org/sites/default/files/common/notif/2012/E010A.pdf",Not a great whale so not covered by IWC suspension,, 02/02/2012 00:00,09/04/2015 00:00,Tasmacetus shepherdi,8310,SPECIES,ALL,ALL,ALL,ALL,ALL,,"No issuance of CITES import or export permits, or certificates for introduction from the sea for primarily commercial purposes for any specimen of a species or stock protected from commercial whaling by the IWC. See Schedule on http://cites.org/sites/default/files/common/notif/2012/E010A.pdf",Not a great whale so not covered by IWC suspension,, 02/02/2012 00:00,09/04/2015 00:00,Ziphius cavirostris,5326,SPECIES,ALL,ALL,ALL,ALL,ALL,,"No issuance of CITES import or export permits, or certificates for introduction from the sea for primarily commercial purposes for any specimen of a species or stock protected from commercial whaling by the IWC. See Schedule on http://cites.org/sites/default/files/common/notif/2012/E010A.pdf",Not a great whale so not covered by IWC suspension,, +01/01/2015,01/01/2017,Pterocarpus erinaceus,67733,SPECIES,NG,ALL,ALL,ALL,ALL,,,The suspension (a country suspension for NG) ended before this species was listed on App III (AP 150219),, From 565f9bf04764699ceda895fd7ee85bb574b8f66c Mon Sep 17 00:00:00 2001 From: Ferdinando Primerano Date: Thu, 28 Feb 2019 10:48:38 +0000 Subject: [PATCH 7/7] Update CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index be643aec3e..fdf67b7f62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### 1.1.2 + +* Update exemptions file for CITES suspension in Compliance Tool +* Add script to rebuild Compliance Tool related mviews + ### 1.1.1 * Fix and improve distributions import scripts