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!

Drop sdur and move to session_durations table

Blue Mouseeeaa29bc7ce3
diff --git a/backend/migrations/clickhouse/2024_10_25.js b/backend/migrations/clickhouse/2024_10_25.js
new file mode 100644
index 000000000..fddcd1753
--- /dev/null
+++ b/backend/migrations/clickhouse/2024_10_25.js
@@ -0,0 +1,30 @@
+const { queriesRunner, dbName } = require('./setup')
+
+const queries = [
+ // Create new session_durations table
+ `CREATE TABLE IF NOT EXISTS ${dbName}.session_durations
+ (
+ psid UInt64,
+ pid FixedString(12),
+ duration UInt32
+ )
+ ENGINE = MergeTree()
+ ORDER BY (pid, psid);`,
+
+ // Copy existing session durations to new table
+ `INSERT INTO ${dbName}.session_durations (psid, pid, duration)
+ SELECT
+ psid,
+ pid,
+ sdur as duration
+ FROM ${dbName}.analytics
+ WHERE
+ sdur IS NOT NULL
+ AND psid IS NOT NULL
+ AND unique = 1;`,
+
+ // Remove sdur column from analytics table
+ `ALTER TABLE ${dbName}.analytics DROP COLUMN IF EXISTS sdur;`,
+]
+
+queriesRunner(queries)
diff --git a/backend/migrations/clickhouse/initialise_database.js b/backend/migrations/clickhouse/initialise_database.js
index a68af848a..eac121cf6 100644
--- a/backend/migrations/clickhouse/initialise_database.js
+++ b/backend/migrations/clickhouse/initialise_database.js
@@ -31,7 +31,6 @@ const CLICKHOUSE_INIT_QUERIES = [
key String,
value String
),
- sdur Nullable(UInt32),
unique UInt8,
created DateTime('UTC')
)
@@ -39,6 +38,16 @@ const CLICKHOUSE_INIT_QUERIES = [
PARTITION BY toYYYYMM(created)
ORDER BY (pid, created);`,
+ // Session duration table
+ `CREATE TABLE IF NOT EXISTS ${dbName}.session_durations
+ (
+ psid UInt64,
+ pid FixedString(12),
+ duration UInt32
+ )
+ ENGINE = MergeTree()
+ ORDER BY (pid, psid);`,
+
// Custom events table
`CREATE TABLE IF NOT EXISTS ${dbName}.customEV
(