Skip to content

Commit

Permalink
Dataset Province of Noord-Holland update to 2022 (#542)
Browse files Browse the repository at this point in the history
* 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
kaskranenburgQ authored Oct 28, 2024
1 parent 5a11018 commit f587c60
Show file tree
Hide file tree
Showing 5 changed files with 910 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ PLATFORMS
arm64-darwin-20
arm64-darwin-21
arm64-darwin-22
arm64-darwin-23
x86_64-darwin-21
x86_64-darwin-22
x86_64-darwin-23
Expand Down
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
Loading

0 comments on commit f587c60

Please sign in to comment.