Skip to content

Commit

Permalink
Merge pull request #563 from quintel/pnh-agriculture-electricity-update
Browse files Browse the repository at this point in the history
Revising electricity final demand in agriculture for the Province of Noord-Holland
  • Loading branch information
DorinevanderVlies authored Dec 13, 2024
2 parents 10dec16 + 16ca008 commit ca6130f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class NoordHollandAgricultureElectricityFix < ActiveRecord::Migration[5.0]
def self.up
directory = Rails.root.join('db/migrate/20241210101108_noord_holland_agriculture_electricity_fix')
data_path = directory.join('data.csv')
commits_path = directory.join('commits.yml')
datasets = []

# By default, CSVImporter only allows updating existing datasets. If the
# migration is adding a new dataset, add the `create_missing_datasets`
# keyword argument. For example:
#
# CSVImporter.run(data_path, commits_path, create_missing_datasets: true) do |row, runner|
# # ...
# end
#
CSVImporter.run(data_path, commits_path) do |row, runner|
print "Updating #{row['geo_id']}... "
commits = runner.call

if commits.any?
datasets.push(find_dataset(commits))
puts 'done!'
else
puts 'nothing to change!'
end
end

sleep(1)
puts
puts "Updated #{datasets.length} datasets with the following IDs:"
puts " #{datasets.map(&:id).join(',')}"
rescue ActiveRecord::RecordInvalid => e
if e.record.is_a?(Commit) && e.record.errors['dataset_edits.value'].any?
warn('')
warn('-' * 80)
warn('The following errors occurred while processing CSV rows:')
warn('')

# Grab all the errors from individual datasets to show those instead. This is typically
# the case when a CSV specifies a value that is not allowed to be edited.
e.record
.dataset_edits
.reject(&:valid?)
.each do |edit|
edit.errors.each do |field, msg|
warn("* #{edit.commit.dataset.geo_id}: #{edit.key}: #{field} #{msg}")
end
end

warn('-' * 80)
end

raise e
end

def self.down
raise ActiveRecord::IrreversibleMigration
end

def find_dataset(commits)
commits.each do |commit|
return commit.dataset if commit&.dataset
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- fields:
# List each field from the data CSV to be included in this commit.
- agriculture_final_demand_electricity_demand
message:
Elektriciteit geleverd aan Landbouw, bosbouw en visserij (SBI A) + Elektriciteit geproduceerd in wkk's (berekend o.b.v. emissieregistratie). Link naar Klimaatmonitor: https://klimaatmonitor.databank.nl/Jive?workspace_guid=bc1a0b72-44e3-47db-9994-e3b198f8665c

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
geo_id,agriculture_final_demand_electricity_demand
PV27_2022,7484
2 changes: 1 addition & 1 deletion 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[7.0].define(version: 2024_11_19_104514) do
ActiveRecord::Schema[7.0].define(version: 2024_12_10_101108) do
create_table "commits", id: :integer, charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t|
t.integer "source_id"
t.integer "user_id"
Expand Down

0 comments on commit ca6130f

Please sign in to comment.