From a49a6450228d3aeef42874a2dbd9c9a7ce88c02d Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Sat, 27 Jun 2026 11:40:02 +0300 Subject: [PATCH] fix completed_at index --- app/controllers/api/submitters_controller.rb | 4 ++-- app/models/submitter.rb | 2 +- ...0627065517_remove_submitters_completed_at_index.rb | 11 +++++++++++ ...83558_add_submitters_completed_at_partial_index.rb | 11 +++++++++++ db/schema.rb | 4 ++-- 5 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20260627065517_remove_submitters_completed_at_index.rb create mode 100644 db/migrate/20260627083558_add_submitters_completed_at_partial_index.rb diff --git a/app/controllers/api/submitters_controller.rb b/app/controllers/api/submitters_controller.rb index fbaf3477..15b3e119 100644 --- a/app/controllers/api/submitters_controller.rb +++ b/app/controllers/api/submitters_controller.rb @@ -104,7 +104,7 @@ module Api private - def maybe_filder_by_completed_at(submitters, params) + def maybe_filter_by_completed_at(submitters, params) if params[:completed_after].present? submitters = submitters.where(completed_at: Time.zone.parse(params[:completed_after])..) end @@ -177,7 +177,7 @@ module Api submitters = submitters.joins(:submission).where(submissions: { template_id: params[:template_id] }) end - maybe_filder_by_completed_at(submitters, params) + maybe_filter_by_completed_at(submitters, params) end def assign_external_id(submitter, attrs) diff --git a/app/models/submitter.rb b/app/models/submitter.rb index 0e4bc6c2..2c515ae2 100644 --- a/app/models/submitter.rb +++ b/app/models/submitter.rb @@ -28,8 +28,8 @@ # # Indexes # +# index_submitters_on_account_id_and_completed_at (account_id,completed_at) WHERE (completed_at IS NOT NULL) # index_submitters_on_account_id_and_id (account_id,id) -# index_submitters_on_completed_at_and_account_id (completed_at,account_id) # index_submitters_on_email (email) # index_submitters_on_external_id (external_id) # index_submitters_on_slug (slug) UNIQUE diff --git a/db/migrate/20260627065517_remove_submitters_completed_at_index.rb b/db/migrate/20260627065517_remove_submitters_completed_at_index.rb new file mode 100644 index 00000000..e3d420c9 --- /dev/null +++ b/db/migrate/20260627065517_remove_submitters_completed_at_index.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class RemoveSubmittersCompletedAtIndex < ActiveRecord::Migration[8.1] + def up + remove_index :submitters, %i[completed_at account_id], if_exists: true + end + + def down + add_index :submitters, %i[completed_at account_id], if_not_exists: true + end +end diff --git a/db/migrate/20260627083558_add_submitters_completed_at_partial_index.rb b/db/migrate/20260627083558_add_submitters_completed_at_partial_index.rb new file mode 100644 index 00000000..f2b6dcbf --- /dev/null +++ b/db/migrate/20260627083558_add_submitters_completed_at_partial_index.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class AddSubmittersCompletedAtPartialIndex < ActiveRecord::Migration[8.1] + def up + add_index :submitters, %i[account_id completed_at], where: 'completed_at IS NOT NULL', if_not_exists: true + end + + def down + remove_index :submitters, %i[account_id completed_at], if_exists: true + end +end diff --git a/db/schema.rb b/db/schema.rb index d8d7d1cb..7b65ff4e 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.1].define(version: 2026_05_06_121640) do +ActiveRecord::Schema[8.1].define(version: 2026_06_27_083558) do # These are extensions that must be enabled in order to support this database enable_extension "btree_gin" enable_extension "pg_catalog.plpgsql" @@ -405,8 +405,8 @@ ActiveRecord::Schema[8.1].define(version: 2026_05_06_121640) do t.datetime "updated_at", null: false t.string "uuid", null: false t.text "values", null: false + t.index ["account_id", "completed_at"], name: "index_submitters_on_account_id_and_completed_at", where: "(completed_at IS NOT NULL)" t.index ["account_id", "id"], name: "index_submitters_on_account_id_and_id" - t.index ["completed_at", "account_id"], name: "index_submitters_on_completed_at_and_account_id" t.index ["email"], name: "index_submitters_on_email" t.index ["external_id"], name: "index_submitters_on_external_id" t.index ["slug"], name: "index_submitters_on_slug", unique: true