forked from farhoodlabs/paperclip
Merge pull request #2818 from mvanhorn/fix/2705-identifier-collision
fix(server): prevent identifier collision in issue creation
This commit is contained in:
@@ -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 });
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user