Skip to content

Commit

Permalink
Merge pull request #131 from Vizzuality/feature/production-seeds
Browse files Browse the repository at this point in the history
TPI Production Ready Seeds
  • Loading branch information
tsubik committed Nov 24, 2019
2 parents b7b9be2 + d89edcc commit 93cba49
Show file tree
Hide file tree
Showing 25 changed files with 795 additions and 1,039 deletions.
1 change: 1 addition & 0 deletions app/admin/cp_assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
year_columns = collection.flat_map(&:emissions_all_years).uniq.sort

column :id
column(:company_id) { |a| a.company.id }
column(:company) { |a| a.company.name }
column :assessment_date
column :publication_date, &:publication_date_csv
Expand Down
1 change: 1 addition & 0 deletions app/admin/instrument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
permit_params :name, :instrument_type_id

config.batch_actions = false
config.sort_order = 'name_asc'

decorate_with InstrumentDecorator

Expand Down
1 change: 1 addition & 0 deletions app/admin/laws_sectors.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ActiveAdmin.register LawsSector do
config.batch_actions = false
config.sort_order = 'name_asc'

menu parent: 'Laws', priority: 5

Expand Down
3 changes: 3 additions & 0 deletions app/admin/mq_assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
form partial: 'form'

index do
column :methodology_version
column :title, &:title_link
column :assessment_date
column :publication_date
Expand All @@ -75,6 +76,8 @@

csv do
column :id
column :methodology_version
column(:company_id) { |a| a.company.id }
column(:company) { |a| a.company.name }
column :assessment_date
column :publication_date, &:publication_date_csv
Expand Down
2 changes: 1 addition & 1 deletion app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Company < ApplicationRecord

validates :ca100, inclusion: {in: [true, false]}
validates_presence_of :name, :slug, :isin, :market_cap_group
validates_uniqueness_of :slug, :isin, :name
validates_uniqueness_of :slug, :name

def to_s
name
Expand Down
13 changes: 12 additions & 1 deletion app/services/csv_import/base_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ class BaseImporter
attr_reader :file, :import_results
attr_accessor :override_id

def initialize(file)
# @param file [File]
# @param options [Hash]
# @option override_id [Boolean] override automatic ids and make use of id in the import data
def initialize(file, options = {})
@file = file
@override_id = options[:override_id] if options[:override_id]
end

def call
Expand All @@ -17,6 +21,7 @@ def call

ActiveRecord::Base.transaction(requires_new: true) do
import
reset_id_seq if override_id
raise ActiveRecord::Rollback if errors.any?
end

Expand Down Expand Up @@ -60,6 +65,12 @@ def csv_converters

private

def reset_id_seq
table_name = resource_klass.table_name
seq_name = "#{table_name}_id_seq"
ActiveRecord::Base.connection.execute("select setval('#{seq_name}', max(id)) from #{table_name};")
end

def reset_import_results
@import_results = {
new_records: 0,
Expand Down
7 changes: 6 additions & 1 deletion app/services/csv_import/companies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def resource_klass
end

def prepare_company(row)
return prepare_overridden_resource(row) if override_id

find_record_by(:id, row) ||
find_record_by(:isin, row) ||
find_record_by(:name, row) ||
Expand All @@ -40,10 +42,13 @@ def company_attributes(row)
name: row[:name],
isin: row[:isin],
sector: find_or_create_tpi_sector(row[:sector]),
market_cap_group: row[:market_cap_group],
market_cap_group: row[:market_cap_group].downcase,
geography: geographies[row[:geography_iso]],
headquarters_geography: geographies[row[:headquarters_geography_iso]],
ca100: row[:ca100],
sedol: row[:sedol],
latest_information: row[:latest_information],
historical_comments: row[:historical_comments],
visibility_status: row[:visibility_status]
}
end
Expand Down
6 changes: 3 additions & 3 deletions app/services/csv_import/cp_assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def prepare_assessment(row)
end

def find_company!(row)
return unless row[:company].present?
return unless row[:company_id].present?

Company.where('lower(name) = ?', row[:company].downcase).first!
Company.find(row[:company_id])
end

def assessment_attributes(row)
Expand All @@ -47,7 +47,7 @@ def assessment_attributes(row)
end

def assessment_date(row)
CSVImport::DateUtils.safe_parse(row[:assessment_date], ['%Y-%m-%d'])
CSVImport::DateUtils.safe_parse(row[:assessment_date], ['%Y-%m-%d']) if row[:assessment_date]
end

def publication_date(row)
Expand Down
7 changes: 4 additions & 3 deletions app/services/csv_import/mq_assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def prepare_assessment(row)
end

def find_company!(row)
return unless row[:company].present?
return unless row[:company_id].present?

Company.where('lower(name) = ?', row[:company].downcase).first!
Company.find(row[:company_id])
end

