Fix data migration issues and improve code readability

This commit is contained in:
Simon Larsen 2024-02-25 18:59:34 +00:00
parent 625a2c410c
commit 73a6b2d1fa
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
5 changed files with 26 additions and 21 deletions

View File

@ -9,7 +9,6 @@ export default class AddDurationColumnToSpanTable extends DataMigrationBase {
}
public override async migrate(): Promise<void> {
const hasDurationColumn: boolean =
await SpanService.doesColumnExistInDatabase('durationUnixNano');

View File

@ -11,8 +11,6 @@ export default class ChangeLogSeverityColumnTypeFromTextToNumber extends DataMig
public override async migrate(): Promise<void> {
debugger;
const logSeverityNumberColumn: AnalyticsTableColumn | undefined =
new Log().tableColumns.find((column: AnalyticsTableColumn) => {
return column.key === 'severityNumber';

View File

@ -42,7 +42,7 @@ const DataMigrations: Array<DataMigrationBase> = [
new AddStartDateToMonitorStatusTimeline(),
new AddStartDateToScheduledEventsStateTimeline(),
new AddDurationColumnToSpanTable(),
new ChangeLogSeverityColumnTypeFromTextToNumber()
new ChangeLogSeverityColumnTypeFromTextToNumber(),
];
export default DataMigrations;

View File

@ -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<Stream> = 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(

View File

@ -451,9 +451,12 @@ export default class StatementGenerator<TBaseModel extends AnalyticsBaseModel> {
};
}
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<TBaseModel extends AnalyticsBaseModel> {
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<TBaseModel extends AnalyticsBaseModel> {
logger.info(`${this.model.tableName} Add Column Statement`);
logger.info(statement);
debugger;
return statement;
}