Files
acmcc/supabase/migrations/20260611200000_accounting_je_indexes.sql
T
admin f5f6285bbd Accounting: fix empty JE/GL pages + nightly Buildium GL pull sync
- Add missing indexes on journal_entry_lines (journal_entry_id, account_id)
  and journal_entries (company_id, date): Bridgewater's ledger query took
  7.5s, blew the 8s statement timeout, and rendered the JE/GL pages empty
- Paginate JE/GL page fetches past the 1000-row PostgREST cap (shared
  fetchJournalEntries helper) and surface query errors instead of
  swallowing them into an empty list
- New buildium-gl-sync edge function (scheduled nightly via pg_cron):
  incrementally pulls new Buildium GL activity into accounting journal
  entries via GET /v1/generalledger, reconstructing double-entry JEs by
  grouping per-account entries by transaction id; watermark + 14-day
  overlap window, dedupe on (company_id, external_source, external_id),
  account mapping by external_id/code/name with auto-create for new
  Buildium accounts

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-11 22:12:38 -04:00

13 lines
623 B
SQL

-- Performance: journal_entry_lines had no FK indexes, so every nested
-- journal_entries -> journal_entry_lines fetch seq-scanned all lines per entry.
-- For Bridgewater (4,189 JEs / 10,075 lines) the JE + GL pages exceeded the 8s
-- statement timeout and rendered empty.
create index if not exists journal_entry_lines_journal_entry_id_idx
on accounting.journal_entry_lines (journal_entry_id);
create index if not exists journal_entry_lines_account_id_idx
on accounting.journal_entry_lines (account_id);
create index if not exists journal_entries_company_date_idx
on accounting.journal_entries (company_id, date);