Skip to content

Commit 9f4b731

Browse files
committed
Preserve collation when changing column type (#3479)
Fixes #3476 (cherry picked from commit a6b55b5)
1 parent 0fdf784 commit 9f4b731

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
@@ -967,9 +967,25 @@ await Test(
967967

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

972-
AssertSql("""ALTER TABLE "People" ALTER COLUMN "SomeColumn" TYPE bigint;""");
985+
AssertSql(
986+
"""
987+
ALTER TABLE "People" ALTER COLUMN "SomeColumn" TYPE text COLLATE "POSIX";
988+
""");
973989
}
974990

975991
public override async Task Alter_column_make_required()

0 commit comments

Comments
 (0)