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.

4.8 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,
    TeamId,
} from '~/types'

import { InternalPersonWithDistinctId, LifecycleMarkPerson } from './person-repository'

export interface RawPostgresPersonRepository {
    fetchPerson(
        teamId: Team['id'],
        distinctId: string,
        options?: { forUpdate?: boolean; useReadReplica?: boolean }
    ): Promise<InternalPerson | undefined>

    fetchPersonsByDistinctIds(
        teamPersons: { teamId: TeamId; distinctId: string }[],
        useReadReplica?: boolean
    ): Promise<InternalPersonWithDistinctId[]>

    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 }[],
        tx?: TransactionClient
    ): Promise<CreatePersonResult>

    updatePerson(
        person: InternalPerson,
        update: PersonUpdateFields,
        tag?: string,
        tx?: TransactionClient
    ): Promise<[InternalPerson, PersonMessage[], boolean]>

    updatePersonAssertVersion(personUpdate: PersonUpdate): Promise<[number | undefined, PersonMessage[]]>

    updatePersonsBatch(
        personUpdates: PersonUpdate[],
        tx?: TransactionClient
    ): Promise<
        Map<
            string,
            {
                success: boolean
                version?: number
                kafkaMessage?: PersonMessage
                person?: InternalPerson
                error?: Error
            }
        >
    >

    deletePerson(person: InternalPerson, tx?: TransactionClient): Promise<PersonMessage[]>

    /** Batched deletePerson for folded merges; all persons must belong to one team. */
    deletePersons(persons: InternalPerson[], tx?: TransactionClient): Promise<PersonMessage[]>

    /** See PersonRepository.claimLifecycleMarks. */
    claimLifecycleMarks(
        opId: string,
        teamId: number,
        persons: LifecycleMarkPerson[],
        tx?: TransactionClient
    ): Promise<void>

    /** See PersonRepository.releaseLifecycleMarks. */
    releaseLifecycleMarks(opId: string, teamId: number, tx?: TransactionClient): Promise<void>

    /** See PersonRepository.isPersonLive. */
    isPersonLive(person: InternalPerson, tx?: TransactionClient): Promise<boolean>

    readPerson(teamId: Team['id'], distinctId: string, tx?: TransactionClient): Promise<InternalPerson | undefined>

    /** The target and sources are row-locked, in ascending id order, until the transaction ends. */
    readMergeRows(
        teamId: number,
        targetId: string,
        sourceIds: string[],
        tx?: TransactionClient
    ): Promise<InternalPerson[]>

    addDistinctId(
        person: InternalPerson,
        distinctId: string,
        version: number,
        tx?: TransactionClient
    ): Promise<PersonMessage[]>

    moveDistinctIds(
        source: InternalPerson,
        target: InternalPerson,
        limit?: number,
        tx?: TransactionClient
    ): Promise<MoveDistinctIdsResult>

    /** Batched unlimited moveDistinctIds for folded merges; zero moved rows for a source is not a failure. */
    moveDistinctIdsFromPersons(
        sources: InternalPerson[],
        target: InternalPerson,
        tx?: TransactionClient
    ): Promise<MoveDistinctIdsResult>

    /** Distinct-id counts per person id (single team), for the folded-merge limit pre-check. */
    countDistinctIdsForPersons(
        teamId: number,
        personIds: string[],
        tx?: TransactionClient
    ): Promise<Map<string, number>>

    fetchPersonDistinctIds(person: InternalPerson, limit?: number, tx?: TransactionClient): Promise<string[]>

    personPropertiesSize(personId: string, teamId: number): Promise<number>

    updateCohortsAndFeatureFlagsForMerge(
        teamID: Team['id'],
        sourcePersonID: InternalPerson['id'],
        targetPersonID: InternalPerson['id'],
        tx?: TransactionClient
    ): Promise<void>

    updateCohortsAndFeatureFlagsForMergeBatch(
        teamID: Team['id'],
        sourcePersonIDs: InternalPerson['id'][],
        targetPersonID: InternalPerson['id'],
        tx?: TransactionClient
    ): Promise<void>

    inRawTransaction<T>(description: string, transaction: (tx: TransactionClient) => Promise<T>): Promise<T>
}