Skip to content
This repository was archived by the owner on Nov 4, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
4949013
start with the postgres event ingestion process event tests
mariusandra Jan 28, 2021
1ab2d14
get first test to work
mariusandra Jan 28, 2021
4e4aad9
remove siteUrl
mariusandra Jan 28, 2021
4a80f46
pass partial event to test that it still works and retain parity with…
mariusandra Jan 28, 2021
ab5e534
another test
mariusandra Jan 28, 2021
0811e69
refactor
mariusandra Jan 28, 2021
8689965
Merge branch 'ingestion-save' into ingestion-save-tests
Twixes Jan 28, 2021
f3572ce
add more tests
mariusandra Jan 28, 2021
cdf7e4b
Merge branch 'ingestion-save-tests' of github.com:PostHog/posthog-plu…
mariusandra Jan 28, 2021
460b331
opt out of posthog in test mode
mariusandra Jan 28, 2021
6c8edf0
add first alias test
mariusandra Jan 28, 2021
4c27e2c
always use UTC times when talking to postgres
mariusandra Jan 28, 2021
2f6c1a7
prevent a crash
mariusandra Jan 28, 2021
a1b25d0
bit of clarity to help debug
mariusandra Jan 28, 2021
90d859f
fix bug with table name
mariusandra Jan 28, 2021
0b98683
fix bug with passing object instead of id
mariusandra Jan 28, 2021
5fab1e2
add some alias tests (all green now)
mariusandra Jan 28, 2021
60bf596
save merged properties
mariusandra Jan 28, 2021
9234925
few more tests
mariusandra Jan 28, 2021
e63a254
more missing tests
mariusandra Jan 28, 2021
0ad5c14
fix test
mariusandra Jan 28, 2021
f025566
team event properties test
mariusandra Jan 28, 2021
e4a069b
fix bug
mariusandra Jan 28, 2021
e478825
another test (partial)
mariusandra Jan 28, 2021
c2b9c7a
clarify postgres magic
mariusandra Jan 28, 2021
137dcde
different timestamp format for creating event in clickhouse & postgresql
mariusandra Jan 28, 2021
e788892
make element tests fail
mariusandra Jan 28, 2021
cc743cb
capture first team event test
mariusandra Jan 28, 2021
11b2746
insert session recording events
mariusandra Jan 28, 2021
13078d8
Merge branch 'ingestion-save-tests' of github.com:PostHog/posthog-plu…
mariusandra Jan 29, 2021
23a2a65
generate element hashes
mariusandra Jan 29, 2021
c2a7296
create elements and element groups
mariusandra Jan 29, 2021
8f429cd
"key in object" only works with objects, not arrays
mariusandra Jan 29, 2021
d44ace8
test an extra thing
mariusandra Jan 29, 2021
a5011ed
add missing awaits that caused things to be done out of order
mariusandra Jan 29, 2021
ead7882
few extra tests
mariusandra Jan 29, 2021
8a99133
another test
mariusandra Jan 29, 2021
f0d14e2
await for things to happen
mariusandra Jan 29, 2021
684558f
fix to work with latest master
mariusandra Jan 29, 2021
14dff2b
client is called twice - it's initialized for sending celery tasks on…
mariusandra Jan 29, 2021
61799ac
check webhook celery client queue
mariusandra Jan 29, 2021
9276db9
split into postgres & shared process event test
mariusandra Feb 1, 2021
576174b
add query counter
mariusandra Feb 1, 2021
dfaa2ad
clickhouse process event tests v0.1
mariusandra Feb 1, 2021
65d2152
Merge branch 'ingestion-save' into ingestion-save-tests
Twixes Feb 1, 2021
9049790
fix vm test
mariusandra Feb 1, 2021
a69f2cc
Merge branch 'ingestion-save-tests' of github.com:PostHog/posthog-plu…
mariusandra Feb 1, 2021
5eac357
Merge branch 'ingestion-save' into ingestion-save-tests
mariusandra Feb 1, 2021
13ca116
fix "Your test suite must contain at least one test." error for share…
mariusandra Feb 1, 2021
1bf4008
also run non-ingestion tests under test:postgres
mariusandra Feb 1, 2021
6aad714
Clean up utils.ts
Twixes Feb 1, 2021
a8c76af
Clean up TimestampFormat
Twixes Feb 1, 2021
6a353b6
Put get* type Postgres functions in DB class
Twixes Feb 1, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"main": "dist/src/index.js",
"scripts": {
"test": "jest --runInBand tests/**/*.test.ts",
"test:postgres": "yarn test --testPathIgnorePatterns '.*/clickhouse'",
"test:clickhouse": "yarn test --testPathIgnorePatterns '.*/postgres'",
"test:postgres": "jest --runInBand tests/postgres/*.test.ts tests/*.test.ts",
"test:clickhouse": "jest --runInBand tests/clickhouse/*.test.ts",
"benchmark": "node --expose-gc node_modules/.bin/jest --runInBand benchmarks/",
"start": "yarn start:dev",
"start:dist": "node dist/src/index.js --base-dir ../posthog",
Expand All @@ -23,7 +23,9 @@
"prettier": "prettier --write .",
"prettier:check": "prettier --check .",
"prepare": "yarn compile:protobuf",
"prepublishOnly": "yarn build"
"prepublishOnly": "yarn build",
"setup:dev": "cd ../posthog && (dropdb test_posthog || echo 'no db to drop') && createdb test_posthog && source env/bin/activate && DATABASE_URL=postgres://localhost:5432/test_posthog DEBUG=1 python manage.py migrate",
"setup:dev:ee": "export DEBUG=1 PRIMARY_DB=clickhouse DATABASE_URL=postgres://posthog:posthog@localhost:5439/test_posthog PGPASSWORD=posthog && cd ../posthog && (dropdb -p 5439 -h localhost -U posthog test_posthog || echo 'no db to drop') && createdb -p 5439 -h localhost -U posthog test_posthog && source env/bin/activate && python manage.py migrate && python manage.py migrate_clickhouse"
},
"bin": {
"posthog-plugin-server": "bin/posthog-plugin-server"
Expand Down
70 changes: 63 additions & 7 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import { DateTime } from 'luxon'
import { Pool, QueryConfig, QueryResult, QueryResultRow } from 'pg'
import { KAFKA_PERSON, KAFKA_PERSON_UNIQUE_ID } from './ingestion/topics'
import { unparsePersonPartial } from './ingestion/utils'
import { Person, PersonDistinctId, RawPerson, RawOrganization } from './types'
import { castTimestampOrNow, sanitizeSqlIdentifier } from './utils'
import {
Person,
PersonDistinctId,
RawPerson,
RawOrganization,
Team,
PostgresSessionRecordingEvent,
Event,
} from './types'
import { castTimestampOrNow, sanitizeSqlIdentifier, UUIDT } from './utils'

/** The recommended way of accessing the database. */
export class DB {
Expand All @@ -30,6 +38,13 @@ export class DB {
return this.postgres.query(queryTextOrConfig, values)
}

// Person

public async fetchPersons(): Promise<Person[]> {
const result = await this.postgresQuery('SELECT * FROM posthog_person')
return result.rows as Person[]
}

public async fetchPerson(teamId: number, distinctId: string): Promise<Person | undefined> {
const selectResult = await this.postgresQuery(
`SELECT
Expand All @@ -45,8 +60,10 @@ export class DB {
AND posthog_persondistinctid.distinct_id = $2`,
[teamId, distinctId]
)
const rawPerson: RawPerson = selectResult.rows[0]
return { ...rawPerson, created_at: DateTime.fromISO(rawPerson.created_at) }
if (selectResult.rows.length > 0) {
const rawPerson: RawPerson = selectResult.rows[0]
return { ...rawPerson, created_at: DateTime.fromISO(rawPerson.created_at) }
}
}

public async createPerson(
Expand All @@ -55,7 +72,8 @@ export class DB {
teamId: number,
isUserId: number | null,
isIdentified: boolean,
uuid: string
uuid: string,
distinctIds?: string[]
): Promise<Person> {
const insertResult = await this.postgresQuery(
'INSERT INTO posthog_person (created_at, properties, team_id, is_user_id, is_identified, uuid) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *',
Expand All @@ -75,16 +93,22 @@ export class DB {
messages: [{ value: Buffer.from(JSON.stringify(data)) }],
})
}

for (const distinctId of distinctIds || []) {
await this.addDistinctId(personCreated, distinctId)
}

return personCreated
}

public async updatePerson(person: Person, update: Partial<Person>): Promise<Person> {
const updatedPerson: Person = { ...person, ...update }
const values = [...Object.values(unparsePersonPartial(update)), person.id]
await this.postgresQuery(
`UPDATE posthog_person SET ${Object.keys(update).map(
(field, index) => sanitizeSqlIdentifier(field) + ' = $' + (index + 1)
)} WHERE id = $${Object.values(update).length + 1}`,
[...Object.values(unparsePersonPartial(update)), person.id]
values
)
if (this.kafkaProducer) {
const data = {
Expand All @@ -103,7 +127,7 @@ export class DB {
}

public async deletePerson(personId: number): Promise<void> {
await this.postgresQuery('DELETE FROM person_distinct_id WHERE person_id = $1', [personId])
await this.postgresQuery('DELETE FROM posthog_persondistinctid WHERE person_id = $1', [personId])
await this.postgresQuery('DELETE FROM posthog_person WHERE id = $1', [personId])
if (this.clickhouse) {
await this.clickhouse.query(`ALTER TABLE person DELETE WHERE id = ${personId}`).toPromise()
Expand All @@ -113,6 +137,16 @@ export class DB {
}
}

// PersonDistinctId

public async fetchDistinctIdValues(person: Person): Promise<string[]> {
const result = await this.postgresQuery(
'SELECT * FROM posthog_persondistinctid WHERE person_id=$1 and team_id=$2 ORDER BY id',
[person.id, person.team_id]
)
return (result.rows as PersonDistinctId[]).map((pdi) => pdi.distinct_id)
}

public async addDistinctId(person: Person, distinctId: string): Promise<void> {
const insertResult = await this.postgresQuery(
'INSERT INTO posthog_persondistinctid (distinct_id, person_id, team_id) VALUES ($1, $2, $3) RETURNING *',
Expand Down Expand Up @@ -146,11 +180,33 @@ export class DB {
}
}

// Organization

public async fetchOrganization(organizationId: string): Promise<RawOrganization | undefined> {
const selectResult = await this.postgresQuery(`SELECT * FROM posthog_organization WHERE id $1`, [
organizationId,
])
const rawOrganization: RawOrganization = selectResult.rows[0]
return rawOrganization
}

// Event

public async fetchEvents(): Promise<Event[]> {
const result = await this.postgresQuery('SELECT * FROM posthog_event')
return result.rows as Event[]
}

// SessionRecordingEvent

public async fetchSessionRecordingEvents(): Promise<PostgresSessionRecordingEvent[]> {
const result = await this.postgresQuery('SELECT * FROM posthog_sessionrecordingevent')
return result.rows as PostgresSessionRecordingEvent[]
}

// Element

public async fetchElements(): Promise<Element[]> {
return (await this.postgresQuery('SELECT * FROM posthog_element')).rows
}
}
Loading