Skip to content

Commit

Permalink
fix(plugin-server): use tx for create person step of merge (#23372)
Browse files Browse the repository at this point in the history
  • Loading branch information
bretthoerner authored Jul 1, 2024
1 parent 934ac04 commit 0134bc2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 3 additions & 2 deletions plugin-server/src/utils/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@ export class DB {
isUserId: number | null,
isIdentified: boolean,
uuid: string,
distinctIds?: { distinctId: string; version?: number }[]
distinctIds?: { distinctId: string; version?: number }[],
tx?: TransactionClient
): Promise<InternalPerson> {
distinctIds ||= []

Expand All @@ -653,7 +654,7 @@ export class DB {
const personVersion = 0

const { rows } = await this.postgres.query<RawPerson>(
PostgresUse.COMMON_WRITE,
tx ?? PostgresUse.COMMON_WRITE,
`WITH inserted_person AS (
INSERT INTO posthog_person (
created_at, properties, properties_last_updated_at,
Expand Down
9 changes: 6 additions & 3 deletions plugin-server/src/worker/ingestion/person-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ export class PersonState {
isUserId: number | null,
isIdentified: boolean,
creatorEventUuid: string,
distinctIds: { distinctId: string; version?: number }[]
distinctIds: { distinctId: string; version?: number }[],
tx?: TransactionClient
): Promise<InternalPerson> {
if (distinctIds.length < 1) {
throw new Error('at least 1 distinctId is required in `createPerson`')
Expand Down Expand Up @@ -284,7 +285,8 @@ export class PersonState {
isUserId,
isIdentified,
uuid,
distinctIds
distinctIds,
tx
)
}

Expand Down Expand Up @@ -617,7 +619,8 @@ export class PersonState {
[
{ distinctId: distinctId1, version: distinctId1Version },
{ distinctId: distinctId2, version: distinctId2Version },
]
],
tx
),
Promise.resolve(),
]
Expand Down
3 changes: 2 additions & 1 deletion plugin-server/tests/main/process-event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1764,7 +1764,8 @@ describe('when handling $identify', () => {
// completing before continuing with the first identify.
const originalCreatePerson = hub.db.createPerson.bind(hub.db)
const createPersonMock = jest.fn(async (...args) => {
const result = await originalCreatePerson(...args)
// We need to slice off the txn arg, or else we conflict with the `identify` below.
const result = await originalCreatePerson(...args.slice(0, -1))

if (createPersonMock.mock.calls.length === 1) {
// On second invocation, make another identify call
Expand Down

0 comments on commit 0134bc2

Please sign in to comment.