feat: database indexing optimization for financial queries (#128)#395
Open
sinatragianpaolo-oc wants to merge 3 commits intorohitdash08:mainfrom
Open
feat: database indexing optimization for financial queries (#128)#395sinatragianpaolo-oc wants to merge 3 commits intorohitdash08:mainfrom
sinatragianpaolo-oc wants to merge 3 commits intorohitdash08:mainfrom
Conversation
…08#128) - Migration 007_db_indexing.sql: 13 new indexes covering all hot paths - expenses: category filter, type split, monthly aggregation, amount range - bills: active-only partial index, autopay partial index - reminders: pending dispatch partial index, bill FK - recurring_expenses: active partial index - categories: user+name composite - audit_logs: user+date, action+date - user_subscriptions: active partial index - db/index_report.py: flask index-report CLI + check_missing_indexes() - 9 tests: index list validation + query pattern integration tests Closes rohitdash08#128
added 2 commits
March 16, 2026 19:09
Fix 1 — REQUIRED_INDEXES / migration mismatch
Four indexes listed in REQUIRED_INDEXES were not created by
007_db_indexing.sql, causing 'flask index-report' to always report them
as missing even after applying the migration.
Added the four missing CREATE INDEX statements to 007_db_indexing.sql:
- idx_expenses_user_spent_at : basic user+date DESC for simple range queries
- idx_bills_user_due : full (non-partial) user+next_due_date
- idx_reminders_due : full user+send_at for per-user listing
- idx_recurring_expenses_user_start : full user+start_date for history views
Each complements its existing partial-index sibling already in the file:
the full index covers queries that cannot use the partial WHERE clause.
Fix 2 — DATE_TRUNC PostgreSQL-only caveat documented
Added an explicit comment block directly above the
idx_expenses_user_month CREATE INDEX statement warning that DATE_TRUNC
is a PostgreSQL-specific function and that this statement will fail on
SQLite and MySQL.
Tests updated:
- test_critical_indexes_documented: now asserts every REQUIRED_INDEXES
entry including all four previously missing ones
- test_required_indexes_match_migration (new): static regression guard
that reads 007_db_indexing.sql and asserts every name in
REQUIRED_INDEXES has a corresponding CREATE INDEX statement — prevents
the lists from drifting again
- test_date_trunc_index_is_postgres_only (new): reads the migration file
and asserts that a 'postgresql'/'postgres' warning comment appears in
the context preceding idx_expenses_user_month
Adds a JSON HTTP equivalent of the 'flask index-report' CLI command. GET /admin/db/indexes (JWT + ADMIN role required) returns: existing — all current DB indexes required — REQUIRED_INDEXES constant missing — required indexes not yet present extra — indexes present but not in required list healthy — true when missing is empty On non-PostgreSQL engines (SQLite in tests) existing is empty and all required indexes appear as missing — same behaviour as the CLI. Tests added (TestAdminDbIndexesEndpoint): - 401 without auth - 403 for non-admin users - 200 + correct structure for admin - required list matches REQUIRED_INDEXES constant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds database indexing optimization for financial queries to fix #128.
007_db_indexing.sqlcreates targeted indexes for the hottest query patterns (expenses date-range, monthly aggregation, bills/reminders dispatch, categories).flask index-reportCLI command shows current indexes vs. recommended list.GET /admin/db/indexes(JWT + ADMIN role) — HTTP JSON equivalent of the CLI report, returningexisting,required,missing,extra, andhealthyfields.Review fixes
REQUIRED_INDEXES/ migration mismatch fixed — four indexes (idx_expenses_user_spent_at,idx_bills_user_due,idx_reminders_due,idx_recurring_expenses_user_start) were listed inREQUIRED_INDEXESbut absent from the SQL. Added the missingCREATE INDEX IF NOT EXISTSstatements.DATE_TRUNCPostgreSQL-only caveat documented — comment added aboveidx_expenses_user_month.GET /admin/db/indexesendpoint — returns the same data as the CLI in JSON; admin-only (403 for regular users).Tests:
test_required_indexes_match_migration(static regression guard),test_date_trunc_index_is_postgres_only,TestAdminDbIndexesEndpoint(auth, role, structure, required-list match).Closes #128