Skip to content

Commit 75a8e43

Browse files
authored
Merge pull request #4635 from nhsuk/pending-changes-index
Add index on `pending_changes <> '{}'`
2 parents a1e6153 + 39a1ffa commit 75a8e43

9 files changed

Lines changed: 82 additions & 58 deletions

app/helpers/imports_helper.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ module ImportsHelper
1414

1515
def import_issues_count
1616
vaccination_records_with_issues =
17-
policy_scope(VaccinationRecord).with_pending_changes.distinct.pluck(
18-
:patient_id
19-
)
17+
policy_scope(VaccinationRecord).with_pending_changes.pluck(:patient_id)
2018

2119
patients_with_issues = policy_scope(Patient).with_pending_changes.pluck(:id)
2220

app/models/patient.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
#
3434
# Indexes
3535
#
36-
# index_patients_on_family_name_trigram (family_name) USING gin
37-
# index_patients_on_given_name_trigram (given_name) USING gin
38-
# index_patients_on_gp_practice_id (gp_practice_id)
39-
# index_patients_on_names_family_first (family_name,given_name)
40-
# index_patients_on_names_given_first (given_name,family_name)
41-
# index_patients_on_nhs_number (nhs_number) UNIQUE
42-
# index_patients_on_school_id (school_id)
36+
# index_patients_on_family_name_trigram (family_name) USING gin
37+
# index_patients_on_given_name_trigram (given_name) USING gin
38+
# index_patients_on_gp_practice_id (gp_practice_id)
39+
# index_patients_on_names_family_first (family_name,given_name)
40+
# index_patients_on_names_given_first (given_name,family_name)
41+
# index_patients_on_nhs_number (nhs_number) UNIQUE
42+
# index_patients_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb)
43+
# index_patients_on_school_id (school_id)
4344
#
4445
# Foreign Keys
4546
#

app/models/vaccination_record.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@
4040
#
4141
# Indexes
4242
#
43-
# index_vaccination_records_on_batch_id (batch_id)
44-
# index_vaccination_records_on_discarded_at (discarded_at)
45-
# index_vaccination_records_on_location_id (location_id)
46-
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
47-
# index_vaccination_records_on_patient_id (patient_id)
48-
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
49-
# index_vaccination_records_on_programme_id (programme_id)
50-
# index_vaccination_records_on_session_id (session_id)
51-
# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id)
52-
# index_vaccination_records_on_uuid (uuid) UNIQUE
53-
# index_vaccination_records_on_vaccine_id (vaccine_id)
43+
# index_vaccination_records_on_batch_id (batch_id)
44+
# index_vaccination_records_on_discarded_at (discarded_at)
45+
# index_vaccination_records_on_location_id (location_id)
46+
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
47+
# index_vaccination_records_on_patient_id (patient_id)
48+
# index_vaccination_records_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb)
49+
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
50+
# index_vaccination_records_on_programme_id (programme_id)
51+
# index_vaccination_records_on_session_id (session_id)
52+
# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id)
53+
# index_vaccination_records_on_uuid (uuid) UNIQUE
54+
# index_vaccination_records_on_vaccine_id (vaccine_id)
5455
#
5556
# Foreign Keys
5657
#
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
class AddIndexOnPendingChangesNotEmpty < ActiveRecord::Migration[8.0]
4+
disable_ddl_transaction!
5+
6+
def change
7+
add_index :patients,
8+
:id,
9+
where: "pending_changes <> '{}'",
10+
algorithm: :concurrently,
11+
name: "index_patients_on_pending_changes_not_empty"
12+
add_index :vaccination_records,
13+
:id,
14+
where: "pending_changes <> '{}'",
15+
algorithm: :concurrently,
16+
name: "index_vaccination_records_on_pending_changes_not_empty"
17+
end
18+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2025_09_12_134432) do
13+
ActiveRecord::Schema[8.0].define(version: 2025_09_16_074716) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "pg_catalog.plpgsql"
1616
enable_extension "pg_trgm"
@@ -627,6 +627,7 @@
627627
t.index ["given_name", "family_name"], name: "index_patients_on_names_given_first"
628628
t.index ["given_name"], name: "index_patients_on_given_name_trigram", opclass: :gin_trgm_ops, using: :gin
629629
t.index ["gp_practice_id"], name: "index_patients_on_gp_practice_id"
630+
t.index ["id"], name: "index_patients_on_pending_changes_not_empty", where: "(pending_changes <> '{}'::jsonb)"
630631
t.index ["nhs_number"], name: "index_patients_on_nhs_number", unique: true
631632
t.index ["school_id"], name: "index_patients_on_school_id"
632633
end
@@ -925,6 +926,7 @@
925926
t.integer "source", null: false
926927
t.index ["batch_id"], name: "index_vaccination_records_on_batch_id"
927928
t.index ["discarded_at"], name: "index_vaccination_records_on_discarded_at"
929+
t.index ["id"], name: "index_vaccination_records_on_pending_changes_not_empty", where: "(pending_changes <> '{}'::jsonb)"
928930
t.index ["location_id"], name: "index_vaccination_records_on_location_id"
929931
t.index ["nhs_immunisations_api_id"], name: "index_vaccination_records_on_nhs_immunisations_api_id", unique: true
930932
t.index ["patient_id"], name: "index_vaccination_records_on_patient_id"

