fix: JSONB space-after-colon in all registry LIKE/regex patterns

PostgreSQL JSONB value::text serializes JSON with spaces after colons
("number": "B-2026-00001") but all LIKE patterns searched for the
no-space format ("number":"B-2026-00001"), causing zero matches and
every new entry getting sequence #1.

Fixed in allocateSequenceNumber, recalculateSequence, and debug-sequences.
Added PATCH handler to migrate old-format entries (BTG/SDT/USW/GRP)
to new single-letter format (B/S/U/G).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-11 22:22:33 +02:00
parent 4b61d07ffd
commit 5cb438ef67
2 changed files with 88 additions and 20 deletions
@@ -258,7 +258,8 @@ export async function allocateSequenceNumber(
// New format: B-2026-00001
// NOTE: Use [0-9] instead of \d — PostgreSQL POSIX regex may not support \d
const newLike = `%"number":"${companyPrefix}-${yr}-%"%`;
// NOTE: JSONB value::text serializes with a space after colons ("number": "...")
const newLike = `%"number": "${companyPrefix}-${yr}-%"%`;
const newRegex = `${companyPrefix}-${yr}-([0-9]{5})`;
const newMaxRows = await tx.$queryRaw<Array<{ maxSeq: number | null }>>`
SELECT MAX(
@@ -274,7 +275,7 @@ export async function allocateSequenceNumber(
// Old format: BTG-2026-IN-00001 / BTG-2026-OUT-00001
let oldMax = 0;
if (oldPrefix) {
const oldLike = `%"number":"${oldPrefix}-${yr}-%"%`;
const oldLike = `%"number": "${oldPrefix}-${yr}-%"%`;
const oldRegex = `${oldPrefix}-${yr}-(?:IN|OUT|INT)-([0-9]{5})`;
const oldMaxRows = await tx.$queryRaw<Array<{ maxSeq: number | null }>>`
SELECT MAX(
@@ -335,7 +336,8 @@ export async function recalculateSequence(
// Find max from new format (B-2026-00001)
// NOTE: Use [0-9] instead of \d — PostgreSQL POSIX regex may not support \d
const newLike = `%"number":"${companyPrefix}-${yr}-%"%`;
// NOTE: JSONB value::text serializes with a space after colons ("number": "...")
const newLike = `%"number": "${companyPrefix}-${yr}-%"%`;
const newRegex = `${companyPrefix}-${yr}-([0-9]{5})`;
const newRows = await prisma.$queryRaw<Array<{ maxSeq: number | null }>>`
SELECT MAX(
@@ -350,7 +352,7 @@ export async function recalculateSequence(
// Also check old format (BTG-2026-OUT-00001)
if (oldPrefix) {
const oldLike = `%"number":"${oldPrefix}-${yr}-%"%`;
const oldLike = `%"number": "${oldPrefix}-${yr}-%"%`;
const oldRegex = `${oldPrefix}-${yr}-(?:IN|OUT|INT)-([0-9]{5})`;
const oldRows = await prisma.$queryRaw<Array<{ maxSeq: number | null }>>`
SELECT MAX(