Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ def set_finalized_at
# index_invoices_on_customer_billing_entity_sequential (customer_id,billing_entity_id,sequential_id) UNIQUE
# index_invoices_on_number (number)
# index_invoices_on_organization_id_and_customer_id (customer_id,organization_id)
# index_invoices_on_organization_id_lower_number (organization_id, lower((number)::text))
# index_invoices_on_organization_id_lower_purchase_order_number (organization_id, lower((purchase_order_number)::text))
# index_invoices_on_organization_id_number_gin_trgm_ops (organization_id,number) USING gin
# index_invoices_on_organization_id_search_terms_gin_trgm_ops (organization_id,search_terms) USING gin
Expand Down
1 change: 1 addition & 0 deletions app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def payment_request_succeeded
# index_payments_by_cursor (organization_id,created_at DESC,id)
# index_payments_on_customer_id (customer_id)
# index_payments_on_invoice_id (invoice_id)
# index_payments_on_org_pending_processing_created_at (organization_id,payable_payment_status,created_at DESC,id) WHERE (payable_payment_status = ANY (ARRAY['pending'::payment_payable_payment_status, 'processing'::payment_payable_payment_status]))
# index_payments_on_organization_id (organization_id)
# index_payments_on_organization_id_reference_gin_trgm_ops (organization_id,reference) USING gin
# index_payments_on_payable_id_and_payable_type (payable_id,payable_type) UNIQUE WHERE ((payable_payment_status = ANY (ARRAY['pending'::payment_payable_payment_status, 'processing'::payment_payable_payment_status])) AND (payment_type = 'provider'::payment_type))
Expand Down
7 changes: 4 additions & 3 deletions app/models/payment_receipt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ def xml_url
#
# Indexes
#
# index_payment_receipts_on_billing_entity_id (billing_entity_id)
# index_payment_receipts_on_organization_id (organization_id)
# index_payment_receipts_on_payment_id (payment_id) UNIQUE
# index_payment_receipts_on_billing_entity_id (billing_entity_id)
# index_payment_receipts_on_organization_id (organization_id)
# index_payment_receipts_on_organization_id_lower_number (organization_id, lower((number)::text))
# index_payment_receipts_on_payment_id (payment_id) UNIQUE
#
# Foreign Keys
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class AddPaymentReceiptsOrganizationLowerNumberIndex < ActiveRecord::Migration[8.0]
disable_ddl_transaction!

def change
# Supports the exact, case-insensitive receipt_number filter of the payments
# list for large organizations. Same shape as
# index_invoices_on_organization_id_lower_purchase_order_number.
add_index :payment_receipts, "organization_id, lower(number)",
name: "index_payment_receipts_on_organization_id_lower_number",
algorithm: :concurrently,
using: :btree,
if_not_exists: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class AddInvoicesOrganizationLowerNumberIndex < ActiveRecord::Migration[8.0]
disable_ddl_transaction!

def change
# Supports the exact, case-insensitive invoice_number filter of the payments
# list for large organizations. index_invoices_on_number is case-sensitive and
# not organization-scoped; the trgm index only serves ILIKE.
add_index :invoices, "organization_id, lower(number)",
name: "index_invoices_on_organization_id_lower_number",
algorithm: :concurrently,
using: :btree,
if_not_exists: true
end
end
23 changes: 23 additions & 0 deletions db/migrate/20260909090200_add_payments_pending_processing_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

class AddPaymentsPendingProcessingIndex < ActiveRecord::Migration[8.0]
disable_ddl_transaction!

