diff --git a/app/jobs/migrations/normalize_geo_area_job.rb b/app/jobs/migrations/normalize_geo_area_job.rb new file mode 100644 index 000000000..e5f8e7d1d --- /dev/null +++ b/app/jobs/migrations/normalize_geo_area_job.rb @@ -0,0 +1,11 @@ +class Migrations::NormalizeGeoAreaJob < ApplicationJob + def perform(ids) + GeoArea.where(id: ids).find_each do |geo_area| + geojson = RGeo::GeoJSON.decode(geo_area.geometry.to_json, geo_factory: RGeo::Geographic.simple_mercator_factory) + geometry = RGeo::GeoJSON.encode(geojson) + geo_area.update_column(:geometry, geometry) + rescue RGeo::Error::InvalidGeometry + geo_area.destroy + end + end +end diff --git a/lib/tasks/deployment/20230215100231_normalize_geometries.rake b/lib/tasks/deployment/20230215100231_normalize_geometries.rake new file mode 100644 index 000000000..275e37169 --- /dev/null +++ b/lib/tasks/deployment/20230215100231_normalize_geometries.rake @@ -0,0 +1,19 @@ +namespace :after_party do + desc 'Deployment task: normalize_geometries' + task normalize_geometries: :environment do + puts "Running deploy task 'normalize_geometries'" + + progress = ProgressReport.new(GeoArea.count) + GeoArea.in_batches(of: 100) do |geo_areas| + ids = geo_areas.ids + Migrations::NormalizeGeoAreaJob.perform_later(ids) + progress.inc(ids.size) + end + progress.finish + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end