[#100]添加CreateDataBaseOnlyOnStart字段用于启动仅创建数据库,发布x.4.1.01

This commit is contained in:
xuejiaming 2022-01-25 23:34:11 +08:00
parent c05a2d8920
commit 6972d428b9
13 changed files with 79 additions and 64 deletions

View File

@ -1,9 +1,9 @@
:start
::定义版本
set EFCORE2=2.4.1.01-rc1
set EFCORE3=3.4.1.01-rc1
set EFCORE5=5.4.1.01-rc1
set EFCORE6=6.4.1.01-rc1
set EFCORE2=2.4.1.01
set EFCORE3=3.4.1.01
set EFCORE5=5.4.1.01
set EFCORE6=6.4.1.01
::删除所有bin与obj下的文件
@echo off

View File

@ -21,8 +21,9 @@ namespace Sample.Migrations
services.AddShardingDbContext<DefaultShardingTableDbContext>()
.AddEntityConfig(o =>
{
o.CreateShardingTableOnStart = true;
o.EnsureCreatedWithOutShardingTable = true;
o.CreateShardingTableOnStart = false;
o.CreateDataBaseOnlyOnStart = true;
o.EnsureCreatedWithOutShardingTable = false;
o.AddShardingTableRoute<ShardingWithModVirtualTableRoute>();
o.AddShardingTableRoute<ShardingWithDateTimeVirtualTableRoute>();
})

View File

@ -9,11 +9,10 @@ namespace Samples.AutoByDate.SqlServer.Domain.Entities
* @Date: Tuesday, 02 February 2021 17:00:36
* @Email: 326308290@qq.com
*/
public class SysUserLogByDay:IShardingTable
public class SysUserLogByDay
{
public int Id { get; set; }
public string Body { get; set; }
[ShardingTableKey]
public DateTime CreateTime { get; set; }
}
}

View File

@ -1,49 +0,0 @@
using System;
using ChronusJob.Abstractions;
using ChronusJob.Jobs.Attributes;
using Samples.AutoByDate.SqlServer.DbContexts;
using Samples.AutoByDate.SqlServer.Domain.Entities;
using ShardingCore;
using ShardingCore.Core.PhysicTables;
using ShardingCore.Core.VirtualDatabase.VirtualTables;
using ShardingCore.Core.VirtualTables;
using ShardingCore.TableCreator;
namespace Samples.AutoByDate.SqlServer.Jobs
{
/*
* @Author: xjm
* @Description:
* @Date: Tuesday, 02 February 2021 17:24:17
* @Email: 326308290@qq.com
*/
public class AutoCreateTableByDay : IJob
{
/// <summary>
/// 每天中午12点执行,启动的时候执行以下
/// </summary>
/// <param name="virtualTableManager"></param>
/// <param name="tableCreator"></param>
[JobRun(Name = "定时创建分表组件", Cron = "0 0 12 * * ?", RunOnceOnStart = true)]
public void AutoCreateTable(IVirtualTableManager<DefaultShardingDbContext> virtualTableManager, IShardingTableCreator<DefaultShardingDbContext> tableCreator)
{
var virtualTable = virtualTableManager.GetVirtualTable(typeof(SysUserLogByDay));
if (virtualTable == null)
{
return;
}
var now = DateTime.Now.Date.AddDays(1);
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
try
{
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
tableCreator.CreateTable("ds0", typeof(SysUserLogByDay), tail);
}
catch (Exception e)
{
//ignore
}
}
}
}

View File

@ -5,7 +5,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ChronusJob" Version="1.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
</ItemGroup>

View File

@ -28,7 +28,7 @@ namespace Samples.AutoByDate.SqlServer.Shardings
public override void Configure(EntityMetadataTableBuilder<SysUserLogByDay> builder)
{
builder.ShardingProperty(o => o.CreateTime);
}
}
}

View File

@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChronusJob;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
@ -50,9 +49,8 @@ namespace Samples.AutoByDate.SqlServer
{
builder.UseSqlServer(connection);
});
sp.AddDefaultDataSource("ds0", "Data Source=localhost;Initial Catalog=ShardingCoreDB;Integrated Security=True;");
sp.AddDefaultDataSource("ds0", "Data Source=localhost;Initial Catalog=ShardingCoreDBz;Integrated Security=True;");
}).EnsureConfig();
services.AddChronusJob();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -20,6 +20,10 @@ namespace ShardingCore.Core.ShardingConfigurations.Abstractions
/// </summary>
bool? CreateShardingTableOnStart { get; set; }
/// <summary>
/// 是否在启动时创建数据库
/// </summary>
public bool? CreateDataBaseOnlyOnStart { get; set; }
/// <summary>
/// 当查询遇到没有路由被命中时是否抛出错误
/// </summary>
bool ThrowIfQueryRouteNotMatch { get; set; }