def change
# Supports the payment_status filter of the payments list for its rare values
# (pending, processing) on large organizations, ordered like the list itself.
# Partial on purpose: the planner cannot pick it for the dominant values, which
# the (organization_id, created_at DESC, id) cursor index already serves.
# Four columns like index_invoices_by_cursor: the trailing (created_at DESC, id)
# is the list ordering, so the page is read without a sort.
safety_assured do
add_index :payments,
[:organization_id, :payable_payment_status, :created_at, :id],
order: {created_at: :desc, id: :asc},
where: "payable_payment_status IN ('pending', 'processing')",
name: "index_payments_on_org_pending_processing_created_at",
algorithm: :concurrently,
if_not_exists: true
end
end
end
27 changes: 27 additions & 0 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -597,13 +597,15 @@ DROP INDEX IF EXISTS public.index_payments_on_payable_id_and_payable_type_and_er
DROP INDEX IF EXISTS public.index_payments_on_payable_id_and_payable_type;
DROP INDEX IF EXISTS public.index_payments_on_organization_id_reference_gin_trgm_ops;
DROP INDEX IF EXISTS public.index_payments_on_organization_id;
DROP INDEX IF EXISTS public.index_payments_on_org_pending_processing_created_at;
DROP INDEX IF EXISTS public.index_payments_on_invoice_id;
DROP INDEX IF EXISTS public.index_payments_on_customer_id;
DROP INDEX IF EXISTS public.index_payments_by_cursor;
DROP INDEX IF EXISTS public.index_payment_requests_on_organization_id;
DROP INDEX IF EXISTS public.index_payment_requests_on_dunning_campaign_id;
DROP INDEX IF EXISTS public.index_payment_requests_on_customer_id;
DROP INDEX IF EXISTS public.index_payment_receipts_on_payment_id;
DROP INDEX IF EXISTS public.index_payment_receipts_on_organization_id_lower_number;
DROP INDEX IF EXISTS public.index_payment_receipts_on_organization_id;
DROP INDEX IF EXISTS public.index_payment_receipts_on_billing_entity_id;
DROP INDEX IF EXISTS public.index_payment_providers_on_organization_id;
Expand Down Expand Up @@ -666,6 +668,7 @@ DROP INDEX IF EXISTS public.index_invoices_on_payment_due_date;
DROP INDEX IF EXISTS public.index_invoices_on_organization_id_search_terms_gin_trgm_ops;
DROP INDEX IF EXISTS public.index_invoices_on_organization_id_number_gin_trgm_ops;
DROP INDEX IF EXISTS public.index_invoices_on_organization_id_lower_purchase_order_number;
DROP INDEX IF EXISTS public.index_invoices_on_organization_id_lower_number;
DROP INDEX IF EXISTS public.index_invoices_on_organization_id_and_customer_id;
DROP INDEX IF EXISTS public.index_invoices_on_number;
DROP INDEX IF EXISTS public.index_invoices_on_customer_billing_entity_sequential;
Expand Down Expand Up @@ -10027,6 +10030,13 @@ CREATE INDEX index_invoices_on_number ON public.invoices USING btree (number);
CREATE INDEX index_invoices_on_organization_id_and_customer_id ON public.invoices USING btree (customer_id, organization_id);


--
-- Name: index_invoices_on_organization_id_lower_number; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_invoices_on_organization_id_lower_number ON public.invoices USING btree (organization_id, lower((number)::text));


--
-- Name: index_invoices_on_organization_id_lower_purchase_order_number; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -10461,6 +10471,13 @@ CREATE INDEX index_payment_receipts_on_billing_entity_id ON public.payment_recei
CREATE INDEX index_payment_receipts_on_organization_id ON public.payment_receipts USING btree (organization_id);


--
-- Name: index_payment_receipts_on_organization_id_lower_number; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_payment_receipts_on_organization_id_lower_number ON public.payment_receipts USING btree (organization_id, lower((number)::text));


--
-- Name: index_payment_receipts_on_payment_id; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -10510,6 +10527,13 @@ CREATE INDEX index_payments_on_customer_id ON public.payments USING btree (custo
CREATE INDEX index_payments_on_invoice_id ON public.payments USING btree (invoice_id);


--
-- Name: index_payments_on_org_pending_processing_created_at; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX index_payments_on_org_pending_processing_created_at ON public.payments USING btree (organization_id, payable_payment_status, created_at DESC, id) WHERE (payable_payment_status = ANY (ARRAY['pending'::public.payment_payable_payment_status, 'processing'::public.payment_payable_payment_status]));


--
-- Name: index_payments_on_organization_id; Type: INDEX; Schema: public; Owner: -
--
Expand Down Expand Up @@ -14904,6 +14928,9 @@ ALTER TABLE ONLY public.membership_roles
SET search_path TO "$user", public;

INSERT INTO "schema_migrations" (version) VALUES
('20260909090200'),
('20260909090100'),
('20260909090000'),
('20260908180522'),
('20260905223042'),
('20260905223041'),
Expand Down
Loading