Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add regional partner export script #20900

Merged
merged 1 commit into from
Feb 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
168 changes: 168 additions & 0 deletions bin/oneoff/export_regional_partner_data
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#!/usr/bin/env ruby

# Exports regional partner data to json, and imports from json.
# This is to set up an adhoc server with exported data from production

require 'active_support/core_ext/object/try'

# Parse options before requiring the rails env for speed
@action = ARGV.shift.try(:downcase)
unless %w(import export).include? @action
puts "Usage: #{__FILE__} export|import [file]"
exit
end
@filename = ARGV.shift || 'regional_partner_data.json'

puts "#{@action}ing #{@action == 'export' ? 'to' : 'from'} #{File.absolute_path(@filename)}:"

puts
puts "Loading Rails environment (this may take some time)..."
require_relative '../../dashboard/config/environment'
puts " Loaded."
puts

raise "Import not allowed on production" if Rails.env.production? && @action == 'import'

# Explicitly reference these models so they are autoloaded and we can monkey patch below.
# Otherwise they won't yet be autoloaded, and the monkey patch would create a new class
User.name
RegionalPartner.name
Pd::RegionalPartnerMapping.name
Pd::CourseFacilitator.name

class User
ATTRIBUTES = %w(
name
email
)
def export
attributes.slice(*ATTRIBUTES)
end

def self.import(hash)
User.find_by(email: hash['email']) || User.create!(
hash.slice(*ATTRIBUTES).merge(
user_type: 'teacher',
password: 'password',
age: 21
)
)
end

def self.import_program_manager(hash)
import(hash).tap do |user|
user.permission = UserPermission::WORKSHOP_ORGANIZER
end
end

def self.import_facilitator(hash)
import(hash).tap do |user|
user.permission = UserPermission::FACILITATOR
end
end
end

class RegionalPartner
ATTRIBUTES = %w(
name
group
contact_id
urban
attention
street
apartment_or_suite
city
state
zip_code
phone_number
notes
)

def export
attributes.slice(*ATTRIBUTES).merge(
program_managers: program_managers.map(&:export),
mappings: mappings.map(&:export)
).compact.deep_stringify_keys
end

def self.import(hash)
RegionalPartner.find_or_create_by!(hash.slice(*ATTRIBUTES)).tap do |regional_partner|
hash['program_managers'].each do |program_manager_hash|
regional_partner.program_managers.push User.import_program_manager(program_manager_hash)
end

hash['mappings'].each do |mapping_hash|
Pd::RegionalPartnerMapping.import(mapping_hash.merge(regional_partner_id: regional_partner.id))
end
end
end
end

class Pd::RegionalPartnerMapping
ATTRIBUTES = %w(
state
zip_code
)

def export
attributes.slice(*ATTRIBUTES)
end

def self.import(hash)
Pd::RegionalPartnerMapping.find_or_create_by!(hash)
end
end

class Pd::CourseFacilitator
def export
{
course: course,
facilitator: facilitator.export
}
end

def self.import(hash)
Pd::CourseFacilitator.create!(
course: hash['course'],
facilitator: User.import_facilitator(hash['facilitator'])
)
end
end

def export_to(filename)
json = {
regional_partners: RegionalPartner.all.map(&:export),
facilitators: Pd::CourseFacilitator.all.map(&:export)
}

File.open(filename, 'w') do |file|
file.puts JSON.pretty_generate(json)
end

puts "Exported #{RegionalPartner.count} regional partners "\
"and #{Pd::CourseFacilitator.count} facilitators to #{filename}"
end

def import_from(filename)
File.open(filename) do |file|
hash = JSON.parse file.read
hash['regional_partners'].each do |regional_partner_hash|
RegionalPartner.import regional_partner_hash
end
hash['facilitators'].each do |facilitator_hash|
Pd::CourseFacilitator.import facilitator_hash
end

puts "Imported #{hash['regional_partners'].count} regional partners "\
"and #{hash['facilitators'].count} facilitators from #{filename}"
end
end

case @action
when 'export'
export_to @filename
when 'import'
import_from @filename
else
raise "Unexpected action #{@action}"
end