Skip to content

Commit e327d6b

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

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
@@ -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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,30 @@ public override async Task Alter_column_change_type()
969969
AssertSql("""ALTER TABLE "People" ALTER COLUMN "SomeColumn" TYPE bigint;""");
970970
}
971971

972+
[Fact]
973+
public virtual async Task Alter_column_change_type_preserves_collation()
974+
{
975+
await Test(
976+
builder => builder.Entity("People").Property<int>("Id"),
977+
builder => builder.Entity("People").Property<string>("SomeColumn")
978+
.HasColumnType("varchar")
979+
.UseCollation(NonDefaultCollation),
980+
builder => builder.Entity("People").Property<string>("SomeColumn")
981+
.HasColumnType("text")
982+
.UseCollation(NonDefaultCollation),
983+
model =>
984+
{
985+
var table = Assert.Single(model.Tables);
986+
var column = Assert.Single(table.Columns, c => c.Name == "SomeColumn");
987+
Assert.Equal(NonDefaultCollation, column.Collation);
988+
});
989+
990+
AssertSql(
991+
"""
992+
ALTER TABLE "People" ALTER COLUMN "SomeColumn" TYPE text COLLATE "POSIX";
993+
""");
994+
}
995+
972996
public override async Task Alter_column_make_required()
973997
{
974998
await base.Alter_column_make_required();

0 commit comments

Comments
 (0)