refactor: 重构自动建库逻辑

This commit is contained in:
Argo Window10 2019-11-16 13:32:23 +08:00
parent 5f5f2f53e2
commit 2742b459e9
8 changed files with 30 additions and 25 deletions

View File

@ -5,7 +5,6 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{

View File

@ -2,9 +2,9 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using System.Linq;
using System;
namespace Microsoft.AspNetCore.Builder
{
@ -26,34 +26,40 @@ namespace Microsoft.AspNetCore.Builder
{
if (!_init)
{
// 阻止所有线程继续往下运行,等待数据库检查
lock (_locker)
// 优化性能
var config = context.RequestServices.GetRequiredService<IConfiguration>();
var autoGenerate = config.GetValue("AutoGenerateDatabase", false);
if (!autoGenerate) _init = true;
if (autoGenerate)
{
if (!_init)
// 阻止所有线程继续往下运行,等待数据库检查
lock (_locker)
{
// 数据检查
var config = context.RequestServices.GetRequiredService<IConfiguration>();
var dbSection = config.GetSection("DB").GetChildren().FirstOrDefault(c => c.GetValue("Enabled", false));
if (dbSection != null)
if (!_init)
{
var folder = dbSection[SqlFolderKey]?.ReplaceOSPlatformPath();
if (!string.IsNullOrEmpty(folder))
// 数据检查
var dbSection = config.GetSection("DB").GetChildren().FirstOrDefault(c => c.GetValue("Enabled", false));
if (dbSection != null)
{
// 判断文件夹是否存在
var env = context.RequestServices.GetRequiredService<IWebHostEnvironment>();
var fullFolder = Path.Combine(env.ContentRootPath, folder);
if (Directory.Exists(fullFolder))
var folder = dbSection.GetValue(SqlFolderKey, "").ReplaceOSPlatformPath();
if (!string.IsNullOrEmpty(folder))
{
try
// 判断文件夹是否存在
var env = context.RequestServices.GetRequiredService<IWebHostEnvironment>();
var fullFolder = Path.Combine(env.ContentRootPath, folder);
if (Directory.Exists(fullFolder))
{
AutoDbHelper.CheckDB(fullFolder);
try
{
AutoDbHelper.EnsureCreated(fullFolder);
}
catch { }
}
catch { }
}
}
}
_init = true;
}
_init = true;
}
}
await next();

View File

@ -1,10 +1,8 @@
using Bootstrap.DataAccess;
using Longbow.Web;
using Longbow.Web.SignalR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

View File

@ -21,6 +21,7 @@
"Url": "https://client.sdgxgz.com/api/Interface/Log"
}
},
"AutoGenerateDatabase": true,
"DB": [
{
"Enabled": false,

View File

@ -21,6 +21,7 @@
"ConnectionStrings": {
"ba": "Data Source=.;Initial Catalog=BootstrapAdmin;User ID=sa;Password=sa"
},
"AutoGenerateDatabase": false,
"DB": [
{
"Enabled": false,

View File

@ -9,7 +9,7 @@
/// 数据库检查方法
/// <paramref name="folder"></paramref>
/// </summary>
public override void CheckDB(string folder)
public override void EnsureCreated(string folder)
{
// UNDONE: 没有环境暂时未写代码
}

View File

@ -16,7 +16,7 @@ namespace Bootstrap.DataAccess
/// <summary>
/// 数据库检查方法
/// </summary>
public virtual void CheckDB(string folder)
public virtual void EnsureCreated(string folder)
{
_folder = folder;
using var db = Longbow.Data.DbManager.Create();
@ -29,7 +29,7 @@ namespace Bootstrap.DataAccess
case "SqlServerDatabaseProvider":
using (var newDB = ModifyConnectionString(db))
{
if (newDB.ExecuteScalar<int?>("select COUNT(1) from sys.databases where name = N'BootstrapAdmin'") == 0) GenerateSqlServer();
if (newDB.ExecuteScalar<int?>("select COUNT(*) from sys.databases where name = N'BootstrapAdmin'") == 0) GenerateSqlServer();
}
break;
case "MySqlDatabaseProvider":

View File

@ -8,6 +8,6 @@
/// <summary>
/// 数据库检查方法
/// </summary>
public static void CheckDB(string folder) => DbContextManager.Create<AutoDB>()?.CheckDB(folder);
public static void EnsureCreated(string folder) => DbContextManager.Create<AutoDB>()?.EnsureCreated(folder);
}
}