From b5fc95276c875d75dba009fa560a8e7b555f430f Mon Sep 17 00:00:00 2001 From: Jakob Wennberg <149234542+jakobwennberg@users.noreply.github.com> Date: Sat, 18 Apr 2026 14:32:22 +0200 Subject: [PATCH] fix: add missing default_voucher_series column to company_settings (#269) * fix: add missing default_voucher_series column to company_settings The /settings/bookkeeping form, TypeScript types, and Zod schema have all referenced company_settings.default_voucher_series, but no migration ever added it. Saving the setting failed with: "Could not find the 'default_voucher_series' column of 'company_settings' in the schema cache". Adds the column (text NOT NULL DEFAULT 'A') with a CHECK constraint that mirrors the Zod validation ([A-Z]), plus a PostgREST schema reload. Migration already applied to production to unblock reporting users. Co-Authored-By: Claude Opus 4.7 (1M context) * fix: explicitly enforce NOT NULL and DEFAULT on default_voucher_series ADD COLUMN IF NOT EXISTS silently skips the column definition when the column already exists, so environments that got the column out-of-band could end up with a different NOT NULL/DEFAULT attribute than a fresh boot. Add explicit idempotent ALTER COLUMN statements that reassert both, with a backfill pass so SET NOT NULL can't fail on legacy nullable rows. Co-Authored-By: Claude Opus 4.7 (1M context) --------- Co-authored-by: Claude Opus 4.7 (1M context) --- ...ompany_settings_default_voucher_series.sql | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 supabase/migrations/20260418150000_company_settings_default_voucher_series.sql diff --git a/supabase/migrations/20260418150000_company_settings_default_voucher_series.sql b/supabase/migrations/20260418150000_company_settings_default_voucher_series.sql new file mode 100644 index 00000000..e0fb1175 --- /dev/null +++ b/supabase/migrations/20260418150000_company_settings_default_voucher_series.sql @@ -0,0 +1,41 @@ +-- Add default_voucher_series to company_settings. +-- +-- The frontend (/settings/bookkeeping) and TypeScript types have referenced +-- this column for a while, but no migration ever added it, so saving the form +-- failed with: "Could not find the 'default_voucher_series' column of +-- 'company_settings' in the schema cache". +-- +-- The column stores the per-company default voucher series (A–Z) that is +-- pre-selected when booking manual journal entries. The actual sequence +-- counters live in public.voucher_sequences, keyed on (user_id, +-- fiscal_period_id, voucher_series) — this setting only controls the UI +-- default. + +ALTER TABLE public.company_settings + ADD COLUMN IF NOT EXISTS default_voucher_series text NOT NULL DEFAULT 'A'; + +-- ADD COLUMN IF NOT EXISTS silently skips the whole column definition +-- (including NOT NULL) when the column already exists. Re-apply the +-- constraint explicitly so environments that received the column +-- out-of-band still end up with the same schema. +ALTER TABLE public.company_settings + ALTER COLUMN default_voucher_series SET DEFAULT 'A'; + +UPDATE public.company_settings + SET default_voucher_series = 'A' + WHERE default_voucher_series IS NULL; + +ALTER TABLE public.company_settings + ALTER COLUMN default_voucher_series SET NOT NULL; + +-- Match the Zod validation in lib/api/schemas.ts so DB and app stay in sync. +ALTER TABLE public.company_settings + DROP CONSTRAINT IF EXISTS company_settings_default_voucher_series_check; + +ALTER TABLE public.company_settings + ADD CONSTRAINT company_settings_default_voucher_series_check + CHECK (default_voucher_series ~ '^[A-Z]$'); + +-- Reload PostgREST schema cache so the column becomes visible to the API +-- immediately, without waiting for the next automatic reload. +NOTIFY pgrst, 'reload schema';