feat(fusion_accounting_reports): MV for per-account-per-month balances
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
-- Materialized view: per-account aggregated balances by year-month.
|
||||
-- Used by GL drill-down + trial balance for large DBs.
|
||||
-- Refresh strategy: cron every 15 minutes (Task 25); CONCURRENTLY-capable
|
||||
-- thanks to the unique index.
|
||||
|
||||
CREATE MATERIALIZED VIEW IF NOT EXISTS fusion_account_balance_mv AS
|
||||
SELECT
|
||||
ROW_NUMBER() OVER (
|
||||
ORDER BY account_id, company_id, DATE_TRUNC('month', date)
|
||||
)::INTEGER AS id,
|
||||
account_id,
|
||||
company_id,
|
||||
DATE_TRUNC('month', date)::date AS period_month,
|
||||
SUM(debit) AS debit,
|
||||
SUM(credit) AS credit,
|
||||
SUM(balance) AS balance,
|
||||
COUNT(*) AS line_count
|
||||
FROM account_move_line
|
||||
WHERE parent_state = 'posted'
|
||||
GROUP BY account_id, company_id, DATE_TRUNC('month', date);
|
||||
|
||||
-- The (account_id, company_id, period_month) tuple is the natural key.
|
||||
-- We mark it UNIQUE so REFRESH MATERIALIZED VIEW CONCURRENTLY is allowed.
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS fusion_account_balance_mv_pkey
|
||||
ON fusion_account_balance_mv (account_id, company_id, period_month);
|
||||
-- A separate index on the synthetic id is required by Odoo's ORM, which
|
||||
-- expects every model row to be addressable by `id`.
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS fusion_account_balance_mv_id_idx
|
||||
ON fusion_account_balance_mv (id);
|
||||
CREATE INDEX IF NOT EXISTS fusion_account_balance_mv_company_month
|
||||
ON fusion_account_balance_mv (company_id, period_month);
|
||||
Reference in New Issue
Block a user