Skip to content

Commit 7400c1e

Browse files
committed
Begin writing import as rake task
1 parent 11c7585 commit 7400c1e

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@
3939
/node_modules
4040
coverage
4141
.DS_Store
42+
43+
# Ignore import files directory for data from old app
44+
/import_files

Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ gem "thruster", require: false
4141

4242
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
4343
# gem "image_processing", "~> 1.2"
44+
#
45+
# Needed for import process; remove when no longer needed
46+
gem "csv"
47+
48+
# Use RSpec for testing [
4449

4550
group :development, :test do
4651
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ GEM
8989
crass (1.0.6)
9090
cssbundling-rails (1.4.1)
9191
railties (>= 6.0.0)
92+
csv (3.3.2)
9293
date (3.4.1)
9394
debug (1.10.0)
9495
irb (~> 1.10)
@@ -364,6 +365,7 @@ DEPENDENCIES
364365
bootsnap
365366
brakeman
366367
cssbundling-rails
368+
csv
367369
debug
368370
factory_bot_rails
369371
importmap-rails

lib/tasks/import_data.rake

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace :import_data do
2+
# Import data from the old app from CSV files
3+
# Files will be located in the import_files directory
4+
# Files will not be committed to the repository
5+
require "csv"
6+
7+
# This is a starting point for the import process
8+
# We will refactor for elegance
9+
10+
desc "Import Providers and Associate With Regions"
11+
task providers: :environment do
12+
file_path = Rails.root.join("import_files", "Providers.csv")
13+
data = CSV.read(file_path, headers: true)
14+
data.each do |row|
15+
provider = Provider.find_or_create_by!(name: row["Provider_Name"], provider_type: row["Provider_Type"])
16+
region = Region.find_or_create_by!(name: row["region_name"])
17+
# TODO: we need the association table
18+
# Associate the provider with the region if it is not already associated
19+
# unless provider.regions.include?(region)
20+
# provider.regions << region
21+
# end
22+
puts "Provider #{provider.name} associated with region #{region.name}"
23+
24+
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)
25+
# TODO: add provider_id once that association is in place
26+
puts "User #{user.email_address} created"
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)