diff --git a/app/helpers/imports_helper.rb b/app/helpers/imports_helper.rb index 747ba51d05..a4a9b5f305 100644 --- a/app/helpers/imports_helper.rb +++ b/app/helpers/imports_helper.rb @@ -14,9 +14,7 @@ module ImportsHelper def import_issues_count vaccination_records_with_issues = - policy_scope(VaccinationRecord).with_pending_changes.distinct.pluck( - :patient_id - ) + policy_scope(VaccinationRecord).with_pending_changes.pluck(:patient_id) patients_with_issues = policy_scope(Patient).with_pending_changes.pluck(:id) diff --git a/app/models/patient.rb b/app/models/patient.rb index bf8005fa0b..0e8389dbfd 100644 --- a/app/models/patient.rb +++ b/app/models/patient.rb @@ -33,13 +33,14 @@ # # Indexes # -# index_patients_on_family_name_trigram (family_name) USING gin -# index_patients_on_given_name_trigram (given_name) USING gin -# index_patients_on_gp_practice_id (gp_practice_id) -# index_patients_on_names_family_first (family_name,given_name) -# index_patients_on_names_given_first (given_name,family_name) -# index_patients_on_nhs_number (nhs_number) UNIQUE -# index_patients_on_school_id (school_id) +# index_patients_on_family_name_trigram (family_name) USING gin +# index_patients_on_given_name_trigram (given_name) USING gin +# index_patients_on_gp_practice_id (gp_practice_id) +# index_patients_on_names_family_first (family_name,given_name) +# index_patients_on_names_given_first (given_name,family_name) +# index_patients_on_nhs_number (nhs_number) UNIQUE +# index_patients_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb) +# index_patients_on_school_id (school_id) # # Foreign Keys # diff --git a/app/models/vaccination_record.rb b/app/models/vaccination_record.rb index f6d0774956..a6ed785348 100644 --- a/app/models/vaccination_record.rb +++ b/app/models/vaccination_record.rb @@ -40,17 +40,18 @@ # # Indexes # -# index_vaccination_records_on_batch_id (batch_id) -# index_vaccination_records_on_discarded_at (discarded_at) -# index_vaccination_records_on_location_id (location_id) -# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE -# index_vaccination_records_on_patient_id (patient_id) -# index_vaccination_records_on_performed_by_user_id (performed_by_user_id) -# index_vaccination_records_on_programme_id (programme_id) -# index_vaccination_records_on_session_id (session_id) -# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id) -# index_vaccination_records_on_uuid (uuid) UNIQUE -# index_vaccination_records_on_vaccine_id (vaccine_id) +# index_vaccination_records_on_batch_id (batch_id) +# index_vaccination_records_on_discarded_at (discarded_at) +# index_vaccination_records_on_location_id (location_id) +# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE +# index_vaccination_records_on_patient_id (patient_id) +# index_vaccination_records_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb) +# index_vaccination_records_on_performed_by_user_id (performed_by_user_id) +# index_vaccination_records_on_programme_id (programme_id) +# index_vaccination_records_on_session_id (session_id) +# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id) +# index_vaccination_records_on_uuid (uuid) UNIQUE +# index_vaccination_records_on_vaccine_id (vaccine_id) # # Foreign Keys # diff --git a/db/migrate/20250916074716_add_index_on_pending_changes_not_empty.rb b/db/migrate/20250916074716_add_index_on_pending_changes_not_empty.rb new file mode 100644 index 0000000000..4952652977 --- /dev/null +++ b/db/migrate/20250916074716_add_index_on_pending_changes_not_empty.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class AddIndexOnPendingChangesNotEmpty < ActiveRecord::Migration[8.0] + disable_ddl_transaction! + + def change + add_index :patients, + :id, + where: "pending_changes <> '{}'", + algorithm: :concurrently, + name: "index_patients_on_pending_changes_not_empty" + add_index :vaccination_records, + :id, + where: "pending_changes <> '{}'", + algorithm: :concurrently, + name: "index_vaccination_records_on_pending_changes_not_empty" + end +end diff --git a/db/schema.rb b/db/schema.rb index bf1ee1f52b..5af036ab1e 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[8.0].define(version: 2025_09_12_134432) do +ActiveRecord::Schema[8.0].define(version: 2025_09_16_074716) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" enable_extension "pg_trgm" @@ -627,6 +627,7 @@ t.index ["given_name", "family_name"], name: "index_patients_on_names_given_first" t.index ["given_name"], name: "index_patients_on_given_name_trigram", opclass: :gin_trgm_ops, using: :gin t.index ["gp_practice_id"], name: "index_patients_on_gp_practice_id" + t.index ["id"], name: "index_patients_on_pending_changes_not_empty", where: "(pending_changes <> '{}'::jsonb)" t.index ["nhs_number"], name: "index_patients_on_nhs_number", unique: true t.index ["school_id"], name: "index_patients_on_school_id" end @@ -925,6 +926,7 @@ t.integer "source", null: false t.index ["batch_id"], name: "index_vaccination_records_on_batch_id" t.index ["discarded_at"], name: "index_vaccination_records_on_discarded_at" + t.index ["id"], name: "index_vaccination_records_on_pending_changes_not_empty", where: "(pending_changes <> '{}'::jsonb)" t.index ["location_id"], name: "index_vaccination_records_on_location_id" t.index ["nhs_immunisations_api_id"], name: "index_vaccination_records_on_nhs_immunisations_api_id", unique: true t.index ["patient_id"], name: "index_vaccination_records_on_patient_id" diff --git a/spec/factories/patients.rb b/spec/factories/patients.rb index b628841b07..f38f95fbba 100644 --- a/spec/factories/patients.rb +++ b/spec/factories/patients.rb @@ -33,13 +33,14 @@ # # Indexes # -# index_patients_on_family_name_trigram (family_name) USING gin -# index_patients_on_given_name_trigram (given_name) USING gin -# index_patients_on_gp_practice_id (gp_practice_id) -# index_patients_on_names_family_first (family_name,given_name) -# index_patients_on_names_given_first (given_name,family_name) -# index_patients_on_nhs_number (nhs_number) UNIQUE -# index_patients_on_school_id (school_id) +# index_patients_on_family_name_trigram (family_name) USING gin +# index_patients_on_given_name_trigram (given_name) USING gin +# index_patients_on_gp_practice_id (gp_practice_id) +# index_patients_on_names_family_first (family_name,given_name) +# index_patients_on_names_given_first (given_name,family_name) +# index_patients_on_nhs_number (nhs_number) UNIQUE +# index_patients_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb) +# index_patients_on_school_id (school_id) # # Foreign Keys # diff --git a/spec/factories/vaccination_records.rb b/spec/factories/vaccination_records.rb index 383165c000..edb8d72487 100644 --- a/spec/factories/vaccination_records.rb +++ b/spec/factories/vaccination_records.rb @@ -40,17 +40,18 @@ # # Indexes # -# index_vaccination_records_on_batch_id (batch_id) -# index_vaccination_records_on_discarded_at (discarded_at) -# index_vaccination_records_on_location_id (location_id) -# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE -# index_vaccination_records_on_patient_id (patient_id) -# index_vaccination_records_on_performed_by_user_id (performed_by_user_id) -# index_vaccination_records_on_programme_id (programme_id) -# index_vaccination_records_on_session_id (session_id) -# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id) -# index_vaccination_records_on_uuid (uuid) UNIQUE -# index_vaccination_records_on_vaccine_id (vaccine_id) +# index_vaccination_records_on_batch_id (batch_id) +# index_vaccination_records_on_discarded_at (discarded_at) +# index_vaccination_records_on_location_id (location_id) +# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE +# index_vaccination_records_on_patient_id (patient_id) +# index_vaccination_records_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb) +# index_vaccination_records_on_performed_by_user_id (performed_by_user_id) +# index_vaccination_records_on_programme_id (programme_id) +# index_vaccination_records_on_session_id (session_id) +# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id) +# index_vaccination_records_on_uuid (uuid) UNIQUE +# index_vaccination_records_on_vaccine_id (vaccine_id) # # Foreign Keys # diff --git a/spec/models/patient_spec.rb b/spec/models/patient_spec.rb index 6428a85bd3..2f71102d67 100644 --- a/spec/models/patient_spec.rb +++ b/spec/models/patient_spec.rb @@ -33,13 +33,14 @@ # # Indexes # -# index_patients_on_family_name_trigram (family_name) USING gin -# index_patients_on_given_name_trigram (given_name) USING gin -# index_patients_on_gp_practice_id (gp_practice_id) -# index_patients_on_names_family_first (family_name,given_name) -# index_patients_on_names_given_first (given_name,family_name) -# index_patients_on_nhs_number (nhs_number) UNIQUE -# index_patients_on_school_id (school_id) +# index_patients_on_family_name_trigram (family_name) USING gin +# index_patients_on_given_name_trigram (given_name) USING gin +# index_patients_on_gp_practice_id (gp_practice_id) +# index_patients_on_names_family_first (family_name,given_name) +# index_patients_on_names_given_first (given_name,family_name) +# index_patients_on_nhs_number (nhs_number) UNIQUE +# index_patients_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb) +# index_patients_on_school_id (school_id) # # Foreign Keys # diff --git a/spec/models/vaccination_record_spec.rb b/spec/models/vaccination_record_spec.rb index 6c2820d67a..3c440a1234 100644 --- a/spec/models/vaccination_record_spec.rb +++ b/spec/models/vaccination_record_spec.rb @@ -40,17 +40,18 @@ # # Indexes # -# index_vaccination_records_on_batch_id (batch_id) -# index_vaccination_records_on_discarded_at (discarded_at) -# index_vaccination_records_on_location_id (location_id) -# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE -# index_vaccination_records_on_patient_id (patient_id) -# index_vaccination_records_on_performed_by_user_id (performed_by_user_id) -# index_vaccination_records_on_programme_id (programme_id) -# index_vaccination_records_on_session_id (session_id) -# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id) -# index_vaccination_records_on_uuid (uuid) UNIQUE -# index_vaccination_records_on_vaccine_id (vaccine_id) +# index_vaccination_records_on_batch_id (batch_id) +# index_vaccination_records_on_discarded_at (discarded_at) +# index_vaccination_records_on_location_id (location_id) +# index_vaccination_records_on_nhs_immunisations_api_id (nhs_immunisations_api_id) UNIQUE +# index_vaccination_records_on_patient_id (patient_id) +# index_vaccination_records_on_pending_changes_not_empty (id) WHERE (pending_changes <> '{}'::jsonb) +# index_vaccination_records_on_performed_by_user_id (performed_by_user_id) +# index_vaccination_records_on_programme_id (programme_id) +# index_vaccination_records_on_session_id (session_id) +# index_vaccination_records_on_supplied_by_user_id (supplied_by_user_id) +# index_vaccination_records_on_uuid (uuid) UNIQUE +# index_vaccination_records_on_vaccine_id (vaccine_id) # # Foreign Keys #