freelensia

20210621 freelensia_db.rake

Jun 21st, 2021 (edited)
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.63 KB | None | 0 0
  1. namespace :freelensia_db do
  2.   desc "Migrate morderated=1  to existed  messages"
  3.   task :moderated_messages => :environment do |t, args|
  4.     Message.update_all(moderated: 1)
  5.   end
  6.  
  7.   desc "Backfill categories source_of"
  8.   task :backfill_listing_categories_source_of => :environment do |t, args|
  9.     Category.where(url: ['interpreters', 'events']).update_all(source_of: 'listing')
  10.   end
  11.  
  12.   desc "Backfill user custom fields from old origin fields"
  13.   task :backfill_user_custom_fields => :environment do |t, args|
  14.     custom_field_names = ['first name', 'last name', 'display name', 'phone number', 'self introduction']
  15.     mapped_fields = {
  16.       'first name' => 'origin_given_name',
  17.       'last name' => 'origin_family_name',
  18.       'display name' => 'origin_display_name',
  19.       'phone number' => 'origin_phone_number',
  20.       'self introduction' => 'origin_description'
  21.     }
  22.     custom_field_ids = CategoryCustomField.source_of('40017','user').map(&:custom_field_id).uniq
  23.     index = 0
  24.     Person.where('given_name is not null OR family_name is not null OR display_name is not null OR phone_number is not null OR description is not null')
  25.       .select('id, uuid, username, given_name as origin_given_name, family_name as origin_family_name, display_name as origin_display_name, phone_number as origin_phone_number, description as origin_description')
  26.       .find_each do |person|
  27.       custom_field_params = CustomField.where(id: custom_field_ids).inject({}) do |result, custom_field|
  28.         if custom_field_names.include?(custom_field.name(I18n.locale).downcase)
  29.           #custom_field_value = person.find_custom_field_value(custom_field.name(I18n.locale).downcase)
  30.           custom_field_value = person.send(mapped_fields[custom_field.name(I18n.locale).downcase])
  31.           result[custom_field.id] = custom_field_value if custom_field_value.present?
  32.         end
  33.         result
  34.       end.compact
  35.       if custom_field_params.present?
  36.         person.upsert_field_values!(custom_field_params)
  37.         index = index+1
  38.         puts "INDEX: #{index}:"
  39.         puts "-----------------custom_field_params = #{custom_field_params.inspect}"
  40.         puts "-----------------Person #{person.username} was migrated."
  41.       end
  42.     end
  43.   end
  44.  
  45.   desc "Delete all users in a time range"
  46.   task :delete_users_between_time, ['start_time', 'end_time'] => :environment do |t, args|
  47.     target_users = Person.where("created_at >= ? AND created_at <= ?", args['start_time'].to_time, args['end_time'].to_time)
  48.     target_users.find_each do |target_user|
  49.       UserService::API::Users.delete_user(target_user.id)
  50.     end
  51.     target_users.destroy_all
  52.   end
  53. end
  54.  
Add Comment
Please, Sign In to add comment