| | | 1 | | using Microsoft.EntityFrameworkCore.Migrations; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Persistence.EFCore.PostgreSql; |
| | | 4 | | |
| | | 5 | | public static class MigrationHelper |
| | | 6 | | { |
| | | 7 | | public static void AlterColumnDateTime(MigrationBuilder migrationBuilder, IElsaDbContextSchema schema, string tableN |
| | | 8 | | { |
| | 0 | 9 | | migrationBuilder.AddColumn<DateTimeOffset>( |
| | 0 | 10 | | name: $"{columnName}Temp", |
| | 0 | 11 | | schema: schema.Schema, |
| | 0 | 12 | | table: tableName, |
| | 0 | 13 | | type: "timestamptz", |
| | 0 | 14 | | nullable: true); |
| | | 15 | | |
| | 0 | 16 | | migrationBuilder.Sql( |
| | 0 | 17 | | $""" |
| | 0 | 18 | | UPDATE "{schema.Schema}"."{tableName}" |
| | 0 | 19 | | SET "{columnName}Temp" = to_timestamp("{columnName}", 'YYYY-MM-DD HH24:MI:SS.US') AT TIME ZONE 'UTC' |
| | 0 | 20 | | WHERE "{columnName}" IS NOT NULL |
| | 0 | 21 | | """); |
| | | 22 | | |
| | 0 | 23 | | migrationBuilder.DropColumn( |
| | 0 | 24 | | name: columnName, |
| | 0 | 25 | | schema: schema.Schema, |
| | 0 | 26 | | table: tableName); |
| | | 27 | | |
| | 0 | 28 | | migrationBuilder.RenameColumn( |
| | 0 | 29 | | name: $"{columnName}Temp", |
| | 0 | 30 | | schema: schema.Schema, |
| | 0 | 31 | | table: tableName, |
| | 0 | 32 | | newName: columnName); |
| | | 33 | | |
| | 0 | 34 | | migrationBuilder.AlterColumn<DateTimeOffset>( |
| | 0 | 35 | | name: columnName, |
| | 0 | 36 | | schema: schema.Schema, |
| | 0 | 37 | | table: tableName, |
| | 0 | 38 | | type: "timestamptz", |
| | 0 | 39 | | nullable: nullable, |
| | 0 | 40 | | oldClrType: typeof(DateTimeOffset), |
| | 0 | 41 | | oldNullable: nullable); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | public static void AlterColumnBoolean(MigrationBuilder migrationBuilder, IElsaDbContextSchema schema, string tableNa |
| | | 45 | | { |
| | | 46 | | // Step 1: Add a new temporary nullable column |
| | 0 | 47 | | migrationBuilder.AddColumn<bool>( |
| | 0 | 48 | | name: $"{columnName}Temp", |
| | 0 | 49 | | schema: schema.Schema, |
| | 0 | 50 | | table: tableName, |
| | 0 | 51 | | type: "boolean", |
| | 0 | 52 | | nullable: nullable); |
| | | 53 | | |
| | | 54 | | // Step 2: Update the temporary column with converted values from the original column |
| | 0 | 55 | | migrationBuilder.Sql( |
| | 0 | 56 | | $""" |
| | 0 | 57 | | UPDATE "{schema.Schema}"."{tableName}" |
| | 0 | 58 | | SET "{columnName}Temp" = CASE WHEN "{columnName}" = 1 THEN TRUE ELSE FALSE END |
| | 0 | 59 | | WHERE "{columnName}" IS NOT NULL |
| | 0 | 60 | | """); |
| | | 61 | | |
| | | 62 | | // Step 3: Drop the original column |
| | 0 | 63 | | migrationBuilder.DropColumn( |
| | 0 | 64 | | name: columnName, |
| | 0 | 65 | | schema: schema.Schema, |
| | 0 | 66 | | table: tableName); |
| | | 67 | | |
| | | 68 | | // Step 4: Rename the temporary column to the original column name |
| | 0 | 69 | | migrationBuilder.RenameColumn( |
| | 0 | 70 | | name: $"{columnName}Temp", |
| | 0 | 71 | | schema: schema.Schema, |
| | 0 | 72 | | table: tableName, |
| | 0 | 73 | | newName: columnName); |
| | | 74 | | |
| | | 75 | | // Step 5: Alter the new column to be non-nullable |
| | 0 | 76 | | migrationBuilder.AlterColumn<bool>( |
| | 0 | 77 | | name: columnName, |
| | 0 | 78 | | schema: schema.Schema, |
| | 0 | 79 | | table: tableName, |
| | 0 | 80 | | type: "boolean", |
| | 0 | 81 | | nullable: nullable, |
| | 0 | 82 | | oldClrType: typeof(bool), |
| | 0 | 83 | | oldNullable: true); |
| | 0 | 84 | | } |
| | | 85 | | } |