View File

@ -31,6 +31,10 @@ namespace ShardingCore.Core.ShardingConfigurations
/// </summary>
public bool? CreateShardingTableOnStart { get; set; }
/// <summary>
/// 是否在启动时创建数据库
/// </summary>
public bool? CreateDataBaseOnlyOnStart { get; set; }
/// <summary>
/// 当查询遇到没有路由被命中时是否抛出错误
/// </summary>
public bool ThrowIfQueryRouteNotMatch { get; set; } = true;

View File

@ -55,6 +55,10 @@ namespace ShardingCore.DynamicDataSources
using var context = serviceScope.ServiceProvider.GetService<TShardingDbContext>();
if (_entityConfigOptions.EnsureCreatedWithOutShardingTable || !isOnStart)
EnsureCreated(virtualDataSource, context, dataSourceName);
else if(_entityConfigOptions.CreateDataBaseOnlyOnStart.GetValueOrDefault())
{
EnsureCreateDataBaseOnly(context, dataSourceName);
}
var tableEnsureManager = virtualDataSource.ConfigurationParams.TableEnsureManager;
////获取数据库存在的所有的表
var existTables = tableEnsureManager?.GetExistTables(context, dataSourceName) ?? new HashSet<string>();
@ -182,6 +186,33 @@ namespace ShardingCore.DynamicDataSources
}
}
}
private void EnsureCreateDataBaseOnly( DbContext context, string dataSourceName)
{
if (context is IShardingDbContext shardingDbContext)
{
var dbContext = shardingDbContext.GetDbContext(dataSourceName, false, _routeTailFactory.Create(string.Empty));
var modelCacheSyncObject = dbContext.GetModelCacheSyncObject();
var acquire = Monitor.TryEnter(modelCacheSyncObject, TimeSpan.FromSeconds(3));
if (!acquire)
{
throw new ShardingCoreException("cant get modelCacheSyncObject lock");
}
try
{
dbContext.RemoveDbContextAllRelationModel();
dbContext.Database.EnsureCreated();
dbContext.RemoveModelCache();
}
finally
{
Monitor.Exit(modelCacheSyncObject);
}
}
}
}
}

View File

@ -140,6 +140,35 @@ namespace ShardingCore.Extensions
}
#endif
}
/// <summary>
/// 移除所有的表
/// </summary>
/// <param name="dbContext"></param>
public static void RemoveDbContextAllRelationModel(this DbContext dbContext)
{
#if EFCORE6
var contextModel = dbContext.GetService<IDesignTimeModel>().Model; ;
#endif
#if EFCORE2 || EFCORE3 || EFCORE5
var contextModel = dbContext.Model as Model;
#endif
#if EFCORE6
var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel;
contextModelRelationalModel.Tables.Clear();
#endif
#if EFCORE5
var contextModelRelationalModel = contextModel.RelationalModel as RelationalModel;
contextModelRelationalModel.Tables.Clear();
#endif
#if EFCORE2 || EFCORE3
var entityTypes =
contextModel.GetFieldValue("_entityTypes") as SortedDictionary<string, EntityType>;
entityTypes.Clear();
#endif
}
/// <summary>
/// 移除所有的除了我指定的那个类型

View File

@ -45,7 +45,6 @@ namespace ShardingCore.TableCreator
/// <param name="dataSourceName"></param>
/// <param name="shardingEntityType"></param>
/// <param name="tail"></param>
/// <exception cref="ShardingCreateException"></exception>
public void CreateTable(string dataSourceName, Type shardingEntityType, string tail)
{
using (var serviceScope = _serviceProvider.CreateScope())

View File

@ -70,6 +70,7 @@ namespace ShardingCore.VirtualRoutes.Abstractions
var allVirtualDataSources = virtualDataSourceManager.GetAllVirtualDataSources();
var now = DateTime.Now.AddMinutes(IncrementMinutes);
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
foreach (var virtualDataSource in allVirtualDataSources)
{
ISet<string> dataSources = new HashSet<string>();
@ -103,7 +104,6 @@ namespace ShardingCore.VirtualRoutes.Abstractions
}
}
}
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
return Task.CompletedTask;
}