fix: debug-sequences endpoint regex escaping with $queryRawUnsafe

Prisma tagged template literals were mangling regex backslashes.
Switch to $queryRawUnsafe for the complex regex queries.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
AI Assistant
2026-03-11 21:38:40 +02:00
parent f7190bb98e
commit eb39024548
@@ -23,9 +23,10 @@ export async function GET() {
>`SELECT company, year, type, "lastSeq" FROM "RegistrySequence" ORDER BY company, year, type`;
// Get actual max sequences from entries
const actuals = await prisma.$queryRaw<
// Use Prisma.$queryRawUnsafe to avoid tagged-template escaping issues with regex
const actuals = await prisma.$queryRawUnsafe<
Array<{ prefix: string; maxSeq: number; count: number }>
>`
>(`
SELECT
SUBSTRING(value::text FROM '"number":"([A-Z]{3}-\\d{4}-(?:IN|OUT|INT))-') AS prefix,
MAX(CAST(SUBSTRING(value::text FROM '"number":"[A-Z]{3}-\\d{4}-(?:IN|OUT|INT)-(\\d{5})"') AS INTEGER)) AS "maxSeq",
@@ -36,7 +37,7 @@ export async function GET() {
AND value::text ~ '"number":"[A-Z]{3}-\\d{4}-(IN|OUT|INT)-\\d{5}"'
GROUP BY prefix
ORDER BY prefix
`;
`);
return NextResponse.json({
counters,
@@ -55,14 +56,14 @@ export async function POST() {
const deleted = await prisma.$executeRaw`DELETE FROM "RegistrySequence"`;
// Re-create counters from actual entries
const inserted = await prisma.$executeRaw`
const inserted = await prisma.$executeRawUnsafe(`
INSERT INTO "RegistrySequence" (id, company, year, type, "lastSeq", "createdAt", "updatedAt")
SELECT
gen_random_uuid()::text,
SUBSTRING(value::text FROM '"number":"([A-Z]+)-') AS company,
CAST(SUBSTRING(value::text FROM '"number":"[A-Z]+-(\d{4})-') AS INTEGER) AS year,
SUBSTRING(value::text FROM '"number":"[A-Z]+-\d{4}-([A-Z]+)-') AS type,
MAX(CAST(SUBSTRING(value::text FROM '"number":"[A-Z]+-\d{4}-[A-Z]+-(\\d{5})"') AS INTEGER)) AS "lastSeq",
CAST(SUBSTRING(value::text FROM '"number":"[A-Z]+-(\\d{4})-') AS INTEGER) AS year,
SUBSTRING(value::text FROM '"number":"[A-Z]+-\\d{4}-([A-Z]+)-') AS type,
MAX(CAST(SUBSTRING(value::text FROM '"number":"[A-Z]+-\\d{4}-[A-Z]+-(\\d{5})"') AS INTEGER)) AS "lastSeq",
NOW(),
NOW()
FROM "KeyValueStore"
@@ -70,7 +71,7 @@ export async function POST() {
AND key LIKE 'entry:%'
AND value::text ~ '"number":"[A-Z]{3}-\\d{4}-(IN|OUT|INT)-\\d{5}"'
GROUP BY company, year, type
`;
`);
return NextResponse.json({
success: true,