diff --git a/nuget-publish.bat b/nuget-publish.bat index 13945421..6e0ed12a 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -1,8 +1,8 @@ :start ::定义版本 -set EFCORE2=2.1.0.7 -set EFCORE3=3.1.0.7 -set EFCORE5=5.1.0.7 +set EFCORE2=2.1.0.8 +set EFCORE3=3.1.0.8 +set EFCORE5=5.1.0.8 ::删除所有bin与obj下的文件 @echo off diff --git a/samples/Sample.MySql/Controllers/WeatherForecastController.cs b/samples/Sample.MySql/Controllers/WeatherForecastController.cs index 2d122f95..93cf5ec9 100644 --- a/samples/Sample.MySql/Controllers/WeatherForecastController.cs +++ b/samples/Sample.MySql/Controllers/WeatherForecastController.cs @@ -6,32 +6,68 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Sample.MySql.Domain.Entities; +using ShardingCore.Core.PhysicTables; +using ShardingCore.Core.VirtualTables; using ShardingCore.DbContexts.VirtualDbContexts; using ShardingCore.Extensions; +using ShardingCore.TableCreator; namespace Sample.MySql.Controllers { [ApiController] - [Route("[controller]")] + [Route("[controller]/[action]")] public class WeatherForecastController : ControllerBase { private readonly IVirtualDbContext _virtualDbContext; + private readonly IVirtualTableManager _virtualTableManager; + private readonly IShardingTableCreator _tableCreator; - public WeatherForecastController(IVirtualDbContext virtualDbContext) + public WeatherForecastController(IVirtualDbContext virtualDbContext,IVirtualTableManager virtualTableManager, IShardingTableCreator tableCreator) { _virtualDbContext = virtualDbContext; + _virtualTableManager = virtualTableManager; + _tableCreator = tableCreator; } [HttpGet] public async Task Get() { + var taleAllTails = _virtualTableManager.GetVirtualTable("conn1",typeof(SysUserLogByMonth)).GetTaleAllTails(); + + var result = await _virtualDbContext.Set().AnyAsync(); var result1 = await _virtualDbContext.Set().Where(o => o.Id == "2" || o.Id == "3").ToShardingListAsync(); + var result2 = await _virtualDbContext.Set().Skip(1).Take(10).ToShardingListAsync(); + var shardingFirstOrDefaultAsync = await _virtualDbContext.Set().ShardingFirstOrDefaultAsync(); var shardingCountAsync = await _virtualDbContext.Set().ShardingCountAsync(); var shardingCountAsyn2c = _virtualDbContext.Set().ShardingCount(); return Ok(result1); } + [HttpGet] + public async Task Get1() + { + var allVirtualTables = _virtualTableManager.GetAllVirtualTables("conn1"); + foreach (var virtualTable in allVirtualTables) + { + if (virtualTable.EntityType == typeof(SysUserLogByMonth)) + { + var now = DateTime.Now.Date.AddMonths(2); + var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now); + try + { + _virtualTableManager.AddPhysicTable("conn1", virtualTable, new DefaultPhysicTable(virtualTable, tail)); + _tableCreator.CreateTable("conn1", tail); + } + catch (Exception e) + { + //ignore + Console.WriteLine(e); + } + } + } + return Ok(); + } } } diff --git a/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs b/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs index 49b5f883..73781409 100644 --- a/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs +++ b/samples/Sample.MySql/Shardings/SysUserLogByMonthRoute.cs @@ -11,7 +11,7 @@ namespace Sample.MySql.Shardings { public override DateTime GetBeginTime() { - return new DateTime(2021, 3, 01); + return new DateTime(2021, 1, 01); } } } diff --git a/samples/Sample.MySql/Startup.cs b/samples/Sample.MySql/Startup.cs index 8152fae1..d1840233 100644 --- a/samples/Sample.MySql/Startup.cs +++ b/samples/Sample.MySql/Startup.cs @@ -33,12 +33,13 @@ namespace Sample.MySql { o.EnsureCreatedWithOutShardingTable = true; o.CreateShardingTableOnStart = true; - o.AddShardingDbContextWithShardingTable("conn1", "server=xxx;userid=xxx;password=xxx;database=sharding_db123;Charset=utf8;Allow Zero Datetime=True; Pooling=true; Max Pool Size=512;sslmode=none;Allow User Variables=True;", dbConfig => + o.AddShardingDbContextWithShardingTable("conn1", "server=106.54.131.109;userid=xjm;password=mysqlXJM#qwe;database=sharding_db123;Charset=utf8;Allow Zero Datetime=True; Pooling=true; Max Pool Size=512;sslmode=none;Allow User Variables=True;", dbConfig => { dbConfig.AddShardingTableRoute(); dbConfig.AddShardingTableRoute(); }); //o.AddDataSourceVirtualRoute<>(); + o.IgnoreCreateTableError = true; }); diff --git a/src/ShardingCore/AbstractShardingCoreOptions.cs b/src/ShardingCore/AbstractShardingCoreOptions.cs index ea49f508..e70bc5cc 100644 --- a/src/ShardingCore/AbstractShardingCoreOptions.cs +++ b/src/ShardingCore/AbstractShardingCoreOptions.cs @@ -121,5 +121,7 @@ namespace ShardingCore { return _filters; } + + public bool? IgnoreCreateTableError { get; set; } } } diff --git a/src/ShardingCore/Core/VirtualTables/OneDbVirtualTable.cs b/src/ShardingCore/Core/VirtualTables/OneDbVirtualTable.cs index 0435e2fa..5fc44ee1 100644 --- a/src/ShardingCore/Core/VirtualTables/OneDbVirtualTable.cs +++ b/src/ShardingCore/Core/VirtualTables/OneDbVirtualTable.cs @@ -88,7 +88,7 @@ namespace ShardingCore.Core.VirtualTables public List GetTaleAllTails() { - return _virtualTableRoute.GetAllTails(); + return _physicTables.Select(o => o.Tail).ToList(); } public IVirtualTableRoute GetVirtualRoute() diff --git a/src/ShardingCore/IShardingCoreOptions.cs b/src/ShardingCore/IShardingCoreOptions.cs index d982bff0..30d278e7 100644 --- a/src/ShardingCore/IShardingCoreOptions.cs +++ b/src/ShardingCore/IShardingCoreOptions.cs @@ -50,5 +50,7 @@ namespace ShardingCore public List GetFilters(); + bool? IgnoreCreateTableError { get; set; } + } } diff --git a/src/ShardingCore/ShardingBootstrapper.cs b/src/ShardingCore/ShardingBootstrapper.cs index 0d5557a2..814acc61 100644 --- a/src/ShardingCore/ShardingBootstrapper.cs +++ b/src/ShardingCore/ShardingBootstrapper.cs @@ -212,12 +212,14 @@ namespace ShardingCore private void CreateDataTable(string connectKey, IVirtualTable virtualTable) { var shardingConfig = virtualTable.ShardingConfig; - foreach (var tail in virtualTable.GetTaleAllTails()) + foreach (var tail in virtualTable.GetVirtualRoute().GetAllTails()) { if (NeedCreateTable(shardingConfig)) { try { + //添加物理表 + virtualTable.AddPhysicTable(new DefaultPhysicTable(virtualTable, tail)); _tableCreator.CreateTable(connectKey, virtualTable.EntityType, tail); } catch (Exception) @@ -227,8 +229,7 @@ namespace ShardingCore } } - //添加物理表 - virtualTable.AddPhysicTable(new DefaultPhysicTable(virtualTable, tail)); + } } } diff --git a/src/ShardingCore/TableCreator/ShardingTableCreator.cs b/src/ShardingCore/TableCreator/ShardingTableCreator.cs index 19d12f19..493ae79d 100644 --- a/src/ShardingCore/TableCreator/ShardingTableCreator.cs +++ b/src/ShardingCore/TableCreator/ShardingTableCreator.cs @@ -26,13 +26,16 @@ namespace ShardingCore.TableCreator private readonly IShardingDbContextFactory _shardingDbContextFactory; private readonly IVirtualTableManager _virtualTableManager; private readonly IServiceProvider _serviceProvider; + private readonly IShardingCoreOptions _shardingCoreOptions; - public ShardingTableCreator(ILogger logger, IShardingDbContextFactory shardingDbContextFactory, IVirtualTableManager virtualTableManager, IServiceProvider serviceProvider) + public ShardingTableCreator(ILogger logger, IShardingDbContextFactory shardingDbContextFactory, + IVirtualTableManager virtualTableManager, IServiceProvider serviceProvider,IShardingCoreOptions shardingCoreOptions) { _logger = logger; _shardingDbContextFactory = shardingDbContextFactory; _virtualTableManager = virtualTableManager; _serviceProvider = serviceProvider; + _shardingCoreOptions = shardingCoreOptions; } public void CreateTable(string connectKey,string tail) where T : class, IShardingTable @@ -67,9 +70,12 @@ namespace ShardingCore.TableCreator } catch (Exception ex) { - _logger.LogWarning( - $"create table error maybe table:[{virtualTable.GetOriginalTableName()}_{virtualTable.ShardingConfig.TailPrefix}_{tail}]"); - throw new ShardingCreateException(" create table error :", ex); + if (!_shardingCoreOptions.IgnoreCreateTableError.GetValueOrDefault()) + { + _logger.LogWarning( + $"create table error maybe table:[{virtualTable.GetOriginalTableName()}{virtualTable.ShardingConfig.TailPrefix}{tail}]"); + throw new ShardingCreateException(" create table error :", ex); + } } finally { diff --git a/src/ShardingCore/VirtualRoutes/Months/AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute.cs b/src/ShardingCore/VirtualRoutes/Months/AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute.cs index 0a233dc0..463202d2 100644 --- a/src/ShardingCore/VirtualRoutes/Months/AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute.cs +++ b/src/ShardingCore/VirtualRoutes/Months/AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute.cs @@ -23,7 +23,7 @@ namespace ShardingCore.VirtualRoutes.Months var tails=new List(); //提前创建表 - var nowTimeStamp =ShardingCoreHelper.GetNextMonthFirstDay(beginTime); + var nowTimeStamp =ShardingCoreHelper.GetNextMonthFirstDay(DateTime.Now); if (beginTime > nowTimeStamp) throw new ArgumentException("起始时间不正确无法生成正确的表名"); var currentTimeStamp = beginTime;