Skip to content

Begin writing import as rake task #42

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@
/node_modules
coverage
.DS_Store

# Ignore import files directory for data from old app
/import_files
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -364,6 +365,7 @@ DEPENDENCIES
bootsnap
brakeman
cssbundling-rails
csv
debug
factory_bot_rails
importmap-rails
Expand Down
54 changes: 54 additions & 0 deletions lib/tasks/import_data.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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
# 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
# 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)
user.update(provider_id: provider.id)
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|
# 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
Loading