From 2a83a580c8d46e3bcafc900a166ef8f3a18bb43c Mon Sep 17 00:00:00 2001 From: Jonathan Hooper Date: Mon, 2 Oct 2023 12:52:48 -0400 Subject: [PATCH] Read records in KMS migration job while timeout is in effect Currently the ProfileMigrationJob and UserMigrationJob make expensive queries to find users to migrate. These queries need a large amount of time to run. To enable this we run them in a transaction with a local statement timeout. I discovered an issue with this approach. The query was not actually executed in the transaction block. The transaction returned a relation and the query was executed when we attempted to iterated over the records in the relation. This commit adds a `#to_a` call to the relation to ensure the query is run and the records are loaded into memory before the transaction block is complete. [skip changelog] --- app/jobs/multi_region_kms_migration/profile_migration_job.rb | 2 +- app/jobs/multi_region_kms_migration/user_migration_job.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/jobs/multi_region_kms_migration/profile_migration_job.rb b/app/jobs/multi_region_kms_migration/profile_migration_job.rb index 0e79aa2fedc..c334aced307 100644 --- a/app/jobs/multi_region_kms_migration/profile_migration_job.rb +++ b/app/jobs/multi_region_kms_migration/profile_migration_job.rb @@ -49,7 +49,7 @@ def find_profiles_to_migrate(statement_timeout:, profile_count:) ).where( 'encrypted_pii IS NOT NULL', 'encrypted_pii_recovery IS NOT NULL', - ).limit(profile_count) + ).limit(profile_count).to_a end end diff --git a/app/jobs/multi_region_kms_migration/user_migration_job.rb b/app/jobs/multi_region_kms_migration/user_migration_job.rb index 0a8a0b67452..3ff3c51c99b 100644 --- a/app/jobs/multi_region_kms_migration/user_migration_job.rb +++ b/app/jobs/multi_region_kms_migration/user_migration_job.rb @@ -62,7 +62,7 @@ def find_users_to_migrate(statement_timeout:, user_count:) 'encrypted_recovery_code_digest NOT LIKE ?', '%encryption_key%' ) - password_scope.or(personal_key_scope).limit(user_count) + password_scope.or(personal_key_scope).limit(user_count).to_a end end