Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-server): fix conflict case in addPersonlessDistinctId #23378

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading