Skip to content

Commit 7ed82ee

Browse files
authored
Throw on rename or drop index migration operations without table name. (PomeloFoundation#1664)
1 parent 3f8eb8c commit 7ed82ee

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/EFCore.MySql/Migrations/MySqlMigrationsSqlGenerator.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ protected override void Generate(
315315
Check.NotNull(operation, nameof(operation));
316316
Check.NotNull(builder, nameof(builder));
317317

318+
if (string.IsNullOrEmpty(operation.Table))
319+
{
320+
throw new InvalidOperationException(MySqlStrings.IndexTableRequired);
321+
}
322+
318323
if (operation.NewName != null)
319324
{
320325
if (_options.ServerVersion.Supports.RenameIndex)
@@ -639,6 +644,11 @@ protected override void Generate(
639644
Check.NotNull(operation, nameof(operation));
640645
Check.NotNull(builder, nameof(builder));
641646

647+
if (string.IsNullOrEmpty(operation.Table))
648+
{
649+
throw new InvalidOperationException(MySqlStrings.IndexTableRequired);
650+
}
651+
642652
builder
643653
.Append("ALTER TABLE ")
644654
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Table, operation.Schema))

src/EFCore.MySql/Properties/MySqlStrings.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
<value>MySQL sequences cannot be used to generate values for the property '{property}' on entity type '{entityType}' because the property type is '{propertyType}'. Sequences can only be used with integer properties.</value>
131131
</data>
132132
<data name="IndexTableRequired" xml:space="preserve">
133-
<value>MySQL requires the table name to be specified for rename index operations. Specify table name in the call to MigrationBuilder.RenameIndex.</value>
133+
<value>MySQL requires the table name to be specified for index operations. Specify table name in calls to 'MigrationBuilder.RenameIndex' and 'DropIndex'.</value>
134134
</data>
135135
<data name="AlterMemoryOptimizedTable" xml:space="preserve">
136136
<value>To set memory-optimized on a table on or off the table needs to be dropped and recreated.</value>

test/EFCore.MySql.FunctionalTests/MySqlMigrationsSqlGeneratorTest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Microsoft.Extensions.Logging;
1515
using Pomelo.EntityFrameworkCore.MySql.FunctionalTests.TestUtilities;
1616
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
17+
using Pomelo.EntityFrameworkCore.MySql.Internal;
1718
using Pomelo.EntityFrameworkCore.MySql.Metadata.Internal;
1819
using Pomelo.EntityFrameworkCore.MySql.Tests;
1920
using Pomelo.EntityFrameworkCore.MySql.Tests.TestUtilities.Attributes;
@@ -1034,6 +1035,35 @@ public virtual void RenameIndexOperation()
10341035
Sql);
10351036
}
10361037

1038+
[ConditionalFact]
1039+
public virtual void RenameIndexOperations_throws_when_no_table()
1040+
{
1041+
var migrationBuilder = new MigrationBuilder("MySql");
1042+
1043+
migrationBuilder.RenameIndex(
1044+
name: "IX_OldIndex",
1045+
newName: "IX_NewIndex");
1046+
1047+
var ex = Assert.Throws<InvalidOperationException>(
1048+
() => Generate(migrationBuilder.Operations.ToArray()));
1049+
1050+
Assert.Equal(MySqlStrings.IndexTableRequired, ex.Message);
1051+
}
1052+
1053+
[ConditionalFact]
1054+
public virtual void DropIndexOperations_throws_when_no_table()
1055+
{
1056+
var migrationBuilder = new MigrationBuilder("MySql");
1057+
1058+
migrationBuilder.DropIndex(
1059+
name: "IX_Name");
1060+
1061+
var ex = Assert.Throws<InvalidOperationException>(
1062+
() => Generate(migrationBuilder.Operations.ToArray()));
1063+
1064+
Assert.Equal(MySqlStrings.IndexTableRequired, ex.Message);
1065+
}
1066+
10371067
[ConditionalFact]
10381068
[SupportedServerVersionCondition(nameof(ServerVersionSupport.RenameColumn))]
10391069
public virtual void RenameColumnOperation()

0 commit comments

Comments
 (0)