mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-16 10:50:38 +09:00
Merge pull request #2818 from mvanhorn/fix/2705-identifier-collision
fix(server): prevent identifier collision in issue creation
This commit is contained in:
commit
7e78ce0d7e
1 changed files with 11 additions and 1 deletions
|
|
@ -1487,9 +1487,19 @@ export function issueService(db: Db) {
|
||||||
if (executionWorkspaceId) {
|
if (executionWorkspaceId) {
|
||||||
await assertValidExecutionWorkspace(companyId, issueData.projectId, executionWorkspaceId, tx);
|
await assertValidExecutionWorkspace(companyId, issueData.projectId, executionWorkspaceId, tx);
|
||||||
}
|
}
|
||||||
|
// Self-correcting counter: use MAX(issue_number) + 1 if the counter
|
||||||
|
// has drifted below the actual max, preventing identifier collisions.
|
||||||
|
const [maxRow] = await tx
|
||||||
|
.select({ maxNum: sql<number>`coalesce(max(${issues.issueNumber}), 0)` })
|
||||||
|
.from(issues)
|
||||||
|
.where(eq(issues.companyId, companyId));
|
||||||
|
const currentMax = maxRow?.maxNum ?? 0;
|
||||||
|
|
||||||
const [company] = await tx
|
const [company] = await tx
|
||||||
.update(companies)
|
.update(companies)
|
||||||
.set({ issueCounter: sql`${companies.issueCounter} + 1` })
|
.set({
|
||||||
|
issueCounter: sql`greatest(${companies.issueCounter}, ${currentMax}) + 1`,
|
||||||
|
})
|
||||||
.where(eq(companies.id, companyId))
|
.where(eq(companies.id, companyId))
|
||||||
.returning({ issueCounter: companies.issueCounter, issuePrefix: companies.issuePrefix });
|
.returning({ issueCounter: companies.issueCounter, issuePrefix: companies.issuePrefix });
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue