Skip to content

Commit

Permalink
fix(plugin-server): fix conflict case in addPersonlessDistinctId (#23378
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bretthoerner authored Jul 1, 2024
1 parent c7f3a82 commit 1237677
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
18 changes: 17 additions & 1 deletion plugin-server/src/utils/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,23 @@ export class DB {
'addPersonlessDistinctId'
)

return result.rows[0]['is_merged']
if (result.rows.length === 1) {
return result.rows[0]['is_merged']
}

// ON CONFLICT ... DO NOTHING won't give us our RETURNING, so we have to do another SELECT
const existingResult = await this.postgres.query(
PostgresUse.COMMON_WRITE,
`
SELECT is_merged
FROM posthog_personlessdistinctid
WHERE team_id = $1 AND distinct_id = $2
`,
[teamId, distinctId],
'addPersonlessDistinctId'
)

return existingResult.rows[0]['is_merged']
}

public async addPersonlessDistinctIdForMerge(
Expand Down
17 changes: 17 additions & 0 deletions plugin-server/tests/main/db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,23 @@ describe('DB', () => {
return selectResult.rows[0]
}

test('addPersonlessDistinctId', async () => {
const team = await getFirstTeam(hub)
await db.addPersonlessDistinctId(team.id, 'addPersonlessDistinctId')

// This will conflict, but shouldn't throw an error
await db.addPersonlessDistinctId(team.id, 'addPersonlessDistinctId')

const result = await db.postgres.query(
PostgresUse.COMMON_WRITE,
'SELECT id FROM posthog_personlessdistinctid WHERE team_id = $1 AND distinct_id = $2',
[team.id, 'addPersonlessDistinctId'],
'addPersonlessDistinctId'
)

expect(result.rows.length).toEqual(1)
})

describe('createPerson', () => {
let team: Team
const uuid = new UUIDT().toString()
Expand Down

0 comments on commit 1237677

Please sign in to comment.