fix(migrations): adjust retention expiry trigger and validation for fiscal periods (#1104)

This commit is contained in:
Mattsson
2026-07-22 00:43:46 +02:00
committed by GitHub
parent e11f70b347
commit 3920c893f4
4 changed files with 117 additions and 2 deletions
@@ -20,14 +20,35 @@ $$;
-- PostgreSQL fires triggers with the same timing alphabetically. The zz
-- prefix makes this legal correction run after the original migration 017
-- trigger without modifying that shipped enforcement migration.
-- trigger without modifying that shipped enforcement migration. It must run
-- on every update because the original calculate_retention_expiry trigger also
-- runs on every update and would otherwise restore the old, too-early date.
DROP TRIGGER IF EXISTS zz_set_bfl_retention_expiry ON public.fiscal_periods;
CREATE TRIGGER zz_set_bfl_retention_expiry
BEFORE INSERT OR UPDATE OF period_end ON public.fiscal_periods
BEFORE INSERT OR UPDATE ON public.fiscal_periods
FOR EACH ROW EXECUTE FUNCTION public.set_bfl_retention_expiry();
-- The period-start validator depends only on company_id and period_start.
-- Restrict UPDATE execution to those columns so retention and other metadata
-- backfills do not revalidate unchanged historical period dates.
DROP TRIGGER IF EXISTS enforce_period_start_day ON public.fiscal_periods;
CREATE TRIGGER enforce_period_start_day
BEFORE INSERT OR UPDATE OF company_id, period_start ON public.fiscal_periods
FOR EACH ROW
EXECUTE FUNCTION public.enforce_first_of_month_for_subsequent_periods();
UPDATE public.fiscal_periods
SET retention_expires_at = make_date(
extract(year FROM period_end)::integer + 8,
1,
1
)
WHERE retention_expires_at IS DISTINCT FROM make_date(
extract(year FROM period_end)::integer + 8,
1,
1
);
NOTIFY pgrst, 'reload schema';