Skip to content

Commit

Permalink
Merge pull request #693 from unepwcmc/rebuild-compliance-mviews
Browse files Browse the repository at this point in the history
Add script to update mviews
  • Loading branch information
Levia authored Feb 28, 2019
2 parents f055bb9 + 565f9bf commit c473200
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/data/exemptions.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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),,
63 changes: 63 additions & 0 deletions lib/modules/trade/rebuild_compliance_mviews.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module Trade::RebuildComplianceMviews
def self.run
[
:appendix_i,
: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
recreate_non_compliant_view
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
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.recreate_non_compliant_view
db.execute("SELECT rebuild_non_compliant_shipments_view()")
end

def self.db
ActiveRecord::Base.connection
end
end
7 changes: 7 additions & 0 deletions lib/tasks/db_compliance.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace :db do
namespace :compliance do
task :rebuild => :environment do
Trade::RebuildComplianceMviews.run
end
end
end

0 comments on commit c473200

Please sign in to comment.