From a018e7c98105fa9b7fd68a511447cffc7f6a1a5e Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Fri, 21 Mar 2025 18:16:23 +0100 Subject: [PATCH] Move points migration to points rake task --- CHANGELOG.md | 4 ++-- lib/tasks/data_cleanup.rake | 18 ++---------------- lib/tasks/points.rake.ra | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 18 deletions(-) create mode 100644 lib/tasks/points.rake.ra diff --git a/CHANGELOG.md b/CHANGELOG.md index 00ebb102..6247756c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,8 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Added -- `rake data:cleanup:remove_duplicate_points` task added to remove duplicate points from the database and export them to a CSV file. -- `rake data:cleanup:update_points_to_use_lonlat` task added for convenient manual migration of points to the new `lonlat` column. +- `rake data_cleanup:remove_duplicate_points` task added to remove duplicate points from the database and export them to a CSV file. +- `rake points:migrate_to_lonlat` task added for convenient manual migration of points to the new `lonlat` column. - `rake users:activate` task added to activate all users. ## Changed diff --git a/lib/tasks/data_cleanup.rake b/lib/tasks/data_cleanup.rake index 81711fcc..9b9b6095 100644 --- a/lib/tasks/data_cleanup.rake +++ b/lib/tasks/data_cleanup.rake @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'csv' namespace :data_cleanup do @@ -100,20 +102,4 @@ namespace :data_cleanup do puts 'No duplicate points to remove' end end - - desc 'Update points to use lonlat field from latitude and longitude' - task update_points_to_use_lonlat: :environment do - puts 'Updating points to use lonlat...' - - # Use PostGIS functions to properly create geography type - result = ActiveRecord::Base.connection.execute(<<~SQL) - UPDATE points - SET lonlat = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)::geography - WHERE lonlat IS NULL - AND longitude IS NOT NULL - AND latitude IS NOT NULL; - SQL - - puts "Successfully updated #{result.cmd_tuples} points with lonlat values" - end end diff --git a/lib/tasks/points.rake.ra b/lib/tasks/points.rake.ra new file mode 100644 index 00000000..1dc141c6 --- /dev/null +++ b/lib/tasks/points.rake.ra @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +namespace :points do + desc 'Update points to use lonlat field from latitude and longitude' + task migrate_to_lonlat: :environment do + puts 'Updating points to use lonlat...' + + # Use PostGIS functions to properly create geography type + result = ActiveRecord::Base.connection.execute(<<~SQL) + UPDATE points + SET lonlat = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)::geography + WHERE lonlat IS NULL + AND longitude IS NOT NULL + AND latitude IS NOT NULL; + SQL + + puts "Successfully updated #{result.cmd_tuples} points with lonlat values" + end +end