Skip to content

Commit 091bb2b

Browse files
authored
Merge pull request #482 from Jozian/data-migration-gbv-calculated-fields
DAO: Data Migration for GBV Calculated Fields
2 parents 4acd27a + 6ea19ac commit 091bb2b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

db/data_migration/v2.11.1/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Migrations in 2.11.1
2+
3+
This migration updates the configuration of GBV instances *only*. This migrates the fields that calculate average scores on subforms to new versions which support decimal places.
4+
5+
# Verification of data to be updated
6+
7+
These scripts will update the `Field` records where calculations exist.
8+
9+
To validate which fields will be updated, run the following:
10+
11+
```bash
12+
rails r ./db/migrations/v2.11.1/update_calculated_avg_fields_gbv.rb
13+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
save_records = ARGV[0] == 'true'
4+
5+
def fields_with_old_avg_calculations
6+
return unless PrimeroModule.exists? unique_id: PrimeroModule::GBV
7+
8+
avg_fields = Field.where("calculation -> 'expression' ? 'avg'")
9+
# The new type of calculated average field has the avg object as a hash, not an array.
10+
avg_fields.filter { |f| f.calculation.dig('expression', 'avg').is_a?(Array) }
11+
end
12+
13+
def migrate_field(orig_field, save)
14+
old_avg_data = orig_field.calculation.dig('expression', 'avg')
15+
new_field = orig_field.dup
16+
new_field.calculation = { type: 'number', expression: { avg: { data: old_avg_data, extra: { decimalPlaces: 2 } } } }
17+
new_field.disabled = true
18+
new_field.type = 'calculated'
19+
if save
20+
# We because the type of a field cannot be changed, we need to destroy and recreate
21+
orig_field.destroy!
22+
new_field.save!
23+
puts "Updated field #{new_field.name}"
24+
else
25+
puts "Would update #{new_field.name} to the following"
26+
puts new_field.inspect
27+
end
28+
end
29+
30+
fields_with_old_avg_calculations.each { |f| migrate_field(f, save_records) }

0 commit comments

Comments
 (0)