spec/factories/patients.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
#
3434
# Indexes
3535
#
36-
# index_patients_on_family_name_trigram (family_name) USING gin
37-
# index_patients_on_given_name_trigram (given_name) USING gin
38-
# index_patients_on_gp_practice_id (gp_practice_id)
39-
# index_patients_on_names_family_first (family_name,given_name)
40-
# index_patients_on_names_given_first (given_name,family_name)
41-
# index_patients_on_nhs_number (nhs_number) UNIQUE
42-
# index_patients_on_school_id (school_id)
36+
# index_patients_on_family_name_trigram (family_name) USING gin
37+
# index_patients_on_given_name_trigram (given_name) USING gin
38+
# index_patients_on_gp_practice_id (gp_practice_id)
39+
# index_patients_on_names_family_first (family_name,given_name)
40+
# index_patients_on_names_given_first (given_name,family_name)
41+
# index_patients_on_nhs_number (nhs_number) UNIQUE
42+
# index_patients_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb)
43+
# index_patients_on_school_id (school_id)
4344
#
4445
# Foreign Keys
4546
#

spec/factories/vaccination_records.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@
4040
#
4141
# Indexes
4242
#
43-
# index_vaccination_records_on_batch_id (batch_id)
44-
# index_vaccination_records_on_discarded_at (discarded_at)
45-
# index_vaccination_records_on_location_id (location_id)
46-
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
47-
# index_vaccination_records_on_patient_id (patient_id)
48-
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
49-
# index_vaccination_records_on_programme_id (programme_id)
50-
# index_vaccination_records_on_session_id (session_id)
51-
# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id)
52-
# index_vaccination_records_on_uuid (uuid) UNIQUE
53-
# index_vaccination_records_on_vaccine_id (vaccine_id)
43+
# index_vaccination_records_on_batch_id (batch_id)
44+
# index_vaccination_records_on_discarded_at (discarded_at)
45+
# index_vaccination_records_on_location_id (location_id)
46+
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
47+
# index_vaccination_records_on_patient_id (patient_id)
48+
# index_vaccination_records_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb)
49+
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
50+
# index_vaccination_records_on_programme_id (programme_id)
51+
# index_vaccination_records_on_session_id (session_id)
52+
# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id)
53+
# index_vaccination_records_on_uuid (uuid) UNIQUE
54+
# index_vaccination_records_on_vaccine_id (vaccine_id)
5455
#
5556
# Foreign Keys
5657
#

spec/models/patient_spec.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
#
3434
# Indexes
3535
#
36-
# index_patients_on_family_name_trigram (family_name) USING gin
37-
# index_patients_on_given_name_trigram (given_name) USING gin
38-
# index_patients_on_gp_practice_id (gp_practice_id)
39-
# index_patients_on_names_family_first (family_name,given_name)
40-
# index_patients_on_names_given_first (given_name,family_name)
41-
# index_patients_on_nhs_number (nhs_number) UNIQUE
42-
# index_patients_on_school_id (school_id)
36+
# index_patients_on_family_name_trigram (family_name) USING gin
37+
# index_patients_on_given_name_trigram (given_name) USING gin
38+
# index_patients_on_gp_practice_id (gp_practice_id)
39+
# index_patients_on_names_family_first (family_name,given_name)
40+
# index_patients_on_names_given_first (given_name,family_name)
41+
# index_patients_on_nhs_number (nhs_number) UNIQUE
42+
# index_patients_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb)
43+
# index_patients_on_school_id (school_id)
4344
#
4445
# Foreign Keys
4546
#

spec/models/vaccination_record_spec.rb

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@
4040
#
4141
# Indexes
4242
#
43-
# index_vaccination_records_on_batch_id (batch_id)
44-
# index_vaccination_records_on_discarded_at (discarded_at)
45-
# index_vaccination_records_on_location_id (location_id)
46-
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
47-
# index_vaccination_records_on_patient_id (patient_id)
48-
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
49-
# index_vaccination_records_on_programme_id (programme_id)
50-
# index_vaccination_records_on_session_id (session_id)
51-
# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id)
52-
# index_vaccination_records_on_uuid (uuid) UNIQUE
53-
# index_vaccination_records_on_vaccine_id (vaccine_id)
43+
# index_vaccination_records_on_batch_id (batch_id)
44+
# index_vaccination_records_on_discarded_at (discarded_at)
45+
# index_vaccination_records_on_location_id (location_id)
46+
# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE
47+
# index_vaccination_records_on_patient_id (patient_id)
48+
# index_vaccination_records_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb)
49+
# index_vaccination_records_on_performed_by_user_id (performed_by_user_id)
50+
# index_vaccination_records_on_programme_id (programme_id)
51+
# index_vaccination_records_on_session_id (session_id)
52+
# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id)
53+
# index_vaccination_records_on_uuid (uuid) UNIQUE
54+
# index_vaccination_records_on_vaccine_id (vaccine_id)
5455
#
5556
# Foreign Keys
5657
#

0 commit comments

Comments
 (0)