From bb0b1ded718684da6b56bf096a5d104965d91dfa Mon Sep 17 00:00:00 2001 From: Awen Saunders Date: Mon, 25 Nov 2024 17:52:07 +0000 Subject: [PATCH 1/4] Add data migration for gbv decimalPlaces forms --- db/data_migration/v2.12/README.md | 13 +++++++++ .../v2.12/update_calculated_avg_fields_gbv.rb | 27 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 db/data_migration/v2.12/README.md create mode 100644 db/data_migration/v2.12/update_calculated_avg_fields_gbv.rb diff --git a/db/data_migration/v2.12/README.md b/db/data_migration/v2.12/README.md new file mode 100644 index 0000000000..29a91bfd41 --- /dev/null +++ b/db/data_migration/v2.12/README.md @@ -0,0 +1,13 @@ +# Migrations in 2.12 + +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. + +# Verification of data to be updated + +These scripts will update the `Field` records where calculations exist. + +To validate which fields will be updated, run the following: + +```bash +rails r ./db/migrations/v2.12/update_calculated_avg_fields_gbv.rb +``` \ No newline at end of file diff --git a/db/data_migration/v2.12/update_calculated_avg_fields_gbv.rb b/db/data_migration/v2.12/update_calculated_avg_fields_gbv.rb new file mode 100644 index 0000000000..3413f66850 --- /dev/null +++ b/db/data_migration/v2.12/update_calculated_avg_fields_gbv.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +save_records = ARGV[0] == 'true' + +def fields_with_old_avg_calculations + return unless PrimeroModule.exists? unique_id: PrimeroModule::GBV + + avg_fields = Field.where("calculation -> 'expression' ? 'avg'") + # The new type of calculated average field has the avg object as a hash, not an array. + avg_fields.filter { |f| f.calculation.dig('expression', 'avg').is_a?(Array) } +end + +def migrate_field(field, save) + old_avg_data = field.calculation.dig('expression', 'avg') + field.calculation = { type: 'number', expression: { avg: { data: old_avg_data, extra: { decimalPlaces: 2 } } } } + field.disabled = true + field.type = 'calculated' + if save + field.save! + puts "Updated field #{field.name}" + else + puts "Would update #{field.name} to the following" + puts field.inspect + end +end + +fields_with_old_avg_calculations.each { |f| migrate_field(f, save_records) } From aaba8524c61616d07ac7d65137965897d83926cd Mon Sep 17 00:00:00 2001 From: Awen Saunders Date: Mon, 25 Nov 2024 18:11:04 +0000 Subject: [PATCH 2/4] Update data migration to destroy and recreate relevant field --- .../v2.12/update_calculated_avg_fields_gbv.rb | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/db/data_migration/v2.12/update_calculated_avg_fields_gbv.rb b/db/data_migration/v2.12/update_calculated_avg_fields_gbv.rb index 3413f66850..167c895dad 100644 --- a/db/data_migration/v2.12/update_calculated_avg_fields_gbv.rb +++ b/db/data_migration/v2.12/update_calculated_avg_fields_gbv.rb @@ -10,17 +10,20 @@ def fields_with_old_avg_calculations avg_fields.filter { |f| f.calculation.dig('expression', 'avg').is_a?(Array) } end -def migrate_field(field, save) - old_avg_data = field.calculation.dig('expression', 'avg') - field.calculation = { type: 'number', expression: { avg: { data: old_avg_data, extra: { decimalPlaces: 2 } } } } - field.disabled = true - field.type = 'calculated' +def migrate_field(orig_field, save) + old_avg_data = orig_field.calculation.dig('expression', 'avg') + new_field = orig_field.dup + new_field.calculation = { type: 'number', expression: { avg: { data: old_avg_data, extra: { decimalPlaces: 2 } } } } + new_field.disabled = true + new_field.type = 'calculated' if save - field.save! - puts "Updated field #{field.name}" + # We because the type of a field cannot be changed, we need to destroy and recreate + orig_field.destroy! + new_field.save! + puts "Updated field #{new_field.name}" else - puts "Would update #{field.name} to the following" - puts field.inspect + puts "Would update #{new_field.name} to the following" + puts new_field.inspect end end From 4b095caeb03779aacfc65a73e99dace12ca0ba9a Mon Sep 17 00:00:00 2001 From: Awen Saunders Date: Wed, 4 Dec 2024 16:22:15 +0000 Subject: [PATCH 3/4] Move from v2.12 to v2.11.1 for data migration --- db/data_migration/{v2.12 => v2.11.1}/README.md | 0 .../{v2.12 => v2.11.1}/update_calculated_avg_fields_gbv.rb | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename db/data_migration/{v2.12 => v2.11.1}/README.md (100%) rename db/data_migration/{v2.12 => v2.11.1}/update_calculated_avg_fields_gbv.rb (100%) diff --git a/db/data_migration/v2.12/README.md b/db/data_migration/v2.11.1/README.md similarity index 100% rename from db/data_migration/v2.12/README.md rename to db/data_migration/v2.11.1/README.md diff --git a/db/data_migration/v2.12/update_calculated_avg_fields_gbv.rb b/db/data_migration/v2.11.1/update_calculated_avg_fields_gbv.rb similarity index 100% rename from db/data_migration/v2.12/update_calculated_avg_fields_gbv.rb rename to db/data_migration/v2.11.1/update_calculated_avg_fields_gbv.rb From 6ea19ac96d3f4cce652c71ed96a7c70f9f2a5c25 Mon Sep 17 00:00:00 2001 From: Awen Saunders Date: Wed, 4 Dec 2024 16:22:55 +0000 Subject: [PATCH 4/4] Update references in readme to point to new location --- db/data_migration/v2.11.1/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/data_migration/v2.11.1/README.md b/db/data_migration/v2.11.1/README.md index 29a91bfd41..f4c66eb9d8 100644 --- a/db/data_migration/v2.11.1/README.md +++ b/db/data_migration/v2.11.1/README.md @@ -1,4 +1,4 @@ -# Migrations in 2.12 +# Migrations in 2.11.1 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. @@ -9,5 +9,5 @@ These scripts will update the `Field` records where calculations exist. To validate which fields will be updated, run the following: ```bash -rails r ./db/migrations/v2.12/update_calculated_avg_fields_gbv.rb +rails r ./db/migrations/v2.11.1/update_calculated_avg_fields_gbv.rb ``` \ No newline at end of file