Files
paliad/internal/db/migrations/094_clientmatter_six_digit.down.sql
mAi bdd4999213 fix(projects): unbreak Create — drop $1::text reuse + tighten CM CHECK to 6 digits
Two issues m hit and reported in one breath while adding a project:

1. **Internal error on POST /projects** (prod-only, surfaced at 10:23). Both
   ProjectService.Create and CreateCounterclaim re-referenced the uuid
   parameter `$1` as `$1::text` to fill the path placeholder. Postgres'
   planner deduced conflicting types for `$1` (uuid in the id column,
   text in the cast) and rejected the prepared statement with 42P08
   "inconsistent types deduced for parameter". The path placeholder
   value is irrelevant — paliad.projects_sync_path() (BEFORE INSERT
   trigger from mig 018/021) always overwrites it from id and parent
   path. Fix: replace `$1::text` with a literal '' in both INSERTs,
   keeping the parameter list decoupled from the id column's type.
   Same comment now anchors the rationale on both call sites.

2. **CM number length — 6 digits, not 7.** m's correction; mig 018's
   `^[0-9]{7}$` CHECK on paliad.projects.client_number and
   matter_number was wrong. Mig 094 snapshots affected rows to
   paliad.projects_pre_094, NULL-s the 3 surviving 7-digit test
   values (2 client_numbers, 1 matter_number), then swaps the legacy
   `projekte_*_check` constraints from {7} to {6}. Frontend pattern,
   maxLength, placeholder, labels, and i18n hint flipped from 7 → 6
   on both DE and EN sides; format hint reads CCCCCC.MMMMMM now.

Dry-run against live DB (BEGIN..ROLLBACK):
- Fixed Create SQL: trigger populates path = id::text (36 chars). ✓
- Mig 094: 2 rows snapshotted, 0 clients/matters remain after clear,
  0 rows violate the new 6-digit CHECK. ✓

go build, go test ./internal/..., bun run build all clean.
2026-05-17 12:30:53 +02:00

33 lines
1.2 KiB
SQL

-- mig 094 DOWN — restore the 7-digit CHECK and the snapshotted
-- pre-clear client_number / matter_number values from
-- paliad.projects_pre_094. Symmetric to the up migration.
SELECT set_config(
'paliad.audit_reason',
'mig 094 DOWN: restore 7-digit CHECK and pre-094 client_number/matter_number values from snapshot',
true);
-- 1. Drop the 6-digit CHECKs.
ALTER TABLE paliad.projects
DROP CONSTRAINT projekte_client_number_check,
DROP CONSTRAINT projekte_matter_number_check;
-- 2. Restore the original values from the snapshot. Only rows that
-- existed at snapshot time are touched; rows added since stay as
-- they were.
UPDATE paliad.projects p
SET client_number = s.client_number,
matter_number = s.matter_number
FROM paliad.projects_pre_094 s
WHERE p.id = s.id;
-- 3. Re-add the legacy 7-digit CHECKs.
ALTER TABLE paliad.projects
ADD CONSTRAINT projekte_client_number_check
CHECK (client_number IS NULL OR client_number ~ '^[0-9]{7}$'),
ADD CONSTRAINT projekte_matter_number_check
CHECK (matter_number IS NULL OR matter_number ~ '^[0-9]{7}$');
-- 4. Drop the snapshot. The down migration is the only consumer.
DROP TABLE IF EXISTS paliad.projects_pre_094;