Update seeder to use ActiveRecord imports

pull/1/head
rebuilt 3 years ago
parent 124d35d8db
commit 99c6c5bd90

@ -8,13 +8,18 @@ class Seeder
end end
def seed_academic_years(*academic_year_ranges) def seed_academic_years(*academic_year_ranges)
academic_years = []
academic_year_ranges.each do |range| academic_year_ranges.each do |range|
AcademicYear.find_or_create_by! range: academic_year = AcademicYear.find_or_initialize_by(range:)
academic_years << academic_year
end end
AcademicYear.import academic_years, on_duplicate_key_update: :all
end end
def seed_districts_and_schools(csv_file) def seed_districts_and_schools(csv_file)
dese_ids = [] dese_ids = []
schools = []
CSV.parse(File.read(csv_file), headers: true) do |row| CSV.parse(File.read(csv_file), headers: true) do |row|
district_name = row['District'].strip district_name = row['District'].strip
next if rules.any? do |rule| next if rules.any? do |rule|
@ -29,21 +34,26 @@ class Seeder
hs = row['HS?'] hs = row['HS?']
district = District.find_or_create_by! name: district_name district = District.find_or_create_by! name: district_name
district.update! slug: district_name.parameterize, qualtrics_code: district_code district.slug = district_name.parameterize
district.qualtrics_code = district_code
district.save
school = School.find_or_initialize_by(dese_id:, district:) school = School.find_or_initialize_by(dese_id:, district:)
school.name = school_name school.name = school_name
school.qualtrics_code = school_code school.qualtrics_code = school_code
school.is_hs = marked? hs school.is_hs = marked? hs
school.save! schools << school
end end
School.import schools, on_duplicate_key_update: :all
Respondent.joins(:school).where.not("school.dese_id": dese_ids).destroy_all Respondent.joins(:school).where.not("school.dese_id": dese_ids).destroy_all
Survey.joins(:school).where.not("school.dese_id": dese_ids).destroy_all Survey.joins(:school).where.not("school.dese_id": dese_ids).destroy_all
School.where.not(dese_id: dese_ids).destroy_all School.where.not(dese_id: dese_ids).destroy_all
end end
def seed_surveys(csv_file) def seed_surveys(csv_file)
surveys = []
CSV.parse(File.read(csv_file), headers: true) do |row| CSV.parse(File.read(csv_file), headers: true) do |row|
district_name = row['District'].strip district_name = row['District'].strip
next if rules.any? do |rule| next if rules.any? do |rule|
@ -59,9 +69,11 @@ class Seeder
survey = Survey.find_or_initialize_by(school:, academic_year:) survey = Survey.find_or_initialize_by(school:, academic_year:)
is_short_form_school = marked?(short_form) is_short_form_school = marked?(short_form)
survey.form = is_short_form_school ? Survey.forms[:short] : Survey.forms[:normal] survey.form = is_short_form_school ? Survey.forms[:short] : Survey.forms[:normal]
survey.save! surveys << survey
end end
end end
Survey.import surveys, on_duplicate_key_update: :all
end end
def seed_sqm_framework(csv_file) def seed_sqm_framework(csv_file)

Loading…
Cancel
Save