Skip to content

Commit

Permalink
feat(#I12XME): MongoDB 自动建库
Browse files Browse the repository at this point in the history
  • Loading branch information
ArgoZhang committed Nov 16, 2019
1 parent 4129890 commit bf52b8b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion BootstrapAdmin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +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.ps1 = db\MongoDB\install.ps1
db\MongoDB\install.sh = db\MongoDB\install.sh
db\MongoDB\Navigations.js = db\MongoDB\Navigations.js
db\MongoDB\Roles.js = db\MongoDB\Roles.js
Expand Down
File renamed without changes.
8 changes: 6 additions & 2 deletions src/admin/Bootstrap.DataAccess.MongoDB/AutoDB.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Bootstrap.DataAccess.MongoDB
using Bootstrap.Security;
using MongoDB.Driver;

namespace Bootstrap.DataAccess.MongoDB
{
/// <summary>
/// 自动建库实体操作类
Expand All @@ -11,7 +14,8 @@ public class AutoDB : DataAccess.AutoDB
/// </summary>
public override void EnsureCreated(string folder)
{
// UNDONE: 没有环境暂时未写代码
// 检查数据库是否存在
if (DbManager.Dicts.CountDocuments(FilterDefinition<BootstrapDict>.Empty) == 0) GenerateDB(folder);
}
}
}
22 changes: 10 additions & 12 deletions src/admin/Bootstrap.DataAccess/AutoDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,27 @@ namespace Bootstrap.DataAccess
/// </summary>
public class AutoDB
{
private string _folder = "";

/// <summary>
/// 数据库检查方法
/// </summary>
public virtual void EnsureCreated(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);
if (db.ExecuteScalar<int>("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='Users'") == 0) GenerateSQLiteDB(db, folder);
break;
case "SqlServerDatabaseProvider":
using (var newDB = ModifyConnectionString(db))
{
if (newDB.ExecuteScalar<int?>("select COUNT(*) from sys.databases where name = N'BootstrapAdmin'") == 0) GenerateDB();
if (newDB.ExecuteScalar<int?>("select COUNT(*) from sys.databases where name = N'BootstrapAdmin'") == 0) GenerateDB(folder);
}
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) GenerateDB();
if (db.ExecuteScalar<int>("select count(*) from information_schema.tables where table_name ='Users' and Table_Schema = 'BootstrapAdmin'") == 0) GenerateDB(folder);
break;
}
}
Expand All @@ -54,9 +50,8 @@ private IDatabase ModifyConnectionString(IDatabase db)
return new Database(string.Join(";", newsegs), provider);
}

private void GenerateSQLiteDB(IDatabase db)
private void GenerateSQLiteDB(IDatabase db, string folder)
{
var folder = _folder;
var initFile = Path.Combine(folder, "Install.sql");
if (File.Exists(initFile))
{
Expand All @@ -72,13 +67,16 @@ private void GenerateSQLiteDB(IDatabase db)
}
}

private void GenerateDB()
/// <summary>
/// 执行建库脚本
/// </summary>
protected void GenerateDB(string folder)
{
// 检查 install.ps1 脚本
var file = Path.Combine(_folder, $"install.ps1");
var file = Path.Combine(folder, $"install.ps1");
if (File.Exists(file))
{
var psi = new ProcessStartInfo("powershell", $"{file} \"{_folder}\"");
var psi = new ProcessStartInfo("powershell", $"{file} \"{folder}\"");
var p = Process.Start(psi);
p.WaitForExit();
}
Expand Down

0 comments on commit bf52b8b

Please sign in to comment.