diff --git a/BootstrapAdmin.sln b/BootstrapAdmin.sln index b57df96b..f41fec3f 100644 --- a/BootstrapAdmin.sln +++ b/BootstrapAdmin.sln @@ -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 diff --git a/db/MongoDB/install.cmd b/db/MongoDB/install.ps1 similarity index 100% rename from db/MongoDB/install.cmd rename to db/MongoDB/install.ps1 diff --git a/src/admin/Bootstrap.DataAccess.MongoDB/AutoDB.cs b/src/admin/Bootstrap.DataAccess.MongoDB/AutoDB.cs index 690e5184..2ecd122a 100644 --- a/src/admin/Bootstrap.DataAccess.MongoDB/AutoDB.cs +++ b/src/admin/Bootstrap.DataAccess.MongoDB/AutoDB.cs @@ -1,4 +1,7 @@ -namespace Bootstrap.DataAccess.MongoDB +using Bootstrap.Security; +using MongoDB.Driver; + +namespace Bootstrap.DataAccess.MongoDB { /// /// 自动建库实体操作类 @@ -11,7 +14,8 @@ /// public override void EnsureCreated(string folder) { - // UNDONE: 没有环境暂时未写代码 + // 检查数据库是否存在 + if (DbManager.Dicts.CountDocuments(FilterDefinition.Empty) == 0) GenerateDB(folder); } } } diff --git a/src/admin/Bootstrap.DataAccess/AutoDB.cs b/src/admin/Bootstrap.DataAccess/AutoDB.cs index 184de566..50de4654 100644 --- a/src/admin/Bootstrap.DataAccess/AutoDB.cs +++ b/src/admin/Bootstrap.DataAccess/AutoDB.cs @@ -11,31 +11,27 @@ namespace Bootstrap.DataAccess /// public class AutoDB { - private string _folder = ""; - /// /// 数据库检查方法 /// 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("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='Users'") == 0) GenerateSQLiteDB(db); + if (db.ExecuteScalar("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("select COUNT(*) from sys.databases where name = N'BootstrapAdmin'") == 0) GenerateDB(); + if (newDB.ExecuteScalar("select COUNT(*) from sys.databases where name = N'BootstrapAdmin'") == 0) GenerateDB(folder); } break; case "MySqlDatabaseProvider": case "MariaDbDatabaseProvider": - // UNDONE: 本地没有环境此处代码未测试 - if (db.ExecuteScalar("select count(*) from information_schema.tables where table_name ='Users' and Table_Schema = 'BootstrapAdmin'") == 0) GenerateDB(); + if (db.ExecuteScalar("select count(*) from information_schema.tables where table_name ='Users' and Table_Schema = 'BootstrapAdmin'") == 0) GenerateDB(folder); break; } } @@ -54,9 +50,8 @@ namespace Bootstrap.DataAccess 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)) { @@ -72,13 +67,16 @@ namespace Bootstrap.DataAccess } } - private void GenerateDB() + /// + /// 执行建库脚本 + /// + 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(); }