commit
3e1b7e94d3
|
@ -33,6 +33,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MongoDB", "MongoDB", "{A06A
|
|||
db\MongoDB\Dicts.js = db\MongoDB\Dicts.js
|
||||
db\MongoDB\Groups.js = db\MongoDB\Groups.js
|
||||
db\MongoDB\init.js = db\MongoDB\init.js
|
||||
db\MongoDB\install.cmd = db\MongoDB\install.cmd
|
||||
db\MongoDB\install.sh = db\MongoDB\install.sh
|
||||
db\MongoDB\Navigations.js = db\MongoDB\Navigations.js
|
||||
db\MongoDB\Roles.js = db\MongoDB\Roles.js
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
# init mongodb data
|
||||
@echo off
|
||||
|
||||
mongo ./init.js
|
||||
mongo BootstrapAdmin --eval "printjson(db.getCollectionNames())"
|
|
@ -1,5 +1,9 @@
|
|||
# init sqlserver database
|
||||
$startPath = "Z:\src\Longbow\BootstrapAdmin\DatabaseScripts\SqlServer"
|
||||
$startPath = $args[0]
|
||||
if ($startPath -eq $null) {
|
||||
$startPath = "Z:\src\Longbow\BootstrapAdmin\db\SqlServer"
|
||||
}
|
||||
|
||||
$sqlInstance = "localhost"
|
||||
$outFile = join-path $startPath "output.log"
|
||||
$sqlFile = join-path $startPath "Install.sql"
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
using Bootstrap.DataAccess.Helper;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
namespace Microsoft.AspNetCore.Builder
|
||||
{
|
||||
/// <summary>
|
||||
/// 自动生成数据库扩展操作类
|
||||
/// </summary>
|
||||
public static class AutoGenerateDatabaseExtensions
|
||||
{
|
||||
private static bool _init;
|
||||
private static object _locker = new object();
|
||||
|
||||
/// <summary>
|
||||
/// 自动生成数据库中间件
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
public static IApplicationBuilder UseAutoGenerateDatabase(this IApplicationBuilder app)
|
||||
{
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
if (!_init)
|
||||
{
|
||||
// 阻止所有线程继续往下运行,等待数据库检查
|
||||
lock (_locker)
|
||||
{
|
||||
if (!_init)
|
||||
{
|
||||
// 数据检查
|
||||
var config = context.RequestServices.GetRequiredService<IConfiguration>();
|
||||
var dbSection = config.GetSection("DB").GetChildren().FirstOrDefault(c => c.GetValue("Enabled", false));
|
||||
if (dbSection != null)
|
||||
{
|
||||
var folder = dbSection["SqlFolder"].ReplaceOSPlatformPath();
|
||||
if (!string.IsNullOrEmpty(folder))
|
||||
{
|
||||
// 判断文件夹是否存在
|
||||
var env = context.RequestServices.GetRequiredService<IWebHostEnvironment>();
|
||||
var fullFolder = Path.Combine(env.ContentRootPath, folder);
|
||||
if (Directory.Exists(fullFolder))
|
||||
{
|
||||
try
|
||||
{
|
||||
AutoDbHelper.CheckDB(fullFolder);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_init = true;
|
||||
}
|
||||
}
|
||||
await next();
|
||||
});
|
||||
return app;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -13,16 +13,19 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Bootstrap.Security.DataAccess" Version="2.2.21" />
|
||||
<PackageReference Include="Bootstrap.Security.Mvc" Version="3.0.1-beta5" />
|
||||
<PackageReference Include="Longbow" Version="3.0.0-beta1" />
|
||||
<PackageReference Include="Longbow.Configuration" Version="2.2.7" />
|
||||
<PackageReference Include="Longbow.Logging" Version="3.0.0" />
|
||||
<PackageReference Include="Longbow.Tasks" Version="2.2.24" />
|
||||
<PackageReference Include="Longbow.Web" Version="3.0.0-beta1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.0.0-preview8.19405.7" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson" Version="3.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
|
||||
<PackageReference Include="Sentry.AspNetCore" Version="2.0.0-beta4" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" />
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.7.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -16,6 +16,11 @@
|
|||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Docker": {
|
||||
"commandName": "Docker",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}"
|
||||
},
|
||||
"Bootstrap.Admin": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
|
@ -23,11 +28,6 @@
|
|||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"applicationUrl": "http://localhost:50852/"
|
||||
},
|
||||
"Docker": {
|
||||
"commandName": "Docker",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -115,6 +115,7 @@ namespace Bootstrap.Admin
|
|||
app.UseHttpsRedirection();
|
||||
app.UseResponseCompression();
|
||||
app.UseStaticFiles();
|
||||
app.UseAutoGenerateDatabase();
|
||||
app.UseRouting();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
{
|
||||
"Enabled": false,
|
||||
"ProviderName": "SqlServer",
|
||||
"SqlFolder": "..\\..\\..\\db\\SqlServer",
|
||||
"ConnectionStrings": {
|
||||
"ba": "Data Source=.;Initial Catalog=BootstrapAdmin;User ID=sa;Password=sa"
|
||||
}
|
||||
|
@ -26,6 +27,7 @@
|
|||
{
|
||||
"Enabled": true,
|
||||
"ProviderName": "Sqlite",
|
||||
"SqlFolder": "..\\..\\..\\db\\SQLite",
|
||||
"ConnectionStrings": {
|
||||
"ba": "Data Source=BootstrapAdmin.db;"
|
||||
}
|
||||
|
@ -33,6 +35,7 @@
|
|||
{
|
||||
"Enabled": false,
|
||||
"ProviderName": "MySql",
|
||||
"SqlFolder": "..\\..\\..\\db\\MySQL",
|
||||
"ConnectionStrings": {
|
||||
"ba": "Server=localhost;Database=BA;Uid=argozhang;Pwd=argo@163.com;SslMode=none;"
|
||||
}
|
||||
|
@ -48,6 +51,7 @@
|
|||
"Enabled": false,
|
||||
"Widget": "Bootstrap.DataAccess.MongoDB",
|
||||
"ProviderName": "MongoDB",
|
||||
"SqlFolder": "..\\..\\..\\db\\MongoDB",
|
||||
"ConnectionStrings": {
|
||||
"ba": "mongodb://localhost:27017/BootstrapAdmin"
|
||||
}
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
namespace Bootstrap.DataAccess.MongoDB
|
||||
{
|
||||
/// <summary>
|
||||
/// 自动建库实体操作类
|
||||
/// </summary>
|
||||
public class AutoDB : DataAccess.AutoDB
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据库检查方法
|
||||
/// <paramref name="folder"></paramref>
|
||||
/// </summary>
|
||||
public override void CheckDB(string folder)
|
||||
{
|
||||
// UNDONE: 没有环境暂时未写代码
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
using PetaPoco;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
||||
namespace Bootstrap.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据库自动生成实体类
|
||||
/// </summary>
|
||||
public class AutoDB
|
||||
{
|
||||
private string _folder;
|
||||
|
||||
/// <summary>
|
||||
/// 数据库检查方法
|
||||
/// </summary>
|
||||
public virtual void CheckDB(string folder)
|
||||
{
|
||||
_folder = folder;
|
||||
using (var db = Longbow.Data.DbManager.Create())
|
||||
{
|
||||
db.CommandTimeout = 5000;
|
||||
switch (db.Provider.GetType().Name)
|
||||
{
|
||||
case "SQLiteDatabaseProvider":
|
||||
if (db.ExecuteScalar<int>("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='Users'") == 0) GenerateSQLiteDB(db);
|
||||
break;
|
||||
case "SqlServerDatabaseProvider":
|
||||
using (var newDB = ModifyConnectionString(db))
|
||||
{
|
||||
if (newDB.ExecuteScalar<int?>("select COUNT(1) from sys.databases where name = N'BootstrapAdmin'") == 0) GenerateSqlServer();
|
||||
}
|
||||
break;
|
||||
case "MySqlDatabaseProvider":
|
||||
case "MariaDbDatabaseProvider":
|
||||
// UNDONE: 本地没有环境此处代码未测试
|
||||
if (db.ExecuteScalar<int>("select count(*) from information_schema.tables where table_name ='Users' and Table_Schema = 'BootstrapAdmin'") == 0) GenerateMySql();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IDatabase ModifyConnectionString(IDatabase db)
|
||||
{
|
||||
var conn = db.ConnectionString;
|
||||
var newsegs = new List<string>();
|
||||
var segs = conn.SpanSplit(";");
|
||||
segs.ForEach(s =>
|
||||
{
|
||||
if (s.StartsWith("Initial Catalog", StringComparison.OrdinalIgnoreCase)) newsegs.Add("Initial Catalog=master");
|
||||
else newsegs.Add(s);
|
||||
});
|
||||
var provider = db.Provider;
|
||||
return new Database(string.Join(";", newsegs), provider);
|
||||
}
|
||||
|
||||
private void GenerateSQLiteDB(IDatabase db)
|
||||
{
|
||||
var folder = _folder;
|
||||
var initFile = Path.Combine(folder, "Install.sql");
|
||||
if (File.Exists(initFile))
|
||||
{
|
||||
var sql = File.ReadAllText(initFile);
|
||||
db.Execute(sql);
|
||||
|
||||
initFile = Path.Combine(folder, "InitData.sql");
|
||||
if (File.Exists(initFile))
|
||||
{
|
||||
sql = File.ReadAllText(initFile);
|
||||
db.Execute(sql);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void GenerateSqlServer()
|
||||
{
|
||||
// 检查 install.ps1 脚本
|
||||
var file = Path.Combine(_folder, $"install.ps1");
|
||||
if (File.Exists(file))
|
||||
{
|
||||
var psi = new ProcessStartInfo("powershell", $"{file} \"{_folder}\"");
|
||||
var p = Process.Start(psi);
|
||||
p.WaitForExit();
|
||||
}
|
||||
}
|
||||
|
||||
private void GenerateMySql()
|
||||
{
|
||||
// UNDONE: 没有环境暂时未写代码
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
namespace Bootstrap.DataAccess.Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据库自动生成帮助类
|
||||
/// </summary>
|
||||
public static class AutoDbHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 数据库检查方法
|
||||
/// </summary>
|
||||
public static void CheckDB(string folder) => DbContextManager.Create<AutoDB>()?.CheckDB(folder);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue