Skip to content

Instantly share code, notes, and snippets.

@fmundaca
Created October 26, 2021 20:16
Show Gist options
  • Save fmundaca/5912bec0dff1db58c64087a762fad2d5 to your computer and use it in GitHub Desktop.
Save fmundaca/5912bec0dff1db58c64087a762fad2d5 to your computer and use it in GitHub Desktop.
namespace :db do
namespace :migrate do
task tenant: :environment do
tenant_name = ENV["TENANT"]
origin_org_slug = ENV["ORIGIN_ORG_SLUG"]
destination_org_slug = ENV["DESTINATION_ORG_SLUG"]
tenant = Tenant.find_by(name: tenant_name)
base_organization = Organization.find_by(slug: origin_org_slug)
tables = ["selection_processes","applicants","organization_clients","organization_client_areas","organization_client_contacts","boards","lists","logs", "educations", "uniq_applicants", "work_experiences"]
tables.each{|table| ActiveRecord::Base.connection.execute("alter sequence \"#{tenant.scheme}\".#{table}_id_seq restart with 51000")}
if ENV['CLEAR_TABLES'] == 'true'
Apartment::Tenant.switch tenant.scheme do
destination_org = Organization.find_by(slug: destination_org_slug)
lists = List.joins(board: :selection_process).where(selection_processes: {organization_id: destination_org.id})
applicants = Applicant.where(list_id: lists.pluck(:id))
ActivityUser.where(id: Log.where(applicant_id: applicants.pluck(:id)).pluck(:logging_id)).destroy_all
ActivityList.where(id: Log.where(applicant_id: applicants.pluck(:id)).pluck(:logging_id)).destroy_all
Log.where(applicant_id: applicants.pluck(:id)).destroy_all
applicants.destroy_all
destination_org.applicants.destroy_all
lists.destroy_all
Board.joins(:selection_process).where(selection_processes: {organization_id: destination_org.id}).destroy_all
destination_org.selection_processes.destroy_all
OrganizationClientContact.joins(organization_client_area: :organization_client).where(organization_clients: {organization_id: destination_org.id}).destroy_all
OrganizationClientArea.joins(:organization_client).where(organization_clients: {organization_id: destination_org.id}).destroy_all
destination_org.organization_clients.destroy_all
end
end
selection_processes = base_organization.selection_processes
organization_clients = base_organization.organization_clients
organization_clients.each do |org_client|
org_client_areas = org_client.organization_client_areas.map{ |e| JSON.load(e.to_json).symbolize_keys }
org_client_data = JSON.load(org_client.to_json).symbolize_keys
Apartment::Tenant.switch tenant.scheme do
destination_org = Organization.find_by slug: destination_org_slug
org_client_data[:organization_id] = destination_org.id
OrganizationClient.find_or_create_by org_client_data
org_client_areas.each{ |oca| OrganizationClientArea.find_or_create_by oca }
end
end
selection_processes.each do |selection_process|
user_email = User.find_by(id: selection_process.user_id)&.email if selection_process.user_id.present?
process_data = JSON.load(selection_process.to_json).symbolize_keys.delete_if{ |k, v| [:open_graph].include? k }
board = selection_process.board
board_data = JSON.load(board.to_json).symbolize_keys.delete_if{ |k,v| [:new_applicant_list_id, :contracted_applicant_list_id].include? k }
visibility_selection_processes_data = []
selection_process.visibility_selection_processes.each do |vsp|
visibility_selection_process = JSON.load(vsp.to_json).symbolize_keys.delete_if { |k, v| [:user_id].include? k }
visibility_selection_process[:user_email] = User.find(vsp.user_id).email
visibility_selection_processes_data << visibility_selection_process
end
lists_data = []
board.lists.each do |list|
applicants = list.applicants
list_data = JSON.load(list.to_json).symbolize_keys.delete_if{ |k,v| v.nil? }
list_data[:applicants] = []
applicants.each do |applicant|
applicant_data = JSON.load(applicant.to_json).symbolize_keys.delete_if{ |k, v| v.blank? or [:uniq_applicant_id].include? k}
applicant_data[:curriculum] = applicant_data[:curriculum]["url"].present? ? applicant_data[:curriculum]["url"].split("/").last : nil
applicant_data[:avatar] = applicant_data[:avatar]["url"].present? ? applicant_data[:avatar]["url"].split("/").last : nil
logs = applicant.logs
applicant_data[:logs] = []
logs.each do |log|
log_data = JSON.load(log.to_json).symbolize_keys
activity = ActivityList.find_by id: log.logging_id if log.logging_type.eql? "ActivityList"
activity = ActivityUser.find_by id: log.logging_id if log.logging_type.eql? "ActivityUser"
log_data[:activity] = JSON.load(activity.to_json).symbolize_keys rescue nil
applicant_data[:logs] << log_data
end
applicant_data[:work_experiences] = applicant.work_experiences.map{|we| JSON.load(we.to_json).symbolize_keys.delete_if{ |k,v| v.blank? }}
list_data[:applicants] << applicant_data
end
lists_data << list_data
end
org_client_contact = OrganizationClientContact.find_by id: selection_process.organization_client_contact_id
org_client_contact_data = org_client_contact.present? ? JSON.load(org_client_contact.to_json).symbolize_keys : nil
Apartment::Tenant.switch tenant.scheme do
destination_org = Organization.find_by(slug: destination_org_slug)
user = User.find_by(email: user_email) || destination_org.users.first # si no existe el usuario se asigna al primero en la base
migrated_org_client_contact = OrganizationClientContact.find_or_create_by(org_client_contact_data) if org_client_contact_data.present?
process_data[:user_id] = user.id
process_data[:organization_id] = destination_org.id
migrated_process = SelectionProcess.find_or_create_by process_data
migrated_process.board.lists.destroy_all # se eliminan las listas creadas de forma automatica
migrated_process.board.destroy # se elimina el tablero creado de forma automatica
migrated_board = Board.find_or_create_by board_data
migrated_board.lists.destroy_all
lists_data.each do |list_data|
applicants_data = list_data[:applicants]
list_data.delete :applicants
migrated_list = List.find_or_create_by list_data
applicants_data.each do |applicant_data|
applicant_data[:first_name] ||= applicant_data[:email]
applicant_data[:last_name] ||= applicant_data[:email]
logs_data = applicant_data[:logs]
applicant_data.delete :logs
work_exp_data = applicant_data[:work_experiences]
applicant_data.delete :work_experiences
migrated_applicant = nil
begin
migrated_applicant = Applicant.create! applicant_data
rescue
puts "error al cargar applicant"
puts "process: #{process_data[:id]} - applicant: #{applicant_data[:id]} - list: #{list_data[:id]}"
end
logs_data.each do |log_data|
activity_data = log_data[:activity]
log_data.delete :activity
migrated_activity = ActivityUser.find_or_create_by(activity_data) if activity_data.present? and log_data[:logging_type].eql? "ActivityUser"
migrated_activity = ActivityList.find_or_create_by(activity_data) if activity_data.present? and log_data[:logging_type].eql? "ActivityList"
migrated_log = Log.find_or_create_by log_data
end
work_exp_data.each do |we_data|
WorkExperience.find_or_create_by we_data
end
if applicant_data[:disqualified_datetime].present?
migrated_applicant.reload
migrated_applicant.disqualified_datetime = applicant_data[:disqualified_datetime]
migrated_applicant.save
end
end
if list_data[:deleted] == true
migrated_list.reload
migrated_list.deleted = list_data[:deleted]
migrated_list.active_applicants = migrated_list.applicants.where(active: true).count
migrated_list.save
end
end
migrated_board = migrated_board.reload
new_applicant_list = migrated_board.lists.find_by(name: "nuevos") || migrated_board.lists.reorder(order: :asc).first
contracted_applicant_list = migrated_board.lists.find_by(name: "contratados") || migrated_board.lists.find_by(name: "oferta") || migrated_board.lists.reorder(order: :asc).last
migrated_board.new_applicant_list_id = new_applicant_list&.id
migrated_board.contracted_applicant_list_id = contracted_applicant_list&.id
migrated_board.save
if process_data[:selection_process_status_id] != 5
migrated_process.reload
migrated_process.selection_process_status_id = process_data[:selection_process_status_id]
migrated_process.save
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment