From 3272b87e10fef38b0787a6260bb4537eb989e5e1 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Wed, 27 Jul 2022 23:11:03 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/smartclickhouse.classes.timedatatable.ts | 51 +++++++++++---------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 92326c3..6e8f058 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@pushrocks/smartclickhouse', - version: '2.0.5', + version: '2.0.6', description: 'an odm for talking to clickhouse' } diff --git a/ts/smartclickhouse.classes.timedatatable.ts b/ts/smartclickhouse.classes.timedatatable.ts index 8748587..5921128 100644 --- a/ts/smartclickhouse.classes.timedatatable.ts +++ b/ts/smartclickhouse.classes.timedatatable.ts @@ -38,29 +38,7 @@ export class TimeDataTable { retainDataForDays: 30 }); - // create table in clickhouse - await smartClickHouseDbRefArg.clickhouseClient - .queryPromise(` - CREATE TABLE IF NOT EXISTS ${newTable.options.tableName} ( - timestamp DateTime64(3, 'Europe/Berlin'), - message String - ) ENGINE=MergeTree() ORDER BY timestamp`); - - // lets adjust the TTL - await smartClickHouseDbRefArg.clickhouseClient - .queryPromise(` - ALTER TABLE ${newTable.options.tableName} MODIFY TTL toDateTime(timestamp) + INTERVAL ${newTable.options.retainDataForDays} DAY - `); - - await newTable.updateColumns(); - console.log(`=======================`) - console.log( - `table with name "${newTable.options.tableName}" in databse ${newTable.smartClickHouseDbRef.options.database} has the following columns:` - ); - for (const column of newTable.columns) { - console.log(`>> ${column.name}: ${column.type}`); - } - console.log('^^^^^^^^^^^^^^\n'); + await newTable.setup(); return newTable; } @@ -75,6 +53,32 @@ export class TimeDataTable { this.options = optionsArg; } + public async setup() { + // create table in clickhouse + await this.smartClickHouseDbRef.clickhouseClient + .queryPromise(` + CREATE TABLE IF NOT EXISTS ${this.options.tableName} ( + timestamp DateTime64(3, 'Europe/Berlin'), + message String + ) ENGINE=MergeTree() ORDER BY timestamp`); + + // lets adjust the TTL + await this.smartClickHouseDbRef.clickhouseClient + .queryPromise(` + ALTER TABLE ${this.options.tableName} MODIFY TTL toDateTime(timestamp) + INTERVAL ${this.options.retainDataForDays} DAY + `); + + await this.updateColumns(); + console.log(`=======================`) + console.log( + `table with name "${this.options.tableName}" in database ${this.smartClickHouseDbRef.options.database} has the following columns:` + ); + for (const column of this.columns) { + console.log(`>> ${column.name}: ${column.type}`); + } + console.log('^^^^^^^^^^^^^^\n'); + } + public columns: IColumnInfo[] = []; /** @@ -174,6 +178,7 @@ export class TimeDataTable { console.log(`Ran into an error. Trying to set up things properly again.`); await this.smartClickHouseDbRef.pingDatabaseUntilAvailable(); await this.smartClickHouseDbRef.createDatabase(); + await this.setup(); this.columns = []; this.healingDeferred.resolve(); this.healingDeferred = null;