def assessment_attributes(row)
Expand All @@ -52,7 +52,8 @@ def assessment_attributes(row)
publication_date: publication_date(row),
level: row[:level],
notes: row[:notes],
questions: get_questions(row)
questions: get_questions(row),
methodology_version: row[:methodology_version]
}
end

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20191121170141_remove_isin_index_from_companies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveIsinIndexFromCompanies < ActiveRecord::Migration[6.0]
def change
remove_index :companies, :isin
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMethodologyVersionToMQAssessments < ActiveRecord::Migration[6.0]
def change
add_column :mq_assessments, :methodology_version, :integer
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_11_20_162650) do
ActiveRecord::Schema.define(version: 2019_11_22_133323) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -83,7 +83,6 @@
t.index ["discarded_at"], name: "index_companies_on_discarded_at"
t.index ["geography_id"], name: "index_companies_on_geography_id"
t.index ["headquarters_geography_id"], name: "index_companies_on_headquarters_geography_id"
t.index ["isin"], name: "index_companies_on_isin", unique: true
t.index ["market_cap_group"], name: "index_companies_on_market_cap_group"
t.index ["sector_id"], name: "index_companies_on_sector_id"
t.index ["slug"], name: "index_companies_on_slug", unique: true
Expand Down Expand Up @@ -337,6 +336,7 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "discarded_at"
t.integer "methodology_version"
t.index ["company_id"], name: "index_mq_assessments_on_company_id"
t.index ["discarded_at"], name: "index_mq_assessments_on_discarded_at"
end
Expand Down
71 changes: 61 additions & 10 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,60 @@
sector.update!(cp_unit: sector_cp_unit) if sector.new_record? && sector_cp_unit.present?
end

# instruments: Instrument Type & Instrument
# envs: ALL
# rubocop:disable Style/WordArray
[
['Capacity-building', ['Knowledge generation',
'Sharing and dissemination',
'Research and development',
'Education and training']],
['Regulation', ['Standards and obligations',
'Building codes',
'Zoning and spatial planning',
'Disclosure obligations']],
['Incentives', ['Taxes', 'Subsidies']],
['Governance and planning', ['Creating bodies/institutions',
'Designing processes',
'Developing plans and strategies',
'Assigning responsibilities to other levels of government',
'Monitoring and evaluation']],
['Direct investment', ['Public goods - early warning systems',
'Public goods - other',
'Social safety nets',
'Provision of climate finance']]
].each do |inst_type, instruments|
type = InstrumentType.find_or_create_by!(name: inst_type)
instruments.each do |inst|
Instrument.find_or_create_by!(name: inst, instrument_type: type)
end
end
# rubocop:enable Style/WordArray

# Laws sectors: Names
# envs: ALL
[
'Agriculture',
'Residential and Commercial',
'Coastal zones',
'Economy-wide',
'Energy',
'Health',
'Industry',
'LULUCF',
'Social development',
'Tourism',
'Transportation',
'Urban',
'Waste',
'Water',
'Rural',
'Environment',
'Other'
].each do |sector|
LawsSector.find_or_create_by!(name: sector)
end

if Rails.env.development? || ENV['SEED_DATA']
# import geographies
TimedLogger.log('Import geographies') do
Expand All @@ -37,8 +91,8 @@

# import companies
TimedLogger.log('Import companies') do
file = File.open(Rails.root.join('db', 'seeds', 'companies.csv'), 'r')
CSVImport::Companies.new(file).call
file = File.open(Rails.root.join('db', 'seeds', 'tpi-companies.csv'), 'r')
CSVImport::Companies.new(file, override_id: true).call
end

# import CP Benchmarks
Expand All @@ -60,24 +114,21 @@

file = File.open(Rails.root.join('db', 'seeds', 'mq-assessments-M2.csv'), 'r')
CSVImport::MQAssessments.new(file).call

file = File.open(Rails.root.join('db', 'seeds', 'mq-assessments-M3.csv'), 'r')
CSVImport::MQAssessments.new(file).call
end

# import Legislations
TimedLogger.log('Import Legislations') do
file = File.open(Rails.root.join('db', 'seeds', 'legislations.csv'), 'r')
importer = CSVImport::Legislations.new(file)
importer.override_id = true
importer.call
ActiveRecord::Base.connection.execute("select setval('legislations_id_seq',max(id)) from legislations;")
CSVImport::Legislations.new(file, override_id: true).call
end

TimedLogger.log('Import Litigations') do
# import Litigations
file = File.open(Rails.root.join('db', 'seeds', 'litigations.csv'), 'r')
importer = CSVImport::Litigations.new(file)
importer.override_id = true
importer.call
ActiveRecord::Base.connection.execute("select setval('litigations_id_seq',max(id)) from litigations;")
CSVImport::Litigations.new(file, override_id: true).call

# import Litigation Sides
file = File.open(Rails.root.join('db', 'seeds', 'litigation-sides.csv'), 'r')
Expand Down
Loading

0 comments on commit 93cba49

Please sign in to comment.