lib/data_update_scripts/20200826141447_nullify_orphaned_page_views_by_user.rb

Summary

Maintainability
A
0 mins
Test Coverage
module DataUpdateScripts
  class NullifyOrphanedPageViewsByUser
    def run
      # Nullify all PageViews belonging to Users that don't exist anymore
      ActiveRecord::Base.connection.execute(
        <<~SQL.squish,
          UPDATE page_views
          SET user_id = NULL
          WHERE user_id IS NOT NULL
          AND user_id NOT IN (SELECT id FROM users);
        SQL
      )
    end
  end
end