lib/tasks/migrate.rake
# usage: rake migrate:recipient RECIPIENT=<id>
rake migrate:recipient RECIPIENT=
rake migrate : recipient RECIPIENT =
def migrate(id)
    recipient = Recipient.find_by(id: id)
    return puts "No Recipient #{id}" unless recipient
    print "Recipient #{id}"
    proposals = Proposal.where(recipient_id: recipient.id)
    users = User.where(organisation_id: recipient.id)
    funds = if recipient&.subscription.try(:active)
              Fund.active
            elsif recipient.reveals.any?
              Fund.where(slug: recipient.reveals).active
            end
    return puts ': no Users' unless users.any?
    users.each_with_index do |user, index|
      if index > 0
        next puts "└─ [Skip]: User #{user.id}" if user.recipients.any?
        print "└─ [Duplicating] Recipient #{id}"
        new_recipient = recipient.dup
        new_recipient.slug = new_recipient.generate_slug(
          new_recipient, new_recipient.slug
        )
        new_recipient.duplicate_of = recipient.id
        new_recipient.save(validate: false)
        print "->#{new_recipient.id}"
        new_proposals = proposals.map do |proposal|
          new_proposal = proposal.dup
          new_proposal.recipient = new_recipient
          new_proposal.countries = proposal.countries
          new_proposal.districts = proposal.districts
          new_proposal.themes = proposal.themes
          new_proposal.duplicate_of = proposal.id
          new_proposal.save(validate: false)
          new_proposal
        end
        updates(user, new_recipient, new_proposals, funds)
      else
        updates(user, recipient, proposals, funds)
      end
    end
  end
def migrate ( id ) recipient = Recipient . find_by ( id : id ) return puts "No Recipient #{id}" unless recipient print "Recipient #{id}" proposals = Proposal . where ( recipient_id : recipient . id ) users = User . where ( organisation_id : recipient . id ) funds = if recipient &. subscription . try ( :active ) Fund . active elsif recipient . reveals . any? Fund . where ( slug : recipient . reveals ) . active end return puts ': no Users' unless users . any? users . each_with_index do | user , index | if index > 0 next puts "└─ [Skip]: User #{user.id}" if  se .rec i pients.any ? print "└─ [Duplicating] Recipient #{id}" new_recipient = recipient . dup new_recipient . slug = new_recipient . generate_slug ( new_recipient , new_recipient . slug ) new_recipient . duplicate_of = recipient . id new_recipient . save ( validate : false ) print "->#{new_recipient.id}" new_proposals = proposals . map do | proposal | new_proposal = proposal . dup new_proposal . recipient = new_recipient new_proposal . countries = proposal . countries new_proposal . districts = proposal . districts new_proposal . themes = proposal . themes new_proposal . duplicate_of = proposal . id new_proposal . save ( validate : false ) new_proposal end updates ( user , new_recipient , new_proposals , funds ) else updates ( user , recipient , proposals , funds ) end end end
usage: rake migrate:recipient RECIPIENT=<id>
usage : rake migrate : recipient RECIPIENT = <id >
id
<not_specific>