Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dcollie2 committed Jan 20, 2025
1 parent cf628a6 commit 89da511
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/tasks/import_data.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ namespace :import_data do

# This is a starting point for the import process
# We will refactor for elegance
# It is not idempotent; it presumes a clean database except for seeds

desc "All"
task all: [ :regions, :providers, :topics ]

desc "Import Providers and Associate With Regions"
task providers: :environment do
file_path = Rails.root.join("import_files", "Providers.csv")
data = CSV.read(file_path, headers: true)
data.each do |row|
# TODO: add old_provider_id to Provider model
provider = Provider.find_or_create_by!(name: row["Provider_Name"], provider_type: row["Provider_Type"])
region = Region.find_or_create_by!(name: row["region_name"])
# TODO: we need the association table
Expand All @@ -22,7 +27,7 @@ namespace :import_data do
puts "Provider #{provider.name} associated with region #{region.name}"

user = User.find_or_create_by!(email_address: "#{row["Provider_Name"].underscore.downcase}@update.me", password_digest: BCrypt::Password.create(row["Provider_Password"]), is_admin: false)
# TODO: add provider_id once that association is in place
user.update(provider_id: provider.id)
puts "User #{user.email_address} created"
end
end
Expand All @@ -32,11 +37,18 @@ namespace :import_data do
file_path = Rails.root.join("import_files", "Topics.csv")
data = CSV.read(file_path, headers: true)
data.each do |row|
topic = Topic.find_or_create_by!(name: row["Topic_Name"])
provider = Provider.find_by(name: row["Provider_Name"])
topic.providers << provider
puts "Topic #{topic.name} associated with provider #{provider.name}"
# look up provider by old_provider_id
provider = Provider.find_by(old_provider_id: row["Provider_ID"])
# Language IDs should correspond to the IDs in old app
# TODO: verify before final import
topic = Topic.find_or_create_by!(name: row["Topic_Name"], provider_id: provider.id, description: row["Topic_Desc"],
language_id: row["Language_ID"], uid: row["Topic_UID"], state: determine_state(row["Topic_Archived"], old_topic_id: row["topic_id"]))
puts "Topic #{topic.name} created"
end

end

def determine_state(archived)
# TODO: validate against state enumerable
archived ? "archived" : "published"
end
end

0 comments on commit 89da511

Please sign in to comment.