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) <noreply@anthropic.com>

* 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) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-04-18 14:32:22 +02:00
committed by GitHub
parent d741c46d4e
commit b5fc95276c
@@ -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 (AZ) 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';