From 73a6b2d1fa5e437fae28ca6ce2e7263631c8056d Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Sun, 25 Feb 2024 18:59:34 +0000 Subject: [PATCH] Fix data migration issues and improve code readability --- .../AddDurationColumnToSpanTable.ts | 1 - ...geLogSeverityColumnTypeFromTextToNumber.ts | 2 -- .../Workers/DataMigrations/Index.ts | 2 +- .../Services/AnalyticsDatabaseService.ts | 9 +++-- .../AnalyticsDatabase/StatementGenerator.ts | 33 ++++++++++--------- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/App/FeatureSet/Workers/DataMigrations/AddDurationColumnToSpanTable.ts b/App/FeatureSet/Workers/DataMigrations/AddDurationColumnToSpanTable.ts index 0637d51c3f..bea72f3558 100644 --- a/App/FeatureSet/Workers/DataMigrations/AddDurationColumnToSpanTable.ts +++ b/App/FeatureSet/Workers/DataMigrations/AddDurationColumnToSpanTable.ts @@ -9,7 +9,6 @@ export default class AddDurationColumnToSpanTable extends DataMigrationBase { } public override async migrate(): Promise { - const hasDurationColumn: boolean = await SpanService.doesColumnExistInDatabase('durationUnixNano'); diff --git a/App/FeatureSet/Workers/DataMigrations/ChangeLogSeverityColumnTypeFromTextToNumber.ts b/App/FeatureSet/Workers/DataMigrations/ChangeLogSeverityColumnTypeFromTextToNumber.ts index 86fc3f6f1d..d2479268d6 100644 --- a/App/FeatureSet/Workers/DataMigrations/ChangeLogSeverityColumnTypeFromTextToNumber.ts +++ b/App/FeatureSet/Workers/DataMigrations/ChangeLogSeverityColumnTypeFromTextToNumber.ts @@ -11,8 +11,6 @@ export default class ChangeLogSeverityColumnTypeFromTextToNumber extends DataMig public override async migrate(): Promise { - debugger; - const logSeverityNumberColumn: AnalyticsTableColumn | undefined = new Log().tableColumns.find((column: AnalyticsTableColumn) => { return column.key === 'severityNumber'; diff --git a/App/FeatureSet/Workers/DataMigrations/Index.ts b/App/FeatureSet/Workers/DataMigrations/Index.ts index 27facee029..a707085de6 100644 --- a/App/FeatureSet/Workers/DataMigrations/Index.ts +++ b/App/FeatureSet/Workers/DataMigrations/Index.ts @@ -42,7 +42,7 @@ const DataMigrations: Array = [ new AddStartDateToMonitorStatusTimeline(), new AddStartDateToScheduledEventsStateTimeline(), new AddDurationColumnToSpanTable(), - new ChangeLogSeverityColumnTypeFromTextToNumber() + new ChangeLogSeverityColumnTypeFromTextToNumber(), ]; export default DataMigrations; diff --git a/CommonServer/Services/AnalyticsDatabaseService.ts b/CommonServer/Services/AnalyticsDatabaseService.ts index c6122f75f2..576eb2ca90 100644 --- a/CommonServer/Services/AnalyticsDatabaseService.ts +++ b/CommonServer/Services/AnalyticsDatabaseService.ts @@ -108,7 +108,8 @@ export default class AnalyticsDatabaseService< return null; } - const statement: string = this.statementGenerator.getColumnTypesStatement(columnName); + const statement: string = + this.statementGenerator.getColumnTypesStatement(columnName); const dbResult: ExecResult = await this.execute(statement); @@ -116,7 +117,11 @@ export default class AnalyticsDatabaseService< dbResult.stream ); - return this.statementGenerator.toTableColumnType(strResult.trim()) as TableColumnType || null; + return ( + (this.statementGenerator.toTableColumnType( + strResult.trim() + ) as TableColumnType) || null + ); } public async countBy( diff --git a/CommonServer/Utils/AnalyticsDatabase/StatementGenerator.ts b/CommonServer/Utils/AnalyticsDatabase/StatementGenerator.ts index 127923f931..e89e85c2c2 100644 --- a/CommonServer/Utils/AnalyticsDatabase/StatementGenerator.ts +++ b/CommonServer/Utils/AnalyticsDatabase/StatementGenerator.ts @@ -451,9 +451,12 @@ export default class StatementGenerator { }; } - public getColumnTypesStatement(columnName: string): string { - return `SELECT type FROM system.columns WHERE table = '${this.model.tableName}' AND database = '${this.database.getDatasourceOptions().database}' AND name = '${columnName}'` + return `SELECT type FROM system.columns WHERE table = '${ + this.model.tableName + }' AND database = '${ + this.database.getDatasourceOptions().database + }' AND name = '${columnName}'`; } public async toRenameColumnStatement( @@ -520,20 +523,22 @@ export default class StatementGenerator { return columns; } - public toTableColumnType(clickhouseType: string): TableColumnType | undefined { + public toTableColumnType( + clickhouseType: string + ): TableColumnType | undefined { return { - 'String': TableColumnType.Text, - 'Int32': TableColumnType.Number, - 'Int64': TableColumnType.LongNumber, - 'Int128': TableColumnType.LongNumber, - 'Float32': TableColumnType.Decimal, - 'Float64': TableColumnType.Decimal, - 'DateTime': TableColumnType.Date, + String: TableColumnType.Text, + Int32: TableColumnType.Number, + Int64: TableColumnType.LongNumber, + Int128: TableColumnType.LongNumber, + Float32: TableColumnType.Decimal, + Float64: TableColumnType.Decimal, + DateTime: TableColumnType.Date, 'Array(String)': TableColumnType.ArrayText, 'Array(Int32)': TableColumnType.ArrayNumber, - 'JSON': TableColumnType.JSON, - 'Nested': TableColumnType.NestedModel, - 'Bool': TableColumnType.Boolean, + JSON: TableColumnType.JSON, + Nested: TableColumnType.NestedModel, + Bool: TableColumnType.Boolean, }[clickhouseType]; } @@ -576,8 +581,6 @@ export default class StatementGenerator { logger.info(`${this.model.tableName} Add Column Statement`); logger.info(statement); - debugger; - return statement; }