diff --git a/plugin-server/src/utils/db/db.ts b/plugin-server/src/utils/db/db.ts index 4883c4fa97eb8..9501f5a58cbf5 100644 --- a/plugin-server/src/utils/db/db.ts +++ b/plugin-server/src/utils/db/db.ts @@ -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 { distinctIds ||= [] @@ -653,7 +654,7 @@ export class DB { const personVersion = 0 const { rows } = await this.postgres.query( - PostgresUse.COMMON_WRITE, + tx ?? PostgresUse.COMMON_WRITE, `WITH inserted_person AS ( INSERT INTO posthog_person ( created_at, properties, properties_last_updated_at, diff --git a/plugin-server/src/worker/ingestion/person-state.ts b/plugin-server/src/worker/ingestion/person-state.ts index 89472de4e345e..3475bc669528a 100644 --- a/plugin-server/src/worker/ingestion/person-state.ts +++ b/plugin-server/src/worker/ingestion/person-state.ts @@ -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 { if (distinctIds.length < 1) { throw new Error('at least 1 distinctId is required in `createPerson`') @@ -284,7 +285,8 @@ export class PersonState { isUserId, isIdentified, uuid, - distinctIds + distinctIds, + tx ) } @@ -617,7 +619,8 @@ export class PersonState { [ { distinctId: distinctId1, version: distinctId1Version }, { distinctId: distinctId2, version: distinctId2Version }, - ] + ], + tx ), Promise.resolve(), ] diff --git a/plugin-server/tests/main/process-event.test.ts b/plugin-server/tests/main/process-event.test.ts index fa693a00b9651..9d9056ce8c380 100644 --- a/plugin-server/tests/main/process-event.test.ts +++ b/plugin-server/tests/main/process-event.test.ts @@ -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