Skip to content

Commit

Permalink
[#215] 并且修复补偿表+HasData的bug 发布6.8.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
xuejmnet committed Nov 1, 2022
1 parent c34b3a8 commit 339b543
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 60 deletions.
2 changes: 1 addition & 1 deletion nuget-publish.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
:start
::定义版本
set SHARDINGCORE=6.8.0.3
set SHARDINGCORE=6.8.0.4

::删除所有bin与obj下的文件
@echo off
Expand Down
8 changes: 4 additions & 4 deletions samples/Sample.MySql/Controllers/WeatherForecastController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public async Task<IActionResult> Get()
// using (var tran = _defaultTableDbContext.Database.BeginTransaction())
// {

SysUserMod? resultX123 = await _defaultTableDbContext.Set<SysUserMod>()
.Where(o => o.Id == "2").FirstOrDefaultAsync();
_defaultTableDbContext.Update(resultX123);
_defaultTableDbContext.SaveChanges();
// SysUserMod? resultX123 = await _defaultTableDbContext.Set<SysUserMod>()
// .Where(o => o.Id == "2").FirstOrDefaultAsync();
// _defaultTableDbContext.Update(resultX123);
// _defaultTableDbContext.SaveChanges();
var resultX1 = await _defaultTableDbContext.Set<SysUserMod>()
.Where(o => o.Id == "2" || o.Id == "3").GroupBy(o => new { o.Id,o.Name })
.Select(o => new
Expand Down
30 changes: 16 additions & 14 deletions samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Sample.MySql.DbContexts
{
public class DefaultShardingDbContext:AbstractShardingDbContext, IShardingTableDbContext
public class DefaultShardingDbContext : AbstractShardingDbContext, IShardingTableDbContext
{
public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> options) : base(options)
{
Expand All @@ -20,14 +20,15 @@ public DefaultShardingDbContext(DbContextOptions<DefaultShardingDbContext> optio
//Database.SetCommandTimeout(30000);
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseLazyLoadingProxies();
}
// protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
// {
// base.OnConfiguring(optionsBuilder);
// optionsBuilder.UseLazyLoadingProxies();
// }

private readonly MethodInfo? _configureGlobalFiltersMethodInfo =
typeof(DefaultShardingDbContext).GetMethod(nameof(ConfigureGlobalFilters), BindingFlags.Instance | BindingFlags.NonPublic);
typeof(DefaultShardingDbContext).GetMethod(nameof(ConfigureGlobalFilters),
BindingFlags.Instance | BindingFlags.NonPublic);

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand All @@ -40,7 +41,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<SysTest>().HasData(new SysTest() { Id = "1", UserId = "123" });
}


protected void ConfigureGlobalFilters<TEntity>(ModelBuilder modelBuilder, IMutableEntityType entityType)
where TEntity : class
{
Expand All @@ -53,17 +54,18 @@ protected void ConfigureGlobalFilters<TEntity>(ModelBuilder modelBuilder, IMutab
{
Expression<Func<TEntity, bool>>? expression = null;
if (typeof(TEntity) == typeof(SysTest))
{
{
expression = e => ((IUser)e).UserId == "123";

} if (typeof(TEntity) == typeof(SysUserMod))
{
}

if (typeof(TEntity) == typeof(SysUserMod))
{
expression = e => ((IAge)e).Age == 99;

}

return expression;
}

public IRouteTail RouteTail { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ public class SysUserModVirtualDataSourceRoute:AbstractShardingOperatorVirtualDat
{
public override string ShardingKeyToDataSourceName(object shardingKey)
{
return $"{shardingKey}";
return $"{((string)shardingKey=="ds1"?"ds1":"ds2")}";
}

public override List<string> GetAllDataSourceNames()
{
return new List<string>()
{
"ds0", "ds1", "ds2"
"ds1", "ds2"
};
}

Expand Down
20 changes: 10 additions & 10 deletions samples/Sample.MySql/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public void ConfigureServices(IServiceCollection services)
o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
}).UseConfig(o =>
{
o.UseEntityFrameworkCoreProxies = true;
o.ThrowIfQueryRouteNotMatch = false;
// o.UseEntityFrameworkCoreProxies = true;
o.ThrowIfQueryRouteNotMatch = false;
o.AutoUseWriteConnectionStringAfterWriteDb = true;
o.UseShardingQuery((conStr, builder) =>
{
Expand Down Expand Up @@ -185,14 +185,14 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>();
// var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
// var entityMetadata = entityMetadataManager.TryGet<SysUserMod>();
using (var scope = app.ApplicationServices.CreateScope())
{
var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
// if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
{
defaultShardingDbContext.Database.Migrate();
}
}
// using (var scope = app.ApplicationServices.CreateScope())
// {
// var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
// // if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
// {
// defaultShardingDbContext.Database.Migrate();
// }
// }
app.ApplicationServices.UseAutoTryCompensateTable();
// using (var scope = app.ApplicationServices.CreateScope())
// {
Expand Down
74 changes: 45 additions & 29 deletions src/ShardingCore/DynamicDataSources/DefaultDataSourceInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
using ShardingCore.Core.VirtualRoutes.TableRoutes;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;

using ShardingCore.Sharding.Abstractions;
using ShardingCore.TableExists.Abstractions;

namespace ShardingCore.DynamicDataSources
{
public class DataSourceInitializer : IDataSourceInitializer
{
private readonly ILogger<DataSourceInitializer> _logger ;
private readonly ILogger<DataSourceInitializer> _logger;

private readonly IShardingProvider _shardingProvider;
private readonly IDbContextCreator _dbContextCreator;
Expand All @@ -48,7 +47,7 @@ public DataSourceInitializer(
IEntityMetadataManager entityMetadataManager,
IShardingTableCreator shardingTableCreator,
ITableEnsureManager tableEnsureManager,
ILogger<DataSourceInitializer> logger )
ILogger<DataSourceInitializer> logger)
{
_shardingProvider = shardingProvider;
_dbContextCreator = dbContextCreator;
Expand All @@ -63,7 +62,7 @@ public DataSourceInitializer(
_logger = logger;
}

public void InitConfigure(string dataSourceName,bool createDatabase,bool createTable)
public void InitConfigure(string dataSourceName, bool createDatabase, bool createTable)
{
using (var shardingScope = _shardingProvider.CreateScope())
{
Expand All @@ -72,45 +71,40 @@ public void InitConfigure(string dataSourceName,bool createDatabase,bool createT
var isDefault = _virtualDataSource.IsDefault(dataSourceName);
if (createDatabase)
{
EnsureCreated(isDefault,shellDbContext,dataSourceName);
EnsureCreated(isDefault, shellDbContext, dataSourceName);
}

if (createTable)
{
var existTables = _tableEnsureManager.GetExistTables((IShardingDbContext)shellDbContext,dataSourceName);
var existTables =
_tableEnsureManager.GetExistTables((IShardingDbContext)shellDbContext, dataSourceName);
var allShardingEntities = _entityMetadataManager.GetAllShardingEntities();
foreach (var entityType in allShardingEntities)
{
//如果是默认数据源
if (_virtualDataSource.IsDefault(dataSourceName))
var entityMetadata = _entityMetadataManager.TryGet(entityType);
if (entityMetadata.IsShardingDataSource())
{
if (_entityMetadataManager.IsShardingTable(entityType))
var virtualDataSourceRoute = _dataSourceRouteManager.GetRoute(entityType);
//如果是分库对象就要判断是否含有当前数据源
if (virtualDataSourceRoute.GetAllDataSourceNames().Contains(dataSourceName))
{
var virtualTableRoute = _tableRouteManager.GetRoute(entityType);
CreateDataTable(dataSourceName, virtualTableRoute, existTables);
CreateDataTable(dataSourceName, entityMetadata, existTables);
}
}
//不是分库对象
else
{
//非默认数据源
if (_entityMetadataManager.IsShardingDataSource(entityType))
if (_virtualDataSource.IsDefault(dataSourceName))
{
var virtualDataSourceRoute = _dataSourceRouteManager.GetRoute(entityType);
if (virtualDataSourceRoute.GetAllDataSourceNames().Contains(dataSourceName))
{
if (_entityMetadataManager.IsShardingTable(entityType))
{
var virtualTableRoute = _tableRouteManager.GetRoute(entityType);
CreateDataTable(dataSourceName, virtualTableRoute,existTables);
}
}
CreateDataTable(dataSourceName, entityMetadata, existTables);
}
}
}
}
}
}
}

private void EnsureCreated(bool isDefault, DbContext context,
string dataSourceName)
{
Expand All @@ -128,7 +122,7 @@ private void EnsureCreated(bool isDefault, DbContext context,
{
dbContext.RemoveDbContextAllRelationModelWithoutShardingDataSourceOnly();
}

dbContext.Database.EnsureCreated();
}
}
Expand All @@ -138,17 +132,18 @@ private void EnsureCreated(bool isDefault, DbContext context,
$"{nameof(IDbContextCreator)}.{nameof(IDbContextCreator.GetShellDbContext)} db context type not impl {nameof(IShardingDbContext)}");
}
}
private void CreateDataTable(string dataSourceName, IVirtualTableRoute tableRoute, ISet<string> existTables)

// private void CreateDataTable(string dataSourceName, IVirtualTableRoute tableRoute, ISet<string> existTables)
private void CreateDataTable(string dataSourceName, EntityMetadata entityMetadata, ISet<string> existTables)
{
var entityMetadata = tableRoute.EntityMetadata;
foreach (var tail in tableRoute.GetTails())
if (!entityMetadata.IsShardingTable())
{
var physicTableName = $"{entityMetadata.LogicTableName}{entityMetadata.TableSeparator}{tail}";
var physicTableName = $"{entityMetadata.LogicTableName}";
try
{
//添加物理表
if (!existTables.Contains(physicTableName))
_tableCreator.CreateTable(dataSourceName, entityMetadata.EntityType, tail);
_tableCreator.CreateTable(dataSourceName, entityMetadata.EntityType, string.Empty);
}
catch (Exception e)
{
Expand All @@ -159,7 +154,28 @@ private void CreateDataTable(string dataSourceName, IVirtualTableRoute tableRout
}
}
}
else
{
var tableRoute = _tableRouteManager.GetRoute(entityMetadata.EntityType);
foreach (var tail in tableRoute.GetTails())
{
var physicTableName = $"{entityMetadata.LogicTableName}{entityMetadata.TableSeparator}{tail}";
try
{
//添加物理表
if (!existTables.Contains(physicTableName))
_tableCreator.CreateTable(dataSourceName, entityMetadata.EntityType, tail);
}
catch (Exception e)
{
if (!_shardingConfigOptions.IgnoreCreateTableError.GetValueOrDefault())
{
_logger.LogWarning(e,
$"table :{physicTableName} will created.");
}
}
}
}
}

}
}
47 changes: 47 additions & 0 deletions src/ShardingCore/Extensions/DbContextExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public static void RemoveDbContextRelationModelThatIsShardingTable(this DbContex
var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();

#if NET6_0
var entityTypes = contextModel.GetEntityTypes();
foreach (var entityType in entityTypes)
{
if (entityType.GetFieldValue("_data") is List<object> _data)
{
_data.Clear();
}
}
var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel;
var valueTuples =
contextModelRelationalModel.Tables.Where(o =>o.Value.EntityTypeMappings.Any(m => entityMetadataManager.IsShardingTable(m.EntityType.ClrType))).Select(o => o.Key).ToList();
Expand All @@ -56,6 +64,14 @@ public static void RemoveDbContextRelationModelThatIsShardingTable(this DbContex
}
#endif
#if NET5_0 || NETSTANDARD2_1
var entityTypes = contextModel.GetEntityTypes();
foreach (var entityType in entityTypes)
{
if (entityType.GetFieldValue("_data") is List<object> _data)
{
_data.Clear();
}
}
var contextModelRelationalModel = contextModel.RelationalModel as RelationalModel;
var valueTuples =
contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => entityMetadataManager.IsShardingTable(m.EntityType.ClrType))).Select(o => o.Key).ToList();
Expand All @@ -67,6 +83,13 @@ public static void RemoveDbContextRelationModelThatIsShardingTable(this DbContex
#if NETCOREAPP2_0 || NETSTANDARD2_0 || NETCOREAPP3_0
var entityTypes =
contextModel.GetFieldValue("_entityTypes") as SortedDictionary<string, EntityType>;
foreach (var entityType in entityTypes)
{
if (entityType.GetFieldValue("_data") is List<object> _data)
{
_data.Clear();
}
}
var list = entityTypes.Where(o=>entityMetadataManager.IsShardingTable(o.Value.ClrType)).Select(o=>o.Key).ToList();
for (int i = 0; i < list.Count; i++)
{
Expand Down Expand Up @@ -124,6 +147,14 @@ public static void RemoveDbContextAllRelationModelWithoutShardingDataSourceOnly(
var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();

#if NET6_0
var entityTypes = contextModel.GetEntityTypes();
foreach (var entityType in entityTypes)
{
if (entityType.GetFieldValue("_data") is List<object> _data)
{
_data.Clear();
}
}
var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel;
var valueTuples =
contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => !entityMetadataManager.IsShardingDataSource(m.EntityType.ClrType) ||entityMetadataManager.TryGet(m.EntityType.ClrType)==null)).Select(o => o.Key).ToList();
Expand All @@ -133,6 +164,14 @@ public static void RemoveDbContextAllRelationModelWithoutShardingDataSourceOnly(
}
#endif
#if NET5_0 || NETSTANDARD2_1
var entityTypes = contextModel.GetEntityTypes();
foreach (var entityType in entityTypes)
{
if (entityType.GetFieldValue("_data") is List<object> _data)
{
_data.Clear();
}
}
var contextModelRelationalModel = contextModel.RelationalModel as RelationalModel;
var valueTuples =
contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => !entityMetadataManager.IsShardingDataSource(m.EntityType.ClrType)||entityMetadataManager.TryGet(m.EntityType.ClrType)==null)).Select(o => o.Key).ToList();
Expand All @@ -142,8 +181,16 @@ public static void RemoveDbContextAllRelationModelWithoutShardingDataSourceOnly(
}
#endif
#if NETCOREAPP2_0 || NETSTANDARD2_0 || NETCOREAPP3_0

var entityTypes =
contextModel.GetFieldValue("_entityTypes") as SortedDictionary<string, EntityType>;
foreach (var entityType in entityTypes)
{
if (entityType.GetFieldValue("_data") is List<object> _data)
{
_data.Clear();
}
}
var list = entityTypes.Where(o => !entityMetadataManager.IsShardingDataSource(o.Value.ClrType) || entityMetadataManager.TryGet(o.Value.ClrType) == null).Select(o => o.Key).ToList();
for (int i = 0; i < list.Count; i++)
{
Expand Down

0 comments on commit 339b543

Please sign in to comment.