Skip to content

Commit 9c747fd

Browse files
committed
Preserve collation when changing column type (#3479)
Fixes #3476 (cherry picked from commit e327d6b)
1 parent e46e43e commit 9c747fd

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs

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

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

462468
builder.AppendLine(";");

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,30 @@ public override async Task Alter_column_change_type()
10471047
""");
10481048
}
10491049

1050+
[Fact]
1051+
public virtual async Task Alter_column_change_type_preserves_collation()
1052+
{
1053+
await Test(
1054+
builder => builder.Entity("People").Property<int>("Id"),
1055+
builder => builder.Entity("People").Property<string>("SomeColumn")
1056+
.HasColumnType("varchar")
1057+
.UseCollation(NonDefaultCollation),
1058+
builder => builder.Entity("People").Property<string>("SomeColumn")
1059+
.HasColumnType("text")
1060+
.UseCollation(NonDefaultCollation),
1061+
model =>
1062+
{
1063+
var table = Assert.Single(model.Tables);
1064+
var column = Assert.Single(table.Columns, c => c.Name == "SomeColumn");
1065+
Assert.Equal(NonDefaultCollation, column.Collation);
1066+
});
1067+
1068+
AssertSql(
1069+
"""
1070+
ALTER TABLE "People" ALTER COLUMN "SomeColumn" TYPE text COLLATE "POSIX";
1071+
""");
1072+
}
1073+
10501074
public override async Task Alter_column_make_required()
10511075
{
10521076
await base.Alter_column_make_required();

0 commit comments

Comments
 (0)