Skip to content

Commit 57e9c68

Browse files
committed
修复没有delimitIdentity时候迁移的bug
1 parent a7e987d commit 57e9c68

File tree

6 files changed

+78
-17
lines changed

6 files changed

+78
-17
lines changed

samples/Sample.AutoCreateIfPresent/DefaultDbContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Sample.AutoCreateIfPresent
1414
{
1515
public class DefaultDbContext:AbstractShardingDbContext,IShardingTableDbContext
1616
{
17+
1718
public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
1819
{
1920
}

samples/Sample.AutoCreateIfPresent/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Sample.AutoCreateIfPresent;
33
using ShardingCore;
44
using ShardingCore.Bootstrappers;
5+
using ShardingCore.Core.DbContextCreator;
6+
using ShardingCore.Core.RuntimeContexts;
57
using ShardingCore.TableExists;
68
using ShardingCore.TableExists.Abstractions;
79

@@ -38,7 +40,8 @@
3840
{
3941
b.UseMySql(conn, new MySqlServerVersion(new Version())).UseLoggerFactory(efLogger);
4042
});
41-
}).ReplaceService<ITableEnsureManager,MySqlTableEnsureManager>().AddShardingCore();
43+
})
44+
.ReplaceService<ITableEnsureManager,MySqlTableEnsureManager>().AddShardingCore();
4245
var app = builder.Build();
4346

4447
// Configure the HTTP request pipeline.

samples/Sample.MySql/Domain/Maps/SysUserLogByMonthMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void Configure(EntityTypeBuilder<SysUserLogByMonth> builder)
1414
{
1515
builder.HasKey(o => o.Id);
1616
builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
17-
builder.ToTable(nameof(SysUserLogByMonth));
17+
builder.ToTable("Sys_User_LogBy_Month");
1818
}
1919
}
2020
}

samples/Sample.MySql/Migrations/20220704042701_init.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
2828
.Annotation("MySql:CharSet", "utf8mb4");
2929

3030
migrationBuilder.CreateTable(
31-
name: "SysUserLogByMonth",
31+
name: "Sys_User_LogBy_Month",
3232
columns: table => new
3333
{
3434
Id = table.Column<string>(type: "varchar(128)", maxLength: 128, nullable: false)
@@ -37,7 +37,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
3737
},
3838
constraints: table =>
3939
{
40-
table.PrimaryKey("PK_SysUserLogByMonth", x => x.Id);
40+
table.PrimaryKey("PK_Sys_User_LogBy_Month", x => x.Id);
4141
})
4242
.Annotation("MySql:CharSet", "utf8mb4");
4343

@@ -64,7 +64,7 @@ protected override void Down(MigrationBuilder migrationBuilder)
6464
name: "SysTest");
6565

6666
migrationBuilder.DropTable(
67-
name: "SysUserLogByMonth");
67+
name: "Sys_User_LogBy_Month");
6868

6969
migrationBuilder.DropTable(
7070
name: "SysUserMod");

src/ShardingCore/Helpers/MigrationHelper.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,15 @@ private static (MigrationResult migrationResult, List<string>) BuildDataSourceSh
158158
shardings.ForEach(aShardingTable =>
159159
{
160160
string newCmd = sourceCmd;
161-
GetReplaceGroups(operation, absTableName, aShardingTable).ForEach(aReplace =>
161+
var replaceGroups = GetReplaceGroups(operation, absTableName, aShardingTable);
162+
foreach (var migrationReplaceItem in replaceGroups)
162163
{
164+
var delimitSourceNameIdentifier = sqlGenerationHelper.DelimitIdentifier(migrationReplaceItem.SourceName);
165+
var delimitTargetNameIdentifier = sqlGenerationHelper.DelimitIdentifier(migrationReplaceItem.TargetName);
163166
newCmd = newCmd.Replace(
164-
sqlGenerationHelper.DelimitIdentifier(aReplace.sourceName),
165-
sqlGenerationHelper.DelimitIdentifier(aReplace.targetName));
166-
});
167+
delimitSourceNameIdentifier,
168+
delimitTargetNameIdentifier);
169+
}
167170
if (newCmd.Contains(
168171
"EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE'"))
169172
{
@@ -185,13 +188,13 @@ string BuildPattern(string absTableName)
185188
}
186189
}
187190

