< Summary

Information
Class: Elsa.Persistence.EFCore.PostgreSql.MigrationHelper
Assembly: Elsa.Persistence.EFCore.PostgreSql
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.PostgreSql/MigrationHelper.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 60
Coverable lines: 60
Total lines: 85
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AlterColumnDateTime(...)100%210%
AlterColumnBoolean(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.PostgreSql/MigrationHelper.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore.Migrations;
 2
 3namespace Elsa.Persistence.EFCore.PostgreSql;
 4
 5public static class MigrationHelper
 6{
 7    public static void AlterColumnDateTime(MigrationBuilder migrationBuilder, IElsaDbContextSchema schema, string tableN
 8    {
 09        migrationBuilder.AddColumn<DateTimeOffset>(
 010            name: $"{columnName}Temp",
 011            schema: schema.Schema,
 012            table: tableName,
 013            type: "timestamptz",
 014            nullable: true);
 15
 016        migrationBuilder.Sql(
 017            $"""
 018             UPDATE "{schema.Schema}"."{tableName}"
 019             SET "{columnName}Temp" = to_timestamp("{columnName}", 'YYYY-MM-DD HH24:MI:SS.US') AT TIME ZONE 'UTC'
 020             WHERE "{columnName}" IS NOT NULL
 021             """);
 22
 023        migrationBuilder.DropColumn(
 024            name: columnName,
 025            schema: schema.Schema,
 026            table: tableName);
 27
 028        migrationBuilder.RenameColumn(
 029            name: $"{columnName}Temp",
 030            schema: schema.Schema,
 031            table: tableName,
 032            newName: columnName);
 33
 034        migrationBuilder.AlterColumn<DateTimeOffset>(
 035            name: columnName,
 036            schema: schema.Schema,
 037            table: tableName,
 038            type: "timestamptz",
 039            nullable: nullable,
 040            oldClrType: typeof(DateTimeOffset),
 041            oldNullable: nullable);
 042    }
 43
 44    public static void AlterColumnBoolean(MigrationBuilder migrationBuilder, IElsaDbContextSchema schema, string tableNa
 45    {
 46        // Step 1: Add a new temporary nullable column
 047        migrationBuilder.AddColumn<bool>(
 048            name: $"{columnName}Temp",
 049            schema: schema.Schema,
 050            table: tableName,
 051            type: "boolean",
 052            nullable: nullable);
 53
 54        // Step 2: Update the temporary column with converted values from the original column
 055        migrationBuilder.Sql(
 056            $"""
 057             UPDATE "{schema.Schema}"."{tableName}"
 058             SET "{columnName}Temp" = CASE WHEN "{columnName}" = 1 THEN TRUE ELSE FALSE END
 059             WHERE "{columnName}" IS NOT NULL
 060             """);
 61
 62        // Step 3: Drop the original column
 063        migrationBuilder.DropColumn(
 064            name: columnName,
 065            schema: schema.Schema,
 066            table: tableName);
 67
 68        // Step 4: Rename the temporary column to the original column name
 069        migrationBuilder.RenameColumn(
 070            name: $"{columnName}Temp",
 071            schema: schema.Schema,
 072            table: tableName,
 073            newName: columnName);
 74
 75        // Step 5: Alter the new column to be non-nullable
 076        migrationBuilder.AlterColumn<bool>(
 077            name: columnName,
 078            schema: schema.Schema,
 079            table: tableName,
 080            type: "boolean",
 081            nullable: nullable,
 082            oldClrType: typeof(bool),
 083            oldNullable: true);
 084    }
 85}