From c8cbcb0f39dec0a935f97c3fd6c19f9c71c284cf Mon Sep 17 00:00:00 2001 From: Richard Lynch Date: Tue, 24 Sep 2024 11:23:42 +0100 Subject: [PATCH] Back fill started at If the claim has a journey session we try and set the `started_at` to that, if it doesn't have a journey session we'll just use the `created_at` timestamp, which for claims pre the introduction of journey sessions will be the started at time. --- ...40924095435_back_fill_claims_started_at.rb | 24 +++++++++++++++++++ db/schema.rb | 4 ++-- spec/factories/claims.rb | 2 ++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20240924095435_back_fill_claims_started_at.rb diff --git a/db/migrate/20240924095435_back_fill_claims_started_at.rb b/db/migrate/20240924095435_back_fill_claims_started_at.rb new file mode 100644 index 0000000000..c0e4d49db2 --- /dev/null +++ b/db/migrate/20240924095435_back_fill_claims_started_at.rb @@ -0,0 +1,24 @@ +class BackFillClaimsStartedAt < ActiveRecord::Migration[7.0] + def up + execute <<-SQL + UPDATE claims + SET started_at = journeys_sessions.created_at + FROM journeys_sessions + WHERE claims.journeys_session_id = journeys_sessions.id + AND claims.started_at IS NULL + SQL + + execute <<-SQL + UPDATE claims + SET started_at = created_at + WHERE journeys_session_id IS NULL + AND claims.started_at IS NULL + SQL + + change_column_null :claims, :started_at, false + end + + def down + Claim.update_all(started_at: nil) + end +end diff --git a/db/schema.rb b/db/schema.rb index e7efeab458..3e6fffa118 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_09_24_091408) do +ActiveRecord::Schema[7.0].define(version: 2024_09_24_095435) do # These are extensions that must be enabled in order to support this database enable_extension "citext" enable_extension "pg_trgm" @@ -111,7 +111,7 @@ t.text "onelogin_idv_first_name" t.text "onelogin_idv_last_name" t.date "onelogin_idv_date_of_birth" - t.datetime "started_at", precision: nil + t.datetime "started_at", precision: nil, null: false t.index ["academic_year"], name: "index_claims_on_academic_year" t.index ["created_at"], name: "index_claims_on_created_at" t.index ["eligibility_type", "eligibility_id"], name: "index_claims_on_eligibility_type_and_eligibility_id" diff --git a/spec/factories/claims.rb b/spec/factories/claims.rb index becd4f94d1..7e2f07646d 100644 --- a/spec/factories/claims.rb +++ b/spec/factories/claims.rb @@ -4,6 +4,8 @@ sequence(:national_insurance_number, 100000) { |n| "QQ#{n}C" } factory :claim do + started_at { Time.zone.now } + transient do policy { Policies::StudentLoans } eligibility_factory { :"#{policy.to_s.underscore}_eligibility" }