diff --git a/app/jobs/data_migrations/prefill_points_counter_cache_job.rb b/app/jobs/data_migrations/prefill_points_counter_cache_job.rb index 2a860c58..fe3715ab 100644 --- a/app/jobs/data_migrations/prefill_points_counter_cache_job.rb +++ b/app/jobs/data_migrations/prefill_points_counter_cache_job.rb @@ -16,12 +16,7 @@ class DataMigrations::PrefillPointsCounterCacheJob < ApplicationJob private def prefill_counter_for_user(user_id) - user = User.find(user_id) - points_count = user.points.count - - User.where(id: user_id).update_all(points_count: points_count) - - Rails.logger.info "Updated points_count for user #{user_id}: #{points_count}" + User.reset_counters(user_id, :points) rescue ActiveRecord::RecordNotFound Rails.logger.warn "User #{user_id} not found, skipping counter cache update" end diff --git a/app/services/imports/create.rb b/app/services/imports/create.rb index 67d05abb..58079188 100644 --- a/app/services/imports/create.rb +++ b/app/services/imports/create.rb @@ -28,6 +28,7 @@ class Imports::Create schedule_stats_creating(user.id) schedule_visit_suggesting(user.id, import) update_import_points_count(import) + User.reset_counters(user.id, :points) rescue StandardError => e import.update!(status: :failed) broadcast_status_update diff --git a/db/migrate/20250821192219_add_points_count_to_users.rb b/db/migrate/20250821192219_add_points_count_to_users.rb index b7a9aaa7..86f056a6 100644 --- a/db/migrate/20250821192219_add_points_count_to_users.rb +++ b/db/migrate/20250821192219_add_points_count_to_users.rb @@ -1,15 +1,11 @@ class AddPointsCountToUsers < ActiveRecord::Migration[8.0] def change add_column :users, :points_count, :integer, default: 0, null: false - + # Initialize counter cache for existing users using background job reversible do |dir| dir.up do - # Enqueue job to prefill counter cache in background - # This prevents the migration from blocking on large datasets - say_with_time "Enqueueing job to prefill points counter cache" do - DataMigrations::PrefillPointsCounterCacheJob.perform_later - end + DataMigrations::PrefillPointsCounterCacheJob.perform_later end end end diff --git a/db/schema.rb b/db/schema.rb index 3283db0a..6cb87072 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,6 @@ # # It's strongly recommended that you check this file into your version control system. - ActiveRecord::Schema[8.0].define(version: 2025_08_23_125940) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" diff --git a/spec/services/imports/create_spec.rb b/spec/services/imports/create_spec.rb index 756268f9..1410b2ee 100644 --- a/spec/services/imports/create_spec.rb +++ b/spec/services/imports/create_spec.rb @@ -27,6 +27,15 @@ RSpec.describe Imports::Create do expect(import.reload.source).to eq('owntracks') end + it 'resets points counter cache' do + allow(User).to receive(:reset_counters) + + service.call + + expect(User).to have_received(:reset_counters).with(user.id, :points) + end + + context 'when import succeeds' do it 'sets status to completed' do service.call