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!

added migrations runner & bumped clickhouse to 2.6.0

Blue Mouse91154201e750
diff --git a/migrations/clickhouse/2022_12_24.js b/migrations/clickhouse/2022_12_24.js
new file mode 100644
index 000000000..1ccd33a3d
--- /dev/null
+++ b/migrations/clickhouse/2022_12_24.js
@@ -0,0 +1,54 @@
+const { queriesRunner, dbName } = require('./setup')
+
+const queries = [
+ `CREATE TABLE IF NOT EXISTS ${dbName}.analytics_temp
+ (
+ sId Nullable(String),
+ pid FixedString(12),
+ pg Nullable(String),
+ dv Nullable(String),
+ br Nullable(String),
+ os Nullable(String),
+ lc Nullable(String),
+ ref Nullable(String),
+ so Nullable(String),
+ me Nullable(String),
+ ca Nullable(String),
+ lt Nullable(UInt16),
+ cc Nullable(FixedString(2)),
+ sdur Nullable(UInt32),
+ unique UInt8,
+ created DateTime
+ )
+ ENGINE = MergeTree()
+ PARTITION BY toYYYYMM(created)
+ ORDER BY (pid, created);`,
+
+ `INSERT INTO ${dbName}.analytics_temp (
+ NULL,
+ SELECT
+ sId,
+ pid,
+ pg,
+ dv,
+ br,
+ os,
+ lc,
+ ref,
+ so,
+ me,
+ ca,
+ lt,
+ cc
+ FROM ${dbName}.analytics,
+ NULL,
+ SELECT
+ unique,
+ created
+ FROM ${dbName}.analytics)`,
+
+ `DROP TABLE ${dbName}.analytics`,
+ `RENAME TABLE ${dbName}.analytics_temp TO ${dbName}.analytics`,
+]
+
+queriesRunner(queries)
diff --git a/migrations/clickhouse/setup.js b/migrations/clickhouse/setup.js
new file mode 100644
index 000000000..30f18a44c
--- /dev/null
+++ b/migrations/clickhouse/setup.js
@@ -0,0 +1,44 @@
+const { ClickHouse } = require('clickhouse')
+const _ = require('lodash')
+require('dotenv').config()
+
+const clickhouse = new ClickHouse({
+ url: process.env.CLICKHOUSE_HOST,
+ port: _.toNumber(process.env.CLICKHOUSE_PORT),
+ debug: false,
+ basicAuth: {
+ username: process.env.CLICKHOUSE_USER,
+ password: process.env.CLICKHOUSE_PASSWORD,
+ },
+ isUseGzip: false,
+ format: 'json',
+ raw: false,
+ config: {
+ session_timeout: 60,
+ output_format_json_quote_64bit_integers: 0,
+ enable_http_compression: 0,
+ database: process.env.CLICKHOUSE_DATABASE,
+ },
+})
+
+const queriesRunner = async (queries) => {
+ for (const query of queries) {
+ if (query) {
+ try {
+ await clickhouse.query(query).toPromise()
+ console.log(`Query OK: ${query}`)
+ } catch (error) {
+ console.error(`Query ERROR: ${query}`)
+ console.error(error)
+ }
+ }
+ }
+}
+
+const dbName = process.env.CLICKHOUSE_DATABASE
+
+module.exports = {
+ clickhouse,
+ queriesRunner,
+ dbName,
+}
\ No newline at end of file
diff --git a/package.json b/package.json
index 49995e287..ffe3b9400 100644
--- a/package.json
+++ b/package.json
@@ -38,7 +38,7 @@
"body-parser": "^1.20.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
- "clickhouse": "^2.5.0",
+ "clickhouse": "^2.6.0",
"cookie-parser": "^1.4.6",
"countries-and-timezones": "^3.3.0",
"cross-env": "^7.0.3",