From 68bbf70207313b51139dbe893855251b3bde0310 Mon Sep 17 00:00:00 2001 From: Raphael Schweizer Date: Fri, 4 Dec 2020 15:21:56 +0100 Subject: [PATCH] Quote FROM table identifier when rewriting fixes invalid migrations when the table name is a reserved keyword (e.g. 'Group') --- .../SqliteMigrationsSqlGenerator.cs | 2 +- .../Migrations/MigrationsSqliteTest.cs | 66 +++++++++---------- .../SqliteMigrationsSqlGeneratorTest.cs | 12 ++-- 3 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/EFCore.Sqlite.Core/Migrations/SqliteMigrationsSqlGenerator.cs b/src/EFCore.Sqlite.Core/Migrations/SqliteMigrationsSqlGenerator.cs index b8ee76d949c..f1136f37470 100644 --- a/src/EFCore.Sqlite.Core/Migrations/SqliteMigrationsSqlGenerator.cs +++ b/src/EFCore.Sqlite.Core/Migrations/SqliteMigrationsSqlGenerator.cs @@ -425,7 +425,7 @@ private IReadOnlyList RewriteOperations( .Append(selectBuilder) .AppendLine() .Append("FROM ") - .Append(table.Name) + .Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(table.Name)) .Append(Dependencies.SqlGenerationHelper.StatementTerminator) .ToString() }); diff --git a/test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsSqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsSqliteTest.cs index 04cc5cc33c2..0cfb966ed40 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsSqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsSqliteTest.cs @@ -121,7 +121,7 @@ public override async Task Alter_table_add_comment() );", @"INSERT INTO ""ef_temp_People"" (""Id"") SELECT ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -140,7 +140,7 @@ public override async Task Alter_table_add_comment_non_default_schema() );", @"INSERT INTO ""ef_temp_People"" (""Id"") SELECT ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -159,7 +159,7 @@ public override async Task Alter_table_change_comment() );", @"INSERT INTO ""ef_temp_People"" (""Id"") SELECT ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -176,7 +176,7 @@ public override async Task Alter_table_remove_comment() );", @"INSERT INTO ""ef_temp_People"" (""Id"") SELECT ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -204,7 +204,7 @@ public override async Task Rename_table_with_primary_key() // @"INSERT INTO ""ef_temp_Persons"" (""Id"") SELECT ""Id"" -FROM Persons;", +FROM ""Persons"";", // @"PRAGMA foreign_keys = 0;", // @@ -285,7 +285,7 @@ public override async Task Add_column_with_comment() // @"INSERT INTO ""ef_temp_People"" (""FullName"", ""Id"") SELECT ""FullName"", ""Id"" -FROM People;", +FROM ""People"";", // @"PRAGMA foreign_keys = 0;", // @@ -325,7 +325,7 @@ public override async Task Add_column_with_check_constraint() );", @"INSERT INTO ""ef_temp_People"" (""DriverLicense"", ""Id"") SELECT ""DriverLicense"", ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -343,7 +343,7 @@ public override async Task Alter_column_make_required() );", @"INSERT INTO ""ef_temp_People"" (""Id"", ""SomeColumn"") SELECT ""Id"", IFNULL(""SomeColumn"", '') -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -361,7 +361,7 @@ public override async Task Alter_column_make_required_with_index() );", @"INSERT INTO ""ef_temp_People"" (""Id"", ""SomeColumn"") SELECT ""Id"", IFNULL(""SomeColumn"", '') -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -381,7 +381,7 @@ public override async Task Alter_column_make_required_with_composite_index() );", @"INSERT INTO ""ef_temp_People"" (""FirstName"", ""Id"", ""LastName"") SELECT IFNULL(""FirstName"", ''), ""Id"", ""LastName"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -404,7 +404,7 @@ public override async Task Alter_column_make_computed(bool? stored) );", @"INSERT INTO ""ef_temp_People"" (""Id"", ""X"", ""Y"") SELECT ""Id"", ""X"", ""Y"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -424,7 +424,7 @@ public override async Task Alter_column_change_computed() );", @"INSERT INTO ""ef_temp_People"" (""Id"", ""X"", ""Y"") SELECT ""Id"", ""X"", ""Y"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -444,7 +444,7 @@ public override async Task Alter_column_change_computed_type() );", @"INSERT INTO ""ef_temp_People"" (""Id"", ""X"", ""Y"") SELECT ""Id"", ""X"", ""Y"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -462,7 +462,7 @@ public override async Task Alter_column_add_comment() );", @"INSERT INTO ""ef_temp_People"" (""Id"") SELECT ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -480,7 +480,7 @@ public override async Task Alter_column_change_comment() );", @"INSERT INTO ""ef_temp_People"" (""Id"") SELECT ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -497,7 +497,7 @@ public override async Task Alter_column_remove_comment() );", @"INSERT INTO ""ef_temp_People"" (""Id"") SELECT ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -514,7 +514,7 @@ public override async Task Alter_column_set_collation() );", @"INSERT INTO ""ef_temp_People"" (""Name"") SELECT ""Name"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -531,7 +531,7 @@ public override async Task Alter_column_reset_collation() );", @"INSERT INTO ""ef_temp_People"" (""Name"") SELECT ""Name"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -548,7 +548,7 @@ public override async Task Drop_column() );", @"INSERT INTO ""ef_temp_People"" (""Id"") SELECT ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -565,7 +565,7 @@ public override async Task Drop_column_primary_key() );", @"INSERT INTO ""ef_temp_People"" (""SomeColumn"") SELECT ""SomeColumn"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -616,7 +616,7 @@ public override async Task Add_primary_key() );", @"INSERT INTO ""ef_temp_People"" (""SomeField"") SELECT ""SomeField"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -633,7 +633,7 @@ public override async Task Add_primary_key_with_name() );", @"INSERT INTO ""ef_temp_People"" (""SomeField"") SELECT ""SomeField"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -652,7 +652,7 @@ public override async Task Add_primary_key_composite_with_name() );", @"INSERT INTO ""ef_temp_People"" (""SomeField1"", ""SomeField2"") SELECT ""SomeField1"", ""SomeField2"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -669,7 +669,7 @@ public override async Task Drop_primary_key() );", @"INSERT INTO ""ef_temp_People"" (""SomeField"") SELECT ""SomeField"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -688,7 +688,7 @@ public override async Task Add_foreign_key() );", @"INSERT INTO ""ef_temp_Orders"" (""CustomerId"", ""Id"") SELECT ""CustomerId"", ""Id"" -FROM Orders;", +FROM ""Orders"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""Orders"";", @"ALTER TABLE ""ef_temp_Orders"" RENAME TO ""Orders"";", @@ -707,7 +707,7 @@ public override async Task Add_foreign_key_with_name() );", @"INSERT INTO ""ef_temp_Orders"" (""CustomerId"", ""Id"") SELECT ""CustomerId"", ""Id"" -FROM Orders;", +FROM ""Orders"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""Orders"";", @"ALTER TABLE ""ef_temp_Orders"" RENAME TO ""Orders"";", @@ -725,7 +725,7 @@ public override async Task Drop_foreign_key() );", @"INSERT INTO ""ef_temp_Orders"" (""CustomerId"", ""Id"") SELECT ""CustomerId"", ""Id"" -FROM Orders;", +FROM ""Orders"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""Orders"";", @"ALTER TABLE ""ef_temp_Orders"" RENAME TO ""Orders"";", @@ -744,7 +744,7 @@ public override async Task Add_unique_constraint() );", @"INSERT INTO ""ef_temp_People"" (""AlternateKeyColumn"", ""Id"") SELECT ""AlternateKeyColumn"", ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -764,7 +764,7 @@ public override async Task Add_unique_constraint_composite_with_name() );", @"INSERT INTO ""ef_temp_People"" (""AlternateKeyColumn1"", ""AlternateKeyColumn2"", ""Id"") SELECT ""AlternateKeyColumn1"", ""AlternateKeyColumn2"", ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -782,7 +782,7 @@ public override async Task Drop_unique_constraint() );", @"INSERT INTO ""ef_temp_People"" (""AlternateKeyColumn"", ""Id"") SELECT ""AlternateKeyColumn"", ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -801,7 +801,7 @@ public override async Task Add_check_constraint_with_name() );", @"INSERT INTO ""ef_temp_People"" (""DriverLicense"", ""Id"") SELECT ""DriverLicense"", ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -820,7 +820,7 @@ public override async Task Alter_check_constraint() );", @"INSERT INTO ""ef_temp_People"" (""DriverLicense"", ""Id"") SELECT ""DriverLicense"", ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", @@ -838,7 +838,7 @@ public override async Task Drop_check_constraint() );", @"INSERT INTO ""ef_temp_People"" (""DriverLicense"", ""Id"") SELECT ""DriverLicense"", ""Id"" -FROM People;", +FROM ""People"";", @"PRAGMA foreign_keys = 0;", @"DROP TABLE ""People"";", @"ALTER TABLE ""ef_temp_People"" RENAME TO ""People"";", diff --git a/test/EFCore.Sqlite.FunctionalTests/Migrations/SqliteMigrationsSqlGeneratorTest.cs b/test/EFCore.Sqlite.FunctionalTests/Migrations/SqliteMigrationsSqlGeneratorTest.cs index d5a2a81562d..90def471fdc 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Migrations/SqliteMigrationsSqlGeneratorTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Migrations/SqliteMigrationsSqlGeneratorTest.cs @@ -768,7 +768,7 @@ public virtual void DropColumn_defers_subsequent_RenameColumn() INSERT INTO ""ef_temp_Blog"" (""Name"") SELECT ""Title"" -FROM Blog; +FROM ""Blog""; GO PRAGMA foreign_keys = 0; @@ -818,7 +818,7 @@ public virtual void Deferred_RenameColumn_defers_subsequent_AddColumn() INSERT INTO ""ef_temp_Blog"" (""Name"") SELECT ""Title"" -FROM Blog; +FROM ""Blog""; GO PRAGMA foreign_keys = 0; @@ -871,7 +871,7 @@ public virtual void Deferred_RenameColumn_defers_subsequent_CreateIndex_unique() INSERT INTO ""ef_temp_Blog"" (""Name"") SELECT ""Title"" -FROM Blog; +FROM ""Blog""; GO PRAGMA foreign_keys = 0; @@ -919,7 +919,7 @@ public virtual void DropColumn_defers_subsequent_AddColumn_required() INSERT INTO ""ef_temp_Blog"" (""Id"") SELECT ""Id"" -FROM Blog; +FROM ""Blog""; GO PRAGMA foreign_keys = 0; @@ -970,7 +970,7 @@ public virtual void Deferred_AddColumn_defers_subsequent_CreateIndex() INSERT INTO ""ef_temp_Blog"" (""Id"") SELECT ""Id"" -FROM Blog; +FROM ""Blog""; GO PRAGMA foreign_keys = 0; @@ -1015,7 +1015,7 @@ public virtual void RenameTable_preserves_pending_rebuilds() INSERT INTO ""ef_temp_Blog"" (""Id"") SELECT ""Id"" -FROM Blog; +FROM ""Blog""; GO PRAGMA foreign_keys = 0;