修复没有delimitIdentity时候迁移的bug

This commit is contained in:
xuejiaming 2022-07-19 16:37:14 +08:00
parent a7e987d163
commit 57e9c685c6
6 changed files with 78 additions and 17 deletions

View File

@ -14,6 +14,7 @@ namespace Sample.AutoCreateIfPresent
{
public class DefaultDbContext:AbstractShardingDbContext,IShardingTableDbContext
{
public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
{
}

View File

@ -2,6 +2,8 @@ using Microsoft.EntityFrameworkCore;
using Sample.AutoCreateIfPresent;
using ShardingCore;
using ShardingCore.Bootstrappers;
using ShardingCore.Core.DbContextCreator;
using ShardingCore.Core.RuntimeContexts;
using ShardingCore.TableExists;
using ShardingCore.TableExists.Abstractions;
@ -38,7 +40,8 @@ builder.Services.AddShardingDbContext<DefaultDbContext>()
{
b.UseMySql(conn, new MySqlServerVersion(new Version())).UseLoggerFactory(efLogger);
});
}).ReplaceService<ITableEnsureManager,MySqlTableEnsureManager>().AddShardingCore();
})
.ReplaceService<ITableEnsureManager,MySqlTableEnsureManager>().AddShardingCore();
var app = builder.Build();
// Configure the HTTP request pipeline.

View File

@ -14,7 +14,7 @@ namespace Sample.MySql.Domain.Maps
{
builder.HasKey(o => o.Id);
builder.Property(o => o.Id).IsRequired().HasMaxLength(128);
builder.ToTable(nameof(SysUserLogByMonth));
builder.ToTable("Sys_User_LogBy_Month");
}
}
}

View File

@ -28,7 +28,7 @@ namespace Sample.MySql.Migrations
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "SysUserLogByMonth",
name: "Sys_User_LogBy_Month",
columns: table => new
{
Id = table.Column<string>(type: "varchar(128)", maxLength: 128, nullable: false)
@ -37,7 +37,7 @@ namespace Sample.MySql.Migrations
},
constraints: table =>
{
table.PrimaryKey("PK_SysUserLogByMonth", x => x.Id);
table.PrimaryKey("PK_Sys_User_LogBy_Month", x => x.Id);
})
.Annotation("MySql:CharSet", "utf8mb4");
@ -64,7 +64,7 @@ namespace Sample.MySql.Migrations
name: "SysTest");
migrationBuilder.DropTable(
name: "SysUserLogByMonth");
name: "Sys_User_LogBy_Month");
migrationBuilder.DropTable(
name: "SysUserMod");

View File

@ -158,12 +158,15 @@ namespace ShardingCore.Helpers
shardings.ForEach(aShardingTable =>
{
string newCmd = sourceCmd;
GetReplaceGroups(operation, absTableName, aShardingTable).ForEach(aReplace =>
var replaceGroups = GetReplaceGroups(operation, absTableName, aShardingTable);
foreach (var migrationReplaceItem in replaceGroups)
{
var delimitSourceNameIdentifier = sqlGenerationHelper.DelimitIdentifier(migrationReplaceItem.SourceName);
var delimitTargetNameIdentifier = sqlGenerationHelper.DelimitIdentifier(migrationReplaceItem.TargetName);
newCmd = newCmd.Replace(
sqlGenerationHelper.DelimitIdentifier(aReplace.sourceName),
sqlGenerationHelper.DelimitIdentifier(aReplace.targetName));
});
delimitSourceNameIdentifier,
delimitTargetNameIdentifier);
}
if (newCmd.Contains(
"EXEC sp_addextendedproperty 'MS_Description', @description, 'SCHEMA', @defaultSchema, 'TABLE'"))
{
@ -185,13 +188,13 @@ namespace ShardingCore.Helpers
}
}
private static List<(string sourceName, string targetName)> GetReplaceGroups(
private static ISet<MigrationReplaceItem> GetReplaceGroups(
MigrationOperation operation, string sourceTableName, string targetTableName)
{
List<(string sourceName, string targetName)> resList =
new List<(string sourceName, string targetName)>
ISet<MigrationReplaceItem> resList =
new HashSet<MigrationReplaceItem>()
{
(sourceTableName, targetTableName)
new MigrationReplaceItem(sourceTableName, targetTableName)
};
string name = operation.GetPropertyValue("Name") as string;
@ -209,7 +212,7 @@ namespace ShardingCore.Helpers
if (Regex.IsMatch(name, aPattern))
{
var newName = new Regex(aPattern).Replace(name, "${1}" + targetTableName + "$3");
resList.Add((name, newName));
resList.Add(new MigrationReplaceItem(name, newName));
break;
}
}
@ -235,14 +238,22 @@ namespace ShardingCore.Helpers
var propertyValue = aProperty.GetValue(operation);
if (propertyValue is MigrationOperation propertyOperation)
{
resList.AddRange(GetReplaceGroups(propertyOperation, sourceTableName, targetTableName));
var migrationReplaceItems = GetReplaceGroups(propertyOperation, sourceTableName, targetTableName);
foreach (var migrationReplaceItem in migrationReplaceItems)
{
resList.Add(migrationReplaceItem);
}
}
else if (listPropertyWhere(aProperty))
{
foreach (var aValue in (IEnumerable)propertyValue)
{
resList.AddRange(GetReplaceGroups((MigrationOperation)aValue, sourceTableName,
targetTableName));
var migrationReplaceItems = GetReplaceGroups((MigrationOperation)aValue, sourceTableName,
targetTableName);
foreach (var migrationReplaceItem in migrationReplaceItems)
{
resList.Add(migrationReplaceItem);
}
}
}
});

View File

@ -0,0 +1,46 @@
using System;
namespace ShardingCore.Helpers
{
public class MigrationReplaceItem
{
public string SourceName { get; }
public string TargetName { get; }
public MigrationReplaceItem(string sourceName,string targetName)
{
SourceName = sourceName;
TargetName = targetName;
}
protected bool Equals(MigrationReplaceItem other)
{
return SourceName == other.SourceName && TargetName == other.TargetName;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((MigrationReplaceItem)obj);
}
#if !EFCORE2
public override int GetHashCode()
{
return HashCode.Combine(SourceName, TargetName);
}
#endif
#if EFCORE2
public override int GetHashCode()
{
unchecked
{
return ((SourceName != null ? SourceName.GetHashCode() : 0) * 397) ^ (TargetName != null ? TargetName.GetHashCode() : 0);
}
}
#endif
}
}