dawarich/lib/tasks/points.rake
2025-03-22 12:51:39 +01:00

19 lines
626 B
Ruby

# 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