Skip to content

Commit

Permalink
modify code to archive only approved institution and related data (#1199
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nfstern02 authored Sep 10, 2024
1 parent 1cfcba4 commit bb961a0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/models/archiver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,16 @@ def self.create_archives(source, archive, previous_version, production_version)
insert_cols = (cols.map { |col| '"' + col + '"' }).join(', ')
select_cols = (cols.map { |col| 's.' + col }).join(', ')

# Only archive approved institutions and related data.
str = "INSERT INTO #{archive.table_name}(#{insert_cols}) SELECT #{select_cols} FROM #{source.table_name} s "
str += source.has_attribute?(:institution_id) ? 'JOIN institutions i ON s.institution_id = i.id JOIN versions v ON i.version_id = v.id' : 'JOIN versions v ON s.version_id = v.id'
str += if source.has_attribute?(:institution_id)
'JOIN institutions i ON s.institution_id = i.id JOIN versions v ON i.version_id = v.id AND i.approved = true'
elsif source.eql?(Institution)
'JOIN versions v ON s.version_id = v.id AND approved = true'
else
'JOIN versions v ON s.version_id = v.id'
end

str += ' WHERE v.number >= ? AND v.number < ?;'
sql = archive.send(:sanitize_sql_for_conditions, [str, previous_version, production_version])
ApplicationRecord.connection.execute(sql)
Expand Down
40 changes: 40 additions & 0 deletions spec/models/archiver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,44 @@
end
end
end

# only approved institutions and related data from the previous version are archived. Zip code rates are not tied to
# institutions, but rather to versions. They are archived. The count changes in the spec reflect this expectation.
# Caution flags are not archived.
context 'when archiving' do
before do
prev_vsn = create(:version, :production)
prev_appr_ins = create(:institution, version: prev_vsn)
create(:institution_program, institution: prev_appr_ins)
create(:versioned_school_certifying_official, institution: prev_appr_ins)
create(:zipcode_rate, version: prev_vsn)

prev_unapproved = create(:institution, version: prev_vsn, approved: false)
create(:institution_program, institution: prev_unapproved)
create(:versioned_school_certifying_official, institution: prev_unapproved)

curr_vsn = create(:version, :production)
curr_appr_ins = create(:institution, version: curr_vsn)
create(:institution_program, institution: curr_appr_ins)
create(:versioned_school_certifying_official, institution: curr_appr_ins)
create(:zipcode_rate, version: curr_vsn)

curr_unaaproved = create(:institution, version: curr_vsn, approved: false)
create(:institution_program, institution: curr_unaaproved)
create(:versioned_school_certifying_official, institution: curr_unaaproved)
end

it 'archives only approved institutions & related data & deletes all archivable data from the previous version' do
expect do
Archiver.archive_previous_versions
end.to change(InstitutionsArchive, :count).by(1)
.and change(InstitutionProgramsArchive, :count).by(1)
.and change(VersionedSchoolCertifyingOfficialsArchive, :count).by(1)
.and change(ZipcodeRatesArchive, :count).by(1)
.and change(Institution, :count).by(-2)
.and change(InstitutionProgram, :count).by(-2)
.and change(VersionedSchoolCertifyingOfficial, :count).by(-2)
.and change(ZipcodeRate, :count).by(-1)
end
end
end

0 comments on commit bb961a0

Please sign in to comment.