From b28412fdd1d9bddd5c3e6abe31c8c4ba22483de1 Mon Sep 17 00:00:00 2001 From: Danny Collier <294724+dcollie2@users.noreply.github.com> Date: Fri, 17 Jan 2025 15:38:06 -0500 Subject: [PATCH 1/3] Begin writing import as rake task --- .gitignore | 3 +++ Gemfile | 5 +++++ Gemfile.lock | 2 ++ lib/tasks/import_data.rake | 29 +++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 lib/tasks/import_data.rake diff --git a/.gitignore b/.gitignore index 7876c16..ea28e1f 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,6 @@ /node_modules coverage .DS_Store + +# Ignore import files directory for data from old app +/import_files diff --git a/Gemfile b/Gemfile index bfe38f3..85d01de 100644 --- a/Gemfile +++ b/Gemfile @@ -41,6 +41,11 @@ gem "thruster", require: false # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] # gem "image_processing", "~> 1.2" +# +# Needed for import process; remove when no longer needed +gem "csv" + +# Use RSpec for testing [ group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem diff --git a/Gemfile.lock b/Gemfile.lock index d557e55..9192a7f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -89,6 +89,7 @@ GEM crass (1.0.6) cssbundling-rails (1.4.1) railties (>= 6.0.0) + csv (3.3.2) date (3.4.1) debug (1.10.0) irb (~> 1.10) @@ -364,6 +365,7 @@ DEPENDENCIES bootsnap brakeman cssbundling-rails + csv debug factory_bot_rails importmap-rails diff --git a/lib/tasks/import_data.rake b/lib/tasks/import_data.rake new file mode 100644 index 0000000..4f15c25 --- /dev/null +++ b/lib/tasks/import_data.rake @@ -0,0 +1,29 @@ +namespace :import_data do + # Import data from the old app from CSV files + # Files will be located in the import_files directory + # Files will not be committed to the repository + require "csv" + + # This is a starting point for the import process + # We will refactor for elegance + + 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| + 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 + # Associate the provider with the region if it is not already associated + # unless provider.regions.include?(region) + # provider.regions << region + # end + 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 + puts "User #{user.email_address} created" + end + end +end From 70e7024843e24523c6ebed03593e3223f51808bc Mon Sep 17 00:00:00 2001 From: Danny Collier <294724+dcollie2@users.noreply.github.com> Date: Sat, 18 Jan 2025 14:20:52 -0500 Subject: [PATCH 2/3] WIP --- lib/tasks/import_data.rake | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/tasks/import_data.rake b/lib/tasks/import_data.rake index 4f15c25..f328a5e 100644 --- a/lib/tasks/import_data.rake +++ b/lib/tasks/import_data.rake @@ -26,4 +26,17 @@ namespace :import_data do puts "User #{user.email_address} created" end end + + desc "Import Topics and their associations" + task topics: :environment 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}" + end + + end end From cc772d6087ce409b8c96d9c018e564e71ebeff06 Mon Sep 17 00:00:00 2001 From: Danny Collier <294724+dcollie2@users.noreply.github.com> Date: Sun, 19 Jan 2025 19:51:35 -0500 Subject: [PATCH 3/3] WIP --- lib/tasks/import_data.rake | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/tasks/import_data.rake b/lib/tasks/import_data.rake index f328a5e..fb71773 100644 --- a/lib/tasks/import_data.rake +++ b/lib/tasks/import_data.rake @@ -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 @@ -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 @@ -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