增加批量增加分库功能
This commit is contained in:
parent
f87c6d7d2d
commit
a66c3ff78c
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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,则生成的编号为:001,002,003,004,005
|
||||
</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>
|
|
@ -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;
|
||||
|
||||
}
|
||||
}
|
|
@ -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>
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
}
|
||||
|
||||
<a asp-page="CreateDbKey">新增分库key</a>
|
||||
<a asp-page="BatchCreateDbKey">批量新增分库key</a>
|
||||
|
||||
<table class="table">
|
||||
<thead>
|
||||
|
|
Loading…
Reference in New Issue