Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace :freelensia_db do
- desc "Migrate morderated=1 to existed messages"
- task :moderated_messages => :environment do |t, args|
- Message.update_all(moderated: 1)
- end
- desc "Backfill categories source_of"
- task :backfill_listing_categories_source_of => :environment do |t, args|
- Category.where(url: ['interpreters', 'events']).update_all(source_of: 'listing')
- end
- desc "Backfill user custom fields from old origin fields"
- task :backfill_user_custom_fields => :environment do |t, args|
- custom_field_names = ['first name', 'last name', 'display name', 'phone number', 'self introduction']
- mapped_fields = {
- 'first name' => 'origin_given_name',
- 'last name' => 'origin_family_name',
- 'display name' => 'origin_display_name',
- 'phone number' => 'origin_phone_number',
- 'self introduction' => 'origin_description'
- }
- custom_field_ids = CategoryCustomField.source_of('40017','user').map(&:custom_field_id).uniq
- index = 0
- 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')
- .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')
- .find_each do |person|
- custom_field_params = CustomField.where(id: custom_field_ids).inject({}) do |result, custom_field|
- if custom_field_names.include?(custom_field.name(I18n.locale).downcase)
- #custom_field_value = person.find_custom_field_value(custom_field.name(I18n.locale).downcase)
- custom_field_value = person.send(mapped_fields[custom_field.name(I18n.locale).downcase])
- result[custom_field.id] = custom_field_value if custom_field_value.present?
- end
- result
- end.compact
- if custom_field_params.present?
- person.upsert_field_values!(custom_field_params)
- index = index+1
- puts "INDEX: #{index}:"
- puts "-----------------custom_field_params = #{custom_field_params.inspect}"
- puts "-----------------Person #{person.username} was migrated."
- end
- end
- end
- desc "Delete all users in a time range"
- task :delete_users_between_time, ['start_time', 'end_time'] => :environment do |t, args|
- target_users = Person.where("created_at >= ? AND created_at <= ?", args['start_time'].to_time, args['end_time'].to_time)
- target_users.find_each do |target_user|
- UserService::API::Users.delete_user(target_user.id)
- end
- target_users.destroy_all
- end
- end
Add Comment
Please, Sign In to add comment