增加批量增加分库功能

This commit is contained in:
tnt group 2022-06-23 09:30:44 +08:00
parent f87c6d7d2d
commit a66c3ff78c
6 changed files with 136 additions and 40 deletions

View File

@ -1,9 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using ShardingCore.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using WebApplication1.Data;
using WebApplication1.Data.Helpers;
using WebApplication1.Data.Sharding;
@ -18,27 +16,23 @@ namespace WebApplication1.Data.Extensions
/// <param name="serviceProvider"></param>
public static IServiceProvider InitialDynamicVirtualDataSource(this IServiceProvider serviceProvider)
{
var dblist = JsonFileHelper.Read<List<string>>(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName) ?? new List<string>();
foreach (var key in dblist)
using (var scope = serviceProvider.CreateScope())
{
DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", key, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{key};");
var db = scope.ServiceProvider.GetRequiredService<AbstaractShardingDbContext>();
db.Database.EnsureCreated();
var dblist = db.TestModelKeys.Select(m => m.Key).ToList();
// 存入到动态库配置文件缓存中
JsonFileHelper.Save(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName, dblist);
// 遍历添加动态数据源
foreach (var key in dblist)
{
DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", key, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{key};");
}
}
return serviceProvider;
//using (var scope = serviceProvider.CreateScope())
//{
// var db = scope.ServiceProvider.GetRequiredService<AbstaractShardingDbContext>();
// db.Database.EnsureCreated();
// var dbKeys = db.TestModelKeys.ToList();
// if (dbKeys.Any())
// {
// foreach (var item in dbKeys)
// {
// DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", item.Key, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{item.Key};");
// }
// }
//}
return serviceProvider;
}
}

View File

@ -0,0 +1,40 @@
@page
@model BatchCreateDbKeyModel
@{
ViewData["title"] = "批量增加分库key";
}
<form asp-page="BatchCreateDbKey" method="post">
<div class="mb-3 row">
<label asp-for="CreateModel.Len" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="CreateModel.Len" placeholder="编号长度,左侧自动补零" class="form-control" required />
<div id="emailHelp" class="form-text">编号长度,编号长度不够左侧自动补零.</div>
</div>
</div>
<div class="mb-3 row">
<label asp-for="CreateModel.StarNum" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="CreateModel.StarNum" class="form-control" placeholder="分库key必填" required />
<div id="emailHelp" class="form-text">起始编号,数值型</div>
</div>
</div>
<div class="mb-3 row">
<label asp-for="CreateModel.EndNum" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="CreateModel.EndNum" class="form-control" required />
<div id="emailHelp" class="form-text">结束编号,数值型.</div>
</div>
</div>
<div class="mb-3 row">
<div class="offset-sm-2">
例如长度为3起始为1结束为5则生成的编号为001002003004005
</div>
</div>
<div class="row">
<div class="offset-sm-2">
<button type="submit" class="btn btn-primary">批量新增</button>
<a asp-page="DbKeyMan" class="btn btn-secondary">返回</a>
</div>
</div>
</form>

View File

@ -0,0 +1,74 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using ShardingCore.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using WebApplication1.Data;
using WebApplication1.Data.Helpers;
using WebApplication1.Data.Models;
using WebApplication1.Data.Sharding;
namespace WebApplication1.Pages
{
public class BatchCreateDbKeyModel : PageModel
{
private readonly AbstaractShardingDbContext db;
[BindProperty]
public BatchCreateModel CreateModel { get; set; }
public BatchCreateDbKeyModel(AbstaractShardingDbContext db)
{
this.db = db;
}
public void OnGet()
{
CreateModel = new BatchCreateModel();
}
public IActionResult OnPost()
{
var keyList = new List<string>();
for (int i = CreateModel.StarNum; i < CreateModel.EndNum; i++)
{
keyList.Add(i.ToString().PadLeft(CreateModel.Len, '0'));
}
foreach (var item in keyList)
{
db.TestModelKeys.Add(new TestModelKey { Key = item });
}
db.SaveChanges();
foreach (var item in keyList)
{
// 动态新增数据源
DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", item, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{item};");
}
// 读取并写入到配置
var dblist = JsonFileHelper.Read<List<string>>(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName);
dblist.AddRange(keyList);
JsonFileHelper.Save(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName, dblist);
return RedirectToPage("DbKeyMan");
}
}
public class BatchCreateModel
{
[Display(Name = "编号长度")]
public int Len { get; set; } = 3;
[Display(Name = "起始编号")]
public int StarNum { get; set; } = 1;
[Display(Name = "结束编号")]
public int EndNum { get; set; } = 300;
}
}

View File

@ -6,23 +6,12 @@
<form asp-page="CreateDbKey" method="post">
<div class="mb-3 row">
<label asp-for="NewModel.Id" class="col-sm-2 col-form-label"></label>
<label asp-for="Key" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="NewModel.Id" class="form-control" readonly />
</div>
</div>
<div class="mb-3 row">
<label asp-for="NewModel.Key" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="NewModel.Key" class="form-control" placeholder="分库key必填" required />
</div>
</div>
<div class="mb-3 row">
<label asp-for="NewModel.CreationDate" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="NewModel.CreationDate" class="form-control" readonly />
<input asp-for="Key" class="form-control" placeholder="分库key必填" required />
</div>
</div>
<div class="row">
<div class="offset-sm-2">
<button type="submit" class="btn btn-primary">提交</button>

View File

@ -16,7 +16,7 @@ namespace WebApplication1.Pages
private readonly AbstaractShardingDbContext db;
[BindProperty]
public TestModelKey NewModel { get; set; }
public string Key { get; set; }
public CreateDbKeyModel(AbstaractShardingDbContext db)
{
@ -25,24 +25,22 @@ namespace WebApplication1.Pages
public void OnGet()
{
NewModel = new TestModelKey();
Key = "";
}
public IActionResult OnPost()
{
NewModel.Id = Guid.NewGuid();
NewModel.CreationDate = DateTime.Now;
db.TestModelKeys.Add(NewModel);
db.TestModelKeys.Add(new TestModelKey { Key = Key });
db.SaveChanges();
// 读取并写入到配置
var dblist = JsonFileHelper.Read<List<string>>(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName);
dblist.Add(NewModel.Key);
dblist.Add(Key);
dblist.Sort();
JsonFileHelper.Save(AppContext.BaseDirectory, TestModelVirtualDataSourceRoute.ConfigFileName, dblist);
// 动态新增数据源
DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", NewModel.Key, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{NewModel.Key};");
DynamicShardingHelper.DynamicAppendDataSource<AbstaractShardingDbContext>("c1", Key, $"server=127.0.0.1;port=5432;uid=postgres;pwd=3#SanJing;database=shardingCoreDemo_{Key};");
return RedirectToPage("DbKeyMan");
}

View File

@ -5,6 +5,7 @@
}
<a asp-page="CreateDbKey">新增分库key</a>
<a asp-page="BatchCreateDbKey">批量新增分库key</a>
<table class="table">
<thead>