修复建表bug和忽略建表错误

This commit is contained in:
xuejiaming 2021-03-23 17:42:37 +08:00
parent 04350e78d4
commit b40075be47
10 changed files with 64 additions and 16 deletions

View File

@ -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

View File

@ -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<IActionResult> Get()
{
var taleAllTails = _virtualTableManager.GetVirtualTable("conn1",typeof(SysUserLogByMonth)).GetTaleAllTails();
var result = await _virtualDbContext.Set<SysTest>().AnyAsync();
var result1 = await _virtualDbContext.Set<SysUserMod>().Where(o => o.Id == "2" || o.Id == "3").ToShardingListAsync();
var result2 = await _virtualDbContext.Set<SysUserLogByMonth>().Skip(1).Take(10).ToShardingListAsync();
var shardingFirstOrDefaultAsync = await _virtualDbContext.Set<SysUserLogByMonth>().ShardingFirstOrDefaultAsync();
var shardingCountAsync = await _virtualDbContext.Set<SysUserMod>().ShardingCountAsync();
var shardingCountAsyn2c = _virtualDbContext.Set<SysUserLogByMonth>().ShardingCount();
return Ok(result1);
}
[HttpGet]
public async Task<IActionResult> 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<SysUserLogByMonth>("conn1", tail);
}
catch (Exception e)
{
//ignore
Console.WriteLine(e);
}
}
}
return Ok();
}
}
}

View File

@ -11,7 +11,7 @@ namespace Sample.MySql.Shardings
{
public override DateTime GetBeginTime()
{
return new DateTime(2021, 3, 01);
return new DateTime(2021, 1, 01);
}
}
}

View File

@ -33,12 +33,13 @@ namespace Sample.MySql
{
o.EnsureCreatedWithOutShardingTable = true;
o.CreateShardingTableOnStart = true;
o.AddShardingDbContextWithShardingTable<DefaultTableDbContext>("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<DefaultTableDbContext>("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<SysUserModVirtualTableRoute>();
dbConfig.AddShardingTableRoute<SysUserLogByMonthRoute>();
});
//o.AddDataSourceVirtualRoute<>();
o.IgnoreCreateTableError = true;
});

View File

@ -121,5 +121,7 @@ namespace ShardingCore
{
return _filters;
}
public bool? IgnoreCreateTableError { get; set; }
}
}

View File

@ -88,7 +88,7 @@ namespace ShardingCore.Core.VirtualTables
public List<string> GetTaleAllTails()
{
return _virtualTableRoute.GetAllTails();
return _physicTables.Select(o => o.Tail).ToList();
}
public IVirtualTableRoute<T> GetVirtualRoute()

View File

@ -50,5 +50,7 @@ namespace ShardingCore
public List<Type> GetFilters();
bool? IgnoreCreateTableError { get; set; }
}
}

View File

@ -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));
}
}
}

View File

@ -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<ShardingTableCreator> logger, IShardingDbContextFactory shardingDbContextFactory, IVirtualTableManager virtualTableManager, IServiceProvider serviceProvider)
public ShardingTableCreator(ILogger<ShardingTableCreator> logger, IShardingDbContextFactory shardingDbContextFactory,
IVirtualTableManager virtualTableManager, IServiceProvider serviceProvider,IShardingCoreOptions shardingCoreOptions)
{
_logger = logger;
_shardingDbContextFactory = shardingDbContextFactory;
_virtualTableManager = virtualTableManager;
_serviceProvider = serviceProvider;
_shardingCoreOptions = shardingCoreOptions;
}
public void CreateTable<T>(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
{

View File

@ -23,7 +23,7 @@ namespace ShardingCore.VirtualRoutes.Months
var tails=new List<string>();
//提前创建表
var nowTimeStamp =ShardingCoreHelper.GetNextMonthFirstDay(beginTime);
var nowTimeStamp =ShardingCoreHelper.GetNextMonthFirstDay(DateTime.Now);
if (beginTime > nowTimeStamp)
throw new ArgumentException("起始时间不正确无法生成正确的表名");
var currentTimeStamp = beginTime;