From f0e310671a90f5d42d9e803edaa87fea75fb55fc Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Wed, 5 Jul 2023 10:40:24 +0200 Subject: [PATCH] Fix migration for SQL Server persisted column with collation (#31182) --- .../Migrations/SqlServerMigrationsSqlGenerator.cs | 10 +++++----- .../Migrations/MigrationsTestBase.cs | 9 ++++++--- .../Migrations/MigrationsSqlServerTest.cs | 10 +++++----- .../Migrations/MigrationsSqliteTest.cs | 10 +++++----- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs index e2993ce5fe4..ded9236cb74 100644 --- a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs +++ b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs @@ -1674,17 +1674,17 @@ protected override void ComputedColumnDefinition( .Append(" AS ") .Append(operation.ComputedColumnSql!); - if (operation.IsStored == true) - { - builder.Append(" PERSISTED"); - } - if (operation.Collation != null) { builder .Append(" COLLATE ") .Append(operation.Collation); } + + if (operation.IsStored == true) + { + builder.Append(" PERSISTED"); + } } /// diff --git a/test/EFCore.Relational.Specification.Tests/Migrations/MigrationsTestBase.cs b/test/EFCore.Relational.Specification.Tests/Migrations/MigrationsTestBase.cs index 9207723a227..7ff86cd7a37 100644 --- a/test/EFCore.Relational.Specification.Tests/Migrations/MigrationsTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/Migrations/MigrationsTestBase.cs @@ -621,13 +621,15 @@ public virtual Task Add_column_with_collation() } }); - [ConditionalFact] - public virtual Task Add_column_computed_with_collation() + [ConditionalTheory] + [InlineData(true)] + [InlineData(false)] + public virtual Task Add_column_computed_with_collation(bool stored) => Test( builder => builder.Entity("People").Property("Id"), builder => { }, builder => builder.Entity("People").Property("Name") - .HasComputedColumnSql("'hello'") + .HasComputedColumnSql("'hello'", stored) .UseCollation(NonDefaultCollation), model => { @@ -637,6 +639,7 @@ public virtual Task Add_column_computed_with_collation() if (AssertComputedColumns) { Assert.Contains("hello", nameColumn.ComputedColumnSql); + Assert.Equal(stored, nameColumn.IsStored); } if (AssertCollations) diff --git a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs index 0b9df14aed0..fed07e2f342 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs @@ -817,14 +817,14 @@ public override async Task Add_column_with_collation() """); } - public override async Task Add_column_computed_with_collation() + public override async Task Add_column_computed_with_collation(bool stored) { - await base.Add_column_computed_with_collation(); + await base.Add_column_computed_with_collation(stored); AssertSql( -""" -ALTER TABLE [People] ADD [Name] AS 'hello' COLLATE German_PhoneBook_CI_AS; -"""); + stored + ? """ALTER TABLE [People] ADD [Name] AS 'hello' COLLATE German_PhoneBook_CI_AS PERSISTED;""" + : """ALTER TABLE [People] ADD [Name] AS 'hello' COLLATE German_PhoneBook_CI_AS;"""); } public override async Task Add_column_shared() diff --git a/test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsSqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsSqliteTest.cs index 29cba6b159c..ce8127a964f 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsSqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsSqliteTest.cs @@ -446,14 +446,14 @@ public override async Task Add_column_with_collation() """); } - public override async Task Add_column_computed_with_collation() + public override async Task Add_column_computed_with_collation(bool stored) { - await base.Add_column_computed_with_collation(); + await base.Add_column_computed_with_collation(stored); AssertSql( -""" -ALTER TABLE "People" ADD "Name" AS ('hello') COLLATE NOCASE; -"""); + stored + ? """ALTER TABLE "People" ADD "Name" AS ('hello') STORED COLLATE NOCASE;""" + : """ALTER TABLE "People" ADD "Name" AS ('hello') COLLATE NOCASE;"""); } public override async Task Add_column_with_check_constraint()