0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-28 18:26:15 +01:00
posthog/ee/clickhouse/migrations/0016_collapsing_person_distinct_id.py
Karl-Aksel Puulmann ba405c823c
Add snapshot tests for clickhouse table schemas (#7572)
* Add a comment to keep topics in sync

* Clean up code relating to table engines

* Add snapshots for table creation queries

* Remove optional import

* Add snapshot tests for CLICKHOUSE_REPLICATION schemas

Note that these are out of sync with cloud in most cases

* Add another warning comment

* Improve naming
2021-12-08 16:07:34 +02:00

36 lines
1.3 KiB
Python

from infi.clickhouse_orm import migrations
from ee.clickhouse.sql.person import *
from posthog.settings import CLICKHOUSE_CLUSTER
TEMPORARY_TABLE_NAME = "person_distinct_id_tmp_migration_0016"
operations = [
migrations.RunSQL(PERSONS_DISTINCT_ID_TABLE_SQL().replace(PERSONS_DISTINCT_ID_TABLE, TEMPORARY_TABLE_NAME, 1)),
migrations.RunSQL(f"DROP TABLE person_distinct_id_mv ON CLUSTER {CLICKHOUSE_CLUSTER}"),
migrations.RunSQL(f"DROP TABLE kafka_person_distinct_id ON CLUSTER {CLICKHOUSE_CLUSTER}"),
migrations.RunSQL(
f"""
INSERT INTO {TEMPORARY_TABLE_NAME} (distinct_id, person_id, team_id, _sign, _timestamp, _offset)
SELECT
distinct_id,
person_id,
team_id,
if(is_deleted==0, 1, -1) as _sign,
_timestamp,
_offset
FROM {PERSONS_DISTINCT_ID_TABLE}
"""
),
migrations.RunSQL(
f"""
RENAME TABLE
{CLICKHOUSE_DATABASE}.{PERSONS_DISTINCT_ID_TABLE} to {CLICKHOUSE_DATABASE}.person_distinct_id_backup,
{CLICKHOUSE_DATABASE}.{TEMPORARY_TABLE_NAME} to {CLICKHOUSE_DATABASE}.{PERSONS_DISTINCT_ID_TABLE}
ON CLUSTER {CLICKHOUSE_CLUSTER}
"""
),
migrations.RunSQL(KAFKA_PERSONS_DISTINCT_ID_TABLE_SQL),
migrations.RunSQL(PERSONS_DISTINCT_ID_TABLE_MV_SQL),
]