From 4c3d76dac68da27969c31afcc321e29b850f58ee Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Mon, 6 May 2019 08:59:51 -0400 Subject: [PATCH] Make LocalDB database installation more robust --- Oqtane.Client/Oqtane.Client.csproj | 2 +- Oqtane.Server/Filters/UpgradeFilter.cs | 19 +++++++++++++++---- Oqtane.Server/Oqtane.Server.csproj | 4 ++-- Oqtane.Server/Oqtane.Server.csproj.user | 21 --------------------- Oqtane.Server/Program.cs | 25 +++++++++++++++++++++++++ Oqtane.Server/Scripts/Initialize.sql | 4 +++- Oqtane.Shared/Oqtane.Shared.csproj | 2 +- 7 files changed, 47 insertions(+), 30 deletions(-) delete mode 100644 Oqtane.Server/Oqtane.Server.csproj.user diff --git a/Oqtane.Client/Oqtane.Client.csproj b/Oqtane.Client/Oqtane.Client.csproj index 090c5b24..aaa24925 100644 --- a/Oqtane.Client/Oqtane.Client.csproj +++ b/Oqtane.Client/Oqtane.Client.csproj @@ -14,7 +14,7 @@ Oqtane Shaun Walker .NET Foundation - .NET Core Web Application Framework for Blazor + Modular Application Framework for Blazor .NET Foundation https://www.oqtane.org https://github.com/oqtane diff --git a/Oqtane.Server/Filters/UpgradeFilter.cs b/Oqtane.Server/Filters/UpgradeFilter.cs index 7bf22e7e..6e76c3f0 100644 --- a/Oqtane.Server/Filters/UpgradeFilter.cs +++ b/Oqtane.Server/Filters/UpgradeFilter.cs @@ -6,6 +6,7 @@ using System.Reflection; using DbUp; using System.Data.SqlClient; using System.Threading; +using System.IO; namespace Oqtane.Filters { @@ -67,13 +68,13 @@ namespace Oqtane.Filters { connection.Open(); SqlCommand command; - if (connectionString.ToLower().Contains("attachdbfilename=")) + if (connectionString.ToLower().Contains("attachdbfilename=")) // LocalDB { - command = new SqlCommand("CREATE DATABASE " + databaseName + " ON ( NAME = '" + databaseName + "', FILENAME = '" + datadirectory + "\\" + databaseName + ".mdf')", connection); + command = new SqlCommand("CREATE DATABASE [" + databaseName + "] ON ( NAME = '" + databaseName + "', FILENAME = '" + datadirectory + "\\" + databaseName + ".mdf')", connection); } else { - command = new SqlCommand("CREATE DATABASE " + databaseName, connection); + command = new SqlCommand("CREATE DATABASE [" + databaseName + "]", connection); } command.ExecuteNonQuery(); } @@ -82,13 +83,23 @@ namespace Oqtane.Filters { throw ex; } + // sleep to allow SQL server to attach new database Thread.Sleep(5000); } + // get initialization script and update connectionstring in Tenants seed data + string initializationScript = ""; + using (StreamReader reader = new StreamReader(Directory.GetCurrentDirectory() + "\\Scripts\\Initialize.sql")) + { + initializationScript = reader.ReadToEnd(); + } + initializationScript = initializationScript.Replace("{ConnectionString}", connectionString); + // handle upgrade scripts var dbUpgradeConfig = DeployChanges.To.SqlDatabase(connectionString) - .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly()); + .WithScript(new DbUp.Engine.SqlScript("Initialize.sql", initializationScript)) + .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly()); // upgrade scripts should be added to /Scripts folder as Embedded Resources var dbUpgrade = dbUpgradeConfig.Build(); if (dbUpgrade.IsUpgradeRequired()) { diff --git a/Oqtane.Server/Oqtane.Server.csproj b/Oqtane.Server/Oqtane.Server.csproj index 30760574..113a325c 100644 --- a/Oqtane.Server/Oqtane.Server.csproj +++ b/Oqtane.Server/Oqtane.Server.csproj @@ -13,7 +13,7 @@ Oqtane Shaun Walker .NET Foundation - .NET Core Web Application Framework for Blazor + Modular Application Framework for Blazor .NET Foundation https://www.oqtane.org https://github.com/oqtane @@ -26,7 +26,7 @@ - + diff --git a/Oqtane.Server/Oqtane.Server.csproj.user b/Oqtane.Server/Oqtane.Server.csproj.user deleted file mode 100644 index 374e80df..00000000 --- a/Oqtane.Server/Oqtane.Server.csproj.user +++ /dev/null @@ -1,21 +0,0 @@ - - - - ProjectDebugger - - - ProjectDebugger - - - ProjectDebugger - - - ProjectDebugger - - - ProjectDebugger - - - IIS Express - - \ No newline at end of file diff --git a/Oqtane.Server/Program.cs b/Oqtane.Server/Program.cs index eb793414..8e6d1632 100644 --- a/Oqtane.Server/Program.cs +++ b/Oqtane.Server/Program.cs @@ -3,6 +3,9 @@ using Microsoft.Extensions.Hosting; using Microsoft.AspNetCore.Blazor.Hosting; using Microsoft.AspNetCore; using Microsoft.Extensions.Configuration; +using System.IO; +using System.Text; +using System; namespace Oqtane.Server { @@ -11,6 +14,7 @@ namespace Oqtane.Server #if DEBUG || RELEASE public static void Main(string[] args) { + PrepareConfiguration(); CreateHostBuilder(args).Build().Run(); } @@ -25,6 +29,7 @@ namespace Oqtane.Server #if WASM public static void Main(string[] args) { + PrepareConfiguration(); BuildWebHost(args).Run(); } @@ -36,5 +41,25 @@ namespace Oqtane.Server .UseStartup() .Build(); #endif + + private static void PrepareConfiguration() + { + string config = ""; + using (StreamReader reader = new StreamReader(Directory.GetCurrentDirectory() + "\\appsettings.json")) + { + config = reader.ReadToEnd(); + } + // if using LocalDB create a unique database name + if (config.Contains("AttachDbFilename=|DataDirectory|\\\\Oqtane.mdf")) + { + string timestamp = DateTime.Now.ToString("yyyyMMddHHmm"); + config = config.Replace("Initial Catalog=Oqtane", "Initial Catalog=Oqtane-" + timestamp) + .Replace("AttachDbFilename=|DataDirectory|\\\\Oqtane.mdf", "AttachDbFilename=|DataDirectory|\\\\Oqtane-" + timestamp + ".mdf"); + using (StreamWriter writer = new StreamWriter(Directory.GetCurrentDirectory() + "\\appsettings.json")) + { + writer.WriteLine(config); + } + } + } } } diff --git a/Oqtane.Server/Scripts/Initialize.sql b/Oqtane.Server/Scripts/Initialize.sql index a3ad3437..7eb7e618 100644 --- a/Oqtane.Server/Scripts/Initialize.sql +++ b/Oqtane.Server/Scripts/Initialize.sql @@ -152,7 +152,7 @@ Create seed data SET IDENTITY_INSERT [dbo].[Tenant] ON GO INSERT [dbo].[Tenant] ([TenantId], [Alias], [DBConnectionString], [DBSchema], [SiteId]) -VALUES (1, N'localhost:44357', N'Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Oqtane.mdf;Initial Catalog=Oqtane;Integrated Security=SSPI;', N'', 1) +VALUES (1, N'localhost:44357', N'{ConnectionString}', N'', 1) GO SET IDENTITY_INSERT [dbo].[Tenant] OFF GO @@ -336,3 +336,5 @@ VALUES (4, N'member', N'Member', N'Members;', 0) GO SET IDENTITY_INSERT [dbo].[User] OFF GO + + diff --git a/Oqtane.Shared/Oqtane.Shared.csproj b/Oqtane.Shared/Oqtane.Shared.csproj index c8a5a270..95593fdd 100644 --- a/Oqtane.Shared/Oqtane.Shared.csproj +++ b/Oqtane.Shared/Oqtane.Shared.csproj @@ -8,7 +8,7 @@ Oqtane Shaun Walker .NET Foundation - .NET Core Web Application Framework for Blazor + Modular Application Framework for Blazor .NET Foundation https://www.oqtane.org https://github.com/oqtane