-
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.
Dataset Province of Noord-Holland update to 2022 (#542)
* First version of etlocal update for Noord-Holland 2022 dataset * Updating commit messages of Noord-holland 2022 dataset * Reversing importable commit changes * Adding commit correct commit messages + minor data update * Adding missing mobility data
- Loading branch information
1 parent
5a11018
commit f587c60
Showing
5 changed files
with
910 additions
and
0 deletions.
There are no files selected for viewing
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
65 changes: 65 additions & 0 deletions
65
db/migrate/20240909071009_pv27_noord_holland_create_dataset_2022.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 Pv27NoordHollandCreateDataset2022 < ActiveRecord::Migration[5.0] | ||
def self.up | ||
directory = Rails.root.join('db/migrate/20240909071009_pv27_noord_holland_create_dataset_2022') | ||
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,create_missing_datasets: true) 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 |
Oops, something went wrong.