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.

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

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

export interface PersonRepositoryTransaction {
    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>

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

    deletePerson(person: InternalPerson): Promise<PersonMessage[]>

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

    /** See PersonRepository.claimLifecycleMarks; the marks are held until this transaction commits. */
    claimLifecycleMarks(opId: string, teamId: number, persons: LifecycleMarkPerson[]): Promise<void>

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

    /** See PersonRepository.isPersonLive; only meaningful while holding the person's mark. */
    isPersonLive(person: InternalPerson): Promise<boolean>

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

    /** The live person that owns the distinct id, read on this transaction without a row lock. */
    readPerson(teamId: Team['id'], distinctId: string): Promise<InternalPerson | undefined>

    /** See PersonRepository.updatePersonsBatch; the rows are written under this transaction. */
    updatePersonsBatch(personUpdates: PersonUpdate[]): Promise<
        Map<
            string,
            {
                success: boolean
                version?: number
                kafkaMessage?: PersonMessage
                person?: InternalPerson
                error?: Error
            }
        >
    >

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

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

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

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

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

    updateCohortsAndFeatureFlagsForMerge(
        teamId: Team['id'],
        sourcePersonID: InternalPerson['id'],
        targetPersonID: InternalPerson['id']
    ): Promise<void>

    updateCohortsAndFeatureFlagsForMergeBatch(
        teamId: Team['id'],
        sourcePersonIDs: InternalPerson['id'][],
        targetPersonID: InternalPerson['id']
    ): Promise<void>
}