Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final update Antwerpen 2022 values #586

Merged
merged 3 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions db/migrate/20250312105614_antwerpenfinal2022.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class Antwerpenfinal2022 < ActiveRecord::Migration[5.0]
def self.up
directory = Rails.root.join('db/migrate/20250312105614_antwerpenfinal2022')
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
Loading