From 16ca0086cf0e421a2a8b84ce45975ae32797ce9e Mon Sep 17 00:00:00 2001 From: Kas Kranenburg Date: Tue, 10 Dec 2024 11:15:57 +0100 Subject: [PATCH] Revising electricity final demand in agriculture --- ...ord_holland_agriculture_electricity_fix.rb | 65 +++++++++++++++++++ .../commits.yml | 7 ++ .../data.csv | 2 + db/schema.rb | 2 +- 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20241210101108_noord_holland_agriculture_electricity_fix.rb create mode 100644 db/migrate/20241210101108_noord_holland_agriculture_electricity_fix/commits.yml create mode 100644 db/migrate/20241210101108_noord_holland_agriculture_electricity_fix/data.csv diff --git a/db/migrate/20241210101108_noord_holland_agriculture_electricity_fix.rb b/db/migrate/20241210101108_noord_holland_agriculture_electricity_fix.rb new file mode 100644 index 00000000..1e027270 --- /dev/null +++ b/db/migrate/20241210101108_noord_holland_agriculture_electricity_fix.rb @@ -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 diff --git a/db/migrate/20241210101108_noord_holland_agriculture_electricity_fix/commits.yml b/db/migrate/20241210101108_noord_holland_agriculture_electricity_fix/commits.yml new file mode 100644 index 00000000..1ae54ed4 --- /dev/null +++ b/db/migrate/20241210101108_noord_holland_agriculture_electricity_fix/commits.yml @@ -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 + diff --git a/db/migrate/20241210101108_noord_holland_agriculture_electricity_fix/data.csv b/db/migrate/20241210101108_noord_holland_agriculture_electricity_fix/data.csv new file mode 100644 index 00000000..f11657be --- /dev/null +++ b/db/migrate/20241210101108_noord_holland_agriculture_electricity_fix/data.csv @@ -0,0 +1,2 @@ +geo_id,agriculture_final_demand_electricity_demand +PV27_2022,7484 \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 504010e4..f436940f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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"