Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

5.6 KB
Raw Blame
import { DateTime } from 'luxon'

import { PersonMessage } from '~/common/persons/person-message'
import { PersonUpdate } from '~/common/persons/person-update-batch'
import { CreatePersonResult, MoveDistinctIdsResult } from '~/common/utils/db/db'
import { TransactionClient } from '~/common/utils/db/postgres'
import { Properties } from '~/plugin-scaffold'
import { InternalPerson, PersonUpdateFields, PropertiesLastOperation, PropertiesLastUpdatedAt, Team } from '~/types'

import { LifecycleMarkPerson } from './person-repository'
import { PersonRepositoryTransaction } from './person-repository-transaction'
import { RawPostgresPersonRepository } from './raw-postgres-person-repository'

export class PostgresPersonRepositoryTransaction implements PersonRepositoryTransaction {
    constructor(
        private transaction: TransactionClient,
        private repository: RawPostgresPersonRepository
    ) {}

    async createPerson(
        createdAt: DateTime,
        properties: Properties,
        propertiesLastUpdatedAt: PropertiesLastUpdatedAt,
        propertiesLastOperation: PropertiesLastOperation,
        teamId: Team['id'],
        isUserId: number | null,
        isIdentified: boolean,
        uuid: string,
        primaryDistinctId: { distinctId: string; version?: number },
        extraDistinctIds?: { distinctId: string; version?: number }[]
    ): Promise<CreatePersonResult> {
        return await this.repository.createPerson(
            createdAt,
            properties,
            propertiesLastUpdatedAt,
            propertiesLastOperation,
            teamId,
            isUserId,
            isIdentified,
            uuid,
            primaryDistinctId,
            extraDistinctIds,
            this.transaction
        )
    }

    async updatePerson(
        person: InternalPerson,
        update: PersonUpdateFields,
        tag?: string
    ): Promise<[InternalPerson, PersonMessage[], boolean]> {
        return await this.repository.updatePerson(person, update, tag, this.transaction)
    }

    async deletePerson(person: InternalPerson): Promise<PersonMessage[]> {
        return await this.repository.deletePerson(person, this.transaction)
    }

    async deletePersons(persons: InternalPerson[]): Promise<PersonMessage[]> {
        return await this.repository.deletePersons(persons, this.transaction)
    }

    async claimLifecycleMarks(opId: string, teamId: number, persons: LifecycleMarkPerson[]): Promise<void> {
        return await this.repository.claimLifecycleMarks(opId, teamId, persons, this.transaction)
    }

    async releaseLifecycleMarks(opId: string, teamId: number): Promise<void> {
        return await this.repository.releaseLifecycleMarks(opId, teamId, this.transaction)
    }

    async isPersonLive(person: InternalPerson): Promise<boolean> {
        return await this.repository.isPersonLive(person, this.transaction)
    }

    async readMergeRows(teamId: number, targetId: string, sourceIds: string[]): Promise<InternalPerson[]> {
        return await this.repository.readMergeRows(teamId, targetId, sourceIds, this.transaction)
    }

    async readPerson(teamId: Team['id'], distinctId: string): Promise<InternalPerson | undefined> {
        return await this.repository.readPerson(teamId, distinctId, this.transaction)
    }

    async updatePersonsBatch(personUpdates: PersonUpdate[]): Promise<
        Map<
            string,
            {
                success: boolean
                version?: number
                kafkaMessage?: PersonMessage
                person?: InternalPerson
                error?: Error
            }
        >
    > {
        return await this.repository.updatePersonsBatch(personUpdates, this.transaction)
    }

    async addDistinctId(person: InternalPerson, distinctId: string, version: number): Promise<PersonMessage[]> {
        return await this.repository.addDistinctId(person, distinctId, version, this.transaction)
    }

    async moveDistinctIds(
        source: InternalPerson,
        target: InternalPerson,
        limit?: number
    ): Promise<MoveDistinctIdsResult> {
        return await this.repository.moveDistinctIds(source, target, limit, this.transaction)
    }

    async moveDistinctIdsFromPersons(
        sources: InternalPerson[],
        target: InternalPerson
    ): Promise<MoveDistinctIdsResult> {
        return await this.repository.moveDistinctIdsFromPersons(sources, target, this.transaction)
    }

    async countDistinctIdsForPersons(
        teamID: Team['id'],
        personIds: InternalPerson['id'][]
    ): Promise<Map<string, number>> {
        return await this.repository.countDistinctIdsForPersons(teamID, personIds, this.transaction)
    }

    async fetchPersonDistinctIds(person: InternalPerson, limit?: number): Promise<string[]> {
        return await this.repository.fetchPersonDistinctIds(person, limit, this.transaction)
    }

    async updateCohortsAndFeatureFlagsForMerge(
        teamID: Team['id'],
        sourcePersonID: InternalPerson['id'],
        targetPersonID: InternalPerson['id']
    ): Promise<void> {
        return await this.repository.updateCohortsAndFeatureFlagsForMerge(
            teamID,
            sourcePersonID,
            targetPersonID,
            this.transaction
        )
    }

    async updateCohortsAndFeatureFlagsForMergeBatch(
        teamID: Team['id'],
        sourcePersonIDs: InternalPerson['id'][],
        targetPersonID: InternalPerson['id']
    ): Promise<void> {
        return await this.repository.updateCohortsAndFeatureFlagsForMergeBatch(
            teamID,
            sourcePersonIDs,
            targetPersonID,
            this.transaction
        )
    }
}