Skip to content

Commit a6b55b5

Browse files
authored
Preserve collation when changing column type (#3479)
Fixes #3476
1 parent 0212cae commit a6b55b5

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,15 @@ protected override void Generate(AlterColumnOperation operation, IModel? model,
456456
.Append("TYPE ")
457457
.Append(type);
458458

459-
if (newCollation != oldCollation)
459+
if (newCollation is not null)
460460
{
461-
builder.Append(" COLLATE ").Append(DelimitIdentifier(newCollation ?? "default"));
461+
builder.Append(" COLLATE ").Append(DelimitIdentifier(newCollation));
462+
}
463+
else if (type == oldType)
464+
{
465+
// If the type is the same, make it more explicit that we're just resetting the collation to the default
466+
// (this isn't really required)
467+
builder.Append(" COLLATE ").Append(DelimitIdentifier("default"));
462468
}
463469

464470
builder.AppendLine(";");

test/EFCore.PG.FunctionalTests/Migrations/MigrationsNpgsqlTest.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,9 +964,25 @@ await Test(
964964

965965
public override async Task Alter_column_change_type()
966966
{
967-
await base.Alter_column_change_type();
967+
await Test(
968+
builder => builder.Entity("People").Property<int>("Id"),
969+
builder => builder.Entity("People").Property<string>("SomeColumn")
970+
.HasColumnType("varchar")
971+
.UseCollation(NonDefaultCollation),
972+
builder => builder.Entity("People").Property<string>("SomeColumn")
973+
.HasColumnType("text")
974+
.UseCollation(NonDefaultCollation),
975+
model =>
976+
{
977+
var table = Assert.Single(model.Tables);
978+
var column = Assert.Single(table.Columns, c => c.Name == "SomeColumn");
979+
Assert.Equal(NonDefaultCollation, column.Collation);
980+
});
968981

969-
AssertSql("""ALTER TABLE "People" ALTER COLUMN "SomeColumn" TYPE bigint;""");
982+
AssertSql(
983+
"""
984+
ALTER TABLE "People" ALTER COLUMN "SomeColumn" TYPE text COLLATE "POSIX";
985+
""");
970986
}
971987

972988
public override async Task Alter_column_make_required()

0 commit comments

Comments
 (0)