Commit graph

2 commits

Author SHA1 Message Date
Janpeter Visser
7bb252c528
fix(ideas): make nextIdeaCode self-correcting against counter drift (#200)
Fixes a P2002 unique constraint crash on (user_id, code) when
idea_code_counter on the User is behind the actual codes in the ideas
table (e.g. after direct DB inserts during development).

After incrementing the counter the function now queries
MAX(CAST(SUBSTRING(code FROM 6) AS INTEGER)) via raw SQL and takes
max(counter, maxExisting + 1) as the next code. String MAX was not
safe above IDEA-999, hence the numeric cast. If the counter lagged it
is updated in-place to stay in sync.

No schema change, no migration, no changes outside idea-code-server.ts.

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 12:46:47 +02:00
dfee518996 lib: idea-code generator + plan_md yaml-frontmatter parser (M12 T-494)
- lib/idea-code.ts: pure formatIdeaCode helper (client-safe, no prisma)
- lib/idea-code-server.ts: atomic nextIdeaCode via Prisma row-lock,
  accepts optional TransactionClient for combining with idea.create
- lib/idea-plan-parser.ts: parsePlanMd extracts ---yaml---/body, runs
  yaml.parse + ideaPlanMdFrontmatterSchema, returns line-info on failure;
  CRLF-tolerant
- adds yaml@^2.8.4 dependency
- 8 unit tests (parser happy/missing/yaml-error/zod-error/empty-stories/CRLF;
  formatIdeaCode pad-3 + overflow)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 19:40:39 +02:00