188-
private static List<(string sourceName, string targetName)> GetReplaceGroups(
191+
private static ISet<MigrationReplaceItem> GetReplaceGroups(
189192
MigrationOperation operation, string sourceTableName, string targetTableName)
190193
{
191-
List<(string sourceName, string targetName)> resList =
192-
new List<(string sourceName, string targetName)>
194+
ISet<MigrationReplaceItem> resList =
195+
new HashSet<MigrationReplaceItem>()
193196
{
194-
(sourceTableName, targetTableName)
197+
new MigrationReplaceItem(sourceTableName, targetTableName)
195198
};
196199

197200
string name = operation.GetPropertyValue("Name") as string;
@@ -209,7 +212,7 @@ string BuildPattern(string absTableName)
209212
if (Regex.IsMatch(name, aPattern))
210213
{
211214
var newName = new Regex(aPattern).Replace(name, "${1}" + targetTableName + "$3");
212-
resList.Add((name, newName));
215+
resList.Add(new MigrationReplaceItem(name, newName));
213216
break;
214217
}
215218
}
@@ -235,14 +238,22 @@ string BuildPattern(string absTableName)
235238
var propertyValue = aProperty.GetValue(operation);
236239
if (propertyValue is MigrationOperation propertyOperation)
237240
{
238-
resList.AddRange(GetReplaceGroups(propertyOperation, sourceTableName, targetTableName));
241+
var migrationReplaceItems = GetReplaceGroups(propertyOperation, sourceTableName, targetTableName);
242+
foreach (var migrationReplaceItem in migrationReplaceItems)
243+
{
244+
resList.Add(migrationReplaceItem);
245+
}
239246
}
240247
else if (listPropertyWhere(aProperty))
241248
{
242249
foreach (var aValue in (IEnumerable)propertyValue)
243250
{
244-
resList.AddRange(GetReplaceGroups((MigrationOperation)aValue, sourceTableName,
245-
targetTableName));
251+
var migrationReplaceItems = GetReplaceGroups((MigrationOperation)aValue, sourceTableName,
252+
targetTableName);
253+
foreach (var migrationReplaceItem in migrationReplaceItems)
254+
{
255+
resList.Add(migrationReplaceItem);
256+
}
246257
}
247258
}
248259
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
3+
namespace ShardingCore.Helpers
4+
{
5+
6+
public class MigrationReplaceItem
7+
{
8+
public string SourceName { get; }
9+
public string TargetName { get; }
10+
11+
public MigrationReplaceItem(string sourceName,string targetName)
12+
{
13+
SourceName = sourceName;
14+
TargetName = targetName;
15+
}
16+
protected bool Equals(MigrationReplaceItem other)
17+
{
18+
return SourceName == other.SourceName && TargetName == other.TargetName;
19+
}
20+
21+
public override bool Equals(object obj)
22+
{
23+
if (ReferenceEquals(null, obj)) return false;
24+
if (ReferenceEquals(this, obj)) return true;
25+
if (obj.GetType() != this.GetType()) return false;
26+
return Equals((MigrationReplaceItem)obj);
27+
}
28+
#if !EFCORE2
29+
30+
public override int GetHashCode()
31+
{
32+
return HashCode.Combine(SourceName, TargetName);
33+
}
34+
#endif
35+
#if EFCORE2
36+
37+
public override int GetHashCode()
38+
{
39+
unchecked
40+
{
41+
return ((SourceName != null ? SourceName.GetHashCode() : 0) * 397) ^ (TargetName != null ? TargetName.GetHashCode() : 0);
42+
}
43+
}
44+
#endif
45+
}
46+
}

0 commit comments

Comments
 (0)