Add a task to mask existing IPs collected with Ahoy

According to the README [1]:

> To mask previously collected IPs, use:
> Ahoy::Visit.find_each do |visit|
>   visit.update_column :ip, Ahoy.mask_ip(visit.ip)
> end

We're adapting the code with our version, since we use the `Visit` model
instead of the `Ahoy::Visit` model.

[1] https://github.com/ankane/ahoy/blob/v5.0.2/README.md#ip-masking
This commit is contained in:
Javi Martín
2024-04-25 23:40:16 +02:00
parent 96ae69fe93
commit 144d1d8d05
3 changed files with 43 additions and 3 deletions

View File

@@ -2,8 +2,10 @@ namespace :consul do
desc "Runs tasks needed to upgrade to the latest version" desc "Runs tasks needed to upgrade to the latest version"
task execute_release_tasks: ["settings:rename_setting_keys", task execute_release_tasks: ["settings:rename_setting_keys",
"settings:add_new_settings", "settings:add_new_settings",
"execute_release_2.1.0_tasks"] "execute_release_2.2.0_tasks"]
desc "Runs tasks needed to upgrade from 2.0.1 to 2.1.0" desc "Runs tasks needed to upgrade from 2.1.1 to 2.2.0"
task "execute_release_2.1.0_tasks": [] task "execute_release_2.2.0_tasks": [
"db:mask_ips"
]
end end

View File

@@ -4,4 +4,15 @@ namespace :db do
I18n.enforce_available_locales = false I18n.enforce_available_locales = false
Tenant.switch(args[:tenant]) { load(Rails.root.join("db", "dev_seeds.rb")) } Tenant.switch(args[:tenant]) { load(Rails.root.join("db", "dev_seeds.rb")) }
end end
desc "Mask IPs collected with Ahoy"
task mask_ips: :environment do
ApplicationLogger.new.info "Masking tracked IPs collected with Ahoy"
Tenant.run_on_each do
Visit.find_each do |visit|
visit.update_column :ip, Ahoy.mask_ip(visit.ip)
end
end
end
end end

27
spec/lib/tasks/db_spec.rb Normal file
View File

@@ -0,0 +1,27 @@
require "rails_helper"
describe "rake db:mask_ips" do
before { Rake::Task["db:mask_ips"].reenable }
it "mask IPs on all tenants" do
create(:visit, ip: "1.1.1.1")
create(:visit, ip: "1.1.1.2")
create(:visit, ip: "1.1.2.2")
create(:tenant, schema: "myhometown")
Tenant.switch("myhometown") do
create(:visit, ip: "1.1.1.1")
create(:visit, ip: "1.1.1.2")
create(:visit, ip: "1.1.3.3")
end
Rake.application.invoke_task("db:mask_ips")
expect(Visit.pluck(:ip)).to match_array %w[1.1.1.0 1.1.1.0 1.1.2.0]
Tenant.switch("myhometown") do
expect(Visit.pluck(:ip)).to match_array %w[1.1.1.0 1.1.1.0 1.1.3.0]
end
end
end