Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

migration

Blue Mousedfd26bff926c
diff --git a/backend/migrations/clickhouse/2024_10_28_sort_of_working.js b/backend/migrations/clickhouse/2024_10_28.js
similarity index 95%
rename from backend/migrations/clickhouse/2024_10_28_sort_of_working.js
rename to backend/migrations/clickhouse/2024_10_28.js
index 6d8c8033e..593284003 100644
--- a/backend/migrations/clickhouse/2024_10_28_sort_of_working.js
+++ b/backend/migrations/clickhouse/2024_10_28.js
@@ -1,5 +1,7 @@
const { queriesRunner, dbName } = require('./setup')
+const SESSION_ID_OFFSET = '586912831'
+
// Generates insert queries from 2022-01-01 where psid is NULL.
const generateInsertQueries = () => {
const queries = []
@@ -19,7 +21,7 @@ const generateInsertQueries = () => {
WITH SessionStarts AS (
SELECT
*,
- 1000000 + dense_rank() OVER (
+ ${SESSION_ID_OFFSET} + dense_rank() OVER (
PARTITION BY toDate(created)
ORDER BY br, os, lc, cc, rg, ct, created
) AS session_id
@@ -88,7 +90,7 @@ const queries = [
// Step 3: Swap Tables
`RENAME TABLE ${dbName}.analytics TO ${dbName}.analytics_backup, ${dbName}.analytics_temp TO ${dbName}.analytics;`,
- // Step 4: Drop Backup Table (Optional)
+ // Step 4: Drop Backup Table
// `DROP TABLE ${dbName}.analytics_backup;`,
// Step 5: Drop the Obsolete 'unique' Column
diff --git a/backend/migrations/clickhouse/2024_10_28_broken.js b/backend/migrations/clickhouse/2024_10_28_broken.js
deleted file mode 100644
index 5f0e82c61..000000000
--- a/backend/migrations/clickhouse/2024_10_28_broken.js
+++ /dev/null
@@ -1,60 +0,0 @@
-const { queriesRunner, dbName } = require('./setup')
-
-const queries = [
- // Step 1: Create Temporary Table
- `
- CREATE TABLE IF NOT EXISTS ${dbName}.analytics_temp AS ${dbName}.analytics
- ENGINE = MergeTree()
- PARTITION BY toYYYYMM(created)
- ORDER BY (pid, created);
- `,
-
- // Step 2: Insert Data with Backfilled psid
- // TODO: Refactor it. It will fail if there are more than 50 million rows.
- `INSERT INTO ${dbName}.analytics_temp
- SELECT
- IF(psid IS NOT NULL, psid,
- dense_rank() OVER (
- PARTITION BY toDate(created), br, os, lc, cc, rg, ct
- ORDER BY created
- )
- ) AS psid,
- pid,
- pg,
- NULL as prev,
- dv,
- br,
- brv,
- os,
- osv,
- lc,
- ref,
- so,
- me,
- ca,
- te,
- co,
- cc,
- rg,
- ct,
- meta.key,
- meta.value,
- unique,
- created
- FROM ${dbName}.analytics`,
-
- // Step 3: Swap Tables
- `
- RENAME TABLE ${dbName}.analytics TO ${dbName}.analytics_backup, ${dbName}.analytics_temp TO ${dbName}.analytics;
- `,
-
- // Step 4: Drop Backup Table (Optional)
- // `
- // DROP TABLE ${dbName}.analytics_backup;
- // `,
-
- // Step 5: Drop the Obsolete `unique` Column
- // `ALTER TABLE ${dbName}.analytics DROP COLUMN IF EXISTS unique;`,
-]
-
-queriesRunner(queries, true)