Skip to content

Commit

Permalink
Begin writing import as rake task
Browse files Browse the repository at this point in the history
  • Loading branch information
dcollie2 committed Jan 18, 2025
1 parent 2027990 commit aa0744c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
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
29 changes: 29 additions & 0 deletions lib/tasks/import_data.rake
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit aa0744c

Please sign in to comment.