-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #563 from quintel/pnh-agriculture-electricity-update
Revising electricity final demand in agriculture for the Province of Noord-Holland
- Loading branch information
Showing
4 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
db/migrate/20241210101108_noord_holland_agriculture_electricity_fix.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
7 changes: 7 additions & 0 deletions
7
db/migrate/20241210101108_noord_holland_agriculture_electricity_fix/commits.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
2 changes: 2 additions & 0 deletions
2
db/migrate/20241210101108_noord_holland_agriculture_electricity_fix/data.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
geo_id,agriculture_final_demand_electricity_demand | ||
PV27_2022,7484 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters