Move points migration to points rake task

This commit is contained in:
Eugene Burmakin 2025-03-21 18:16:23 +01:00
parent dbd9b7f31f
commit a018e7c981
3 changed files with 23 additions and 18 deletions

View file

@ -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

View file

@ -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

19
lib/tasks/points.rake.ra Normal file
View file

@ -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