Skip to content

Commit

Permalink
feat: setup clickhouse migration tool + update metrics schema (add `i…
Browse files Browse the repository at this point in the history
…s_delta` + `is_monotonic` fields) (#138)
  • Loading branch information
wrn14897 committed Dec 3, 2023
1 parent 2fdf815 commit 8c8c476
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/little-colts-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hyperdx/api': minor
'@hyperdx/app': minor
---

feat: add is_delta + is_monotonic fields to metric_stream table (REQUIRES DB
MIGRATION)
6 changes: 6 additions & 0 deletions .changeset/purple-trains-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperdx/api': patch
'@hyperdx/app': patch
---

feat: setup clickhouse migration tool
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ dev-unit:
ci-unit:
npx nx run-many -t ci:unit

# TODO: check db connections before running the migration CLIs
.PHONY: dev-migrate-db
dev-migrate-db:
@echo "Migrating Mongo db...\n"
npx nx run @hyperdx/api:dev:migrate-db
@echo "Migrating ClickHouse db...\n"
npx nx run @hyperdx/api:dev:migrate-ch

.PHONY: build-local
build-local:
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ the `.env` file. After making your changes, rebuild images with

**DB Migration**

Before running the migration, you'll need to install the migration tools:

1. Install local dependencies with `make`.
2. Install
[golang-migrate](https://github.com/golang-migrate/migrate/tree/master/cmd/migrate)
CLI.

You can initiate the DB migration process by executing `make dev-migrate-db`.
This will run the migration scripts in `/packages/api/migrations` against the
local DB.
Expand Down
2 changes: 1 addition & 1 deletion packages/api/migrate-mongo-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export = {
},

// The migrations dir, can be an relative or absolute path. Only edit this when really necessary.
migrationsDir: 'migrations',
migrationsDir: 'migrations/mongo',

// The mongodb collection where the applied changes are stored. Only edit this when really necessary.
changelogCollectionName: 'changelog',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE default.metric_stream DROP COLUMN is_delta;

ALTER TABLE default.metric_stream DROP COLUMN is_monotonic;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE default.metric_stream ADD COLUMN is_delta Boolean CODEC(Delta, ZSTD(1));

ALTER TABLE default.metric_stream ADD COLUMN is_monotonic Boolean CODEC(Delta, ZSTD(1));
4 changes: 3 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
"ci:int": "jest --runInBand --ci --forceExit --coverage",
"dev:int": "jest --watchAll --runInBand --detectOpenHandles",
"dev:migrate-db-create": "ts-node node_modules/.bin/migrate-mongo create -f migrate-mongo-config.ts",
"dev:migrate-db": "ts-node node_modules/.bin/migrate-mongo up -f migrate-mongo-config.ts"
"dev:migrate-db": "ts-node node_modules/.bin/migrate-mongo up -f migrate-mongo-config.ts",
"dev:migrate-ch-create": "migrate create -ext sql -dir ./migrations/ch -seq",
"dev:migrate-ch": "migrate -database 'clickhouse://localhost:9000?database=default&x-multi-statement=true' -path ./migrations/ch up"
}
}
2 changes: 2 additions & 0 deletions packages/api/src/clickhouse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ export const connect = async () => {
value Float64 CODEC(ZSTD(1)),
flags UInt32 CODEC(ZSTD(1)),
unit String CODEC(ZSTD(1)),
is_delta Boolean CODEC(Delta, ZSTD(1)),
is_monotonic Boolean CODEC(Delta, ZSTD(1)),
_string_attributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
_created_at DateTime64(9, 'UTC') DEFAULT toDateTime64(now(), 9) CODEC(Delta(8), ZSTD(1)),
_timestamp_sort_key Int64 MATERIALIZED toUnixTimestamp64Nano(coalesce(timestamp, _created_at)),
Expand Down

0 comments on commit 8c8c476

Please sign in to comment.