Make LocalDB database installation more robust

This commit is contained in:
Shaun Walker 2019-05-06 08:59:51 -04:00
parent d71de1c21f
commit 4c3d76dac6
7 changed files with 47 additions and 30 deletions

View File

@ -14,7 +14,7 @@
<Product>Oqtane</Product>
<Authors>Shaun Walker</Authors>
<Company>.NET Foundation</Company>
<Description>.NET Core Web Application Framework for Blazor</Description>
<Description>Modular Application Framework for Blazor</Description>
<Copyright>.NET Foundation</Copyright>
<PackageProjectUrl>https://www.oqtane.org</PackageProjectUrl>
<RepositoryUrl>https://github.com/oqtane</RepositoryUrl>

View File

@ -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())
{

View File

@ -13,7 +13,7 @@
<Product>Oqtane</Product>
<Authors>Shaun Walker</Authors>
<Company>.NET Foundation</Company>
<Description>.NET Core Web Application Framework for Blazor</Description>
<Description>Modular Application Framework for Blazor</Description>
<Copyright>.NET Foundation</Copyright>
<PackageProjectUrl>https://www.oqtane.org</PackageProjectUrl>
<RepositoryUrl>https://github.com/oqtane</RepositoryUrl>
@ -26,7 +26,7 @@
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Scripts\Initialize.sql" />
<Content Include="Scripts\Initialize.sql" />
</ItemGroup>
<ItemGroup>

View File

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='SSB|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='CSB|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='CLIENTSIDE_BLAZOR|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='SERVERSIDE_BLAZOR|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>IIS Express</ActiveDebugProfile>
</PropertyGroup>
</Project>

View File

@ -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<Startup>()
.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);
}
}
}
}
}

View File

@ -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

View File

@ -8,7 +8,7 @@
<Product>Oqtane</Product>
<Authors>Shaun Walker</Authors>
<Company>.NET Foundation</Company>
<Description>.NET Core Web Application Framework for Blazor</Description>
<Description>Modular Application Framework for Blazor</Description>
<Copyright>.NET Foundation</Copyright>
<PackageProjectUrl>https://www.oqtane.org</PackageProjectUrl>
<RepositoryUrl>https://github.com/oqtane</RepositoryUrl>