mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-30 00:22:40 +01:00
Abstract away DbConnection creation for setup wizard
This commit is contained in:
@@ -101,6 +101,7 @@ namespace Tgstation.Server.Host.Core
|
||||
//setup stuff for setup wizard
|
||||
services.AddSingleton<IIOManager, DefaultIOManager>();
|
||||
services.AddSingleton<IConsole, IO.Console>();
|
||||
services.AddSingleton<IDBConnectionFactory, DBConnectionFactory>();
|
||||
services.AddSingleton<ISetupWizard, SetupWizard>();
|
||||
|
||||
//needed here for JWT configuration
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.Globalization;
|
||||
using Tgstation.Server.Host.Configuration;
|
||||
|
||||
namespace Tgstation.Server.Host.Core
|
||||
{
|
||||
/// <inheritdoc />
|
||||
sealed class DBConnectionFactory : IDBConnectionFactory
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public DbConnection CreateConnection(string connectionString, DatabaseType databaseType)
|
||||
{
|
||||
if (connectionString == null)
|
||||
throw new ArgumentNullException(nameof(connectionString));
|
||||
|
||||
switch (databaseType)
|
||||
{
|
||||
case DatabaseType.MariaDB:
|
||||
case DatabaseType.MySql:
|
||||
return new MySqlConnection
|
||||
{
|
||||
ConnectionString = connectionString
|
||||
};
|
||||
case DatabaseType.SqlServer:
|
||||
return new SqlConnection
|
||||
{
|
||||
ConnectionString = connectionString
|
||||
};
|
||||
default:
|
||||
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Invalid database type ({0})!", databaseType));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Data.Common;
|
||||
using Tgstation.Server.Host.Configuration;
|
||||
|
||||
namespace Tgstation.Server.Host.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// For creating <see cref="DbConnection"/>
|
||||
/// </summary>
|
||||
interface IDBConnectionFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a <see cref="DbConnection"/>
|
||||
/// </summary>
|
||||
/// <param name="connectionString">The <see cref="DbConnection.ConnectionString"/></param>
|
||||
/// <param name="databaseType">The <see cref="DatabaseType"/> to create</param>
|
||||
/// <returns>A new <see cref="DbConnection"/></returns>
|
||||
DbConnection CreateConnection(string connectionString, DatabaseType databaseType);
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,11 @@ namespace Tgstation.Server.Host.Core
|
||||
/// </summary>
|
||||
readonly IApplication application;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IDBConnectionFactory"/> for the <see cref="SetupWizard"/>
|
||||
/// </summary>
|
||||
readonly IDBConnectionFactory dbConnectionFactory;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ILogger"/> for the <see cref="SetupWizard"/>
|
||||
/// </summary>
|
||||
@@ -56,14 +61,16 @@ namespace Tgstation.Server.Host.Core
|
||||
/// <param name="ioManager">The value of <see cref="ioManager"/></param>
|
||||
/// <param name="hostingEnvironment">The value of <see cref="hostingEnvironment"/></param>
|
||||
/// <param name="application">The value of <see cref="application"/></param>
|
||||
/// <param name="dbConnectionFactory">The value of <see cref="dbConnectionFactory"/></param>
|
||||
/// <param name="logger">The value of <see cref="logger"/></param>
|
||||
/// <param name="generalConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="generalConfiguration"/></param>
|
||||
public SetupWizard(IIOManager ioManager, IConsole console, IHostingEnvironment hostingEnvironment, IApplication application, ILogger<SetupWizard> logger, IOptions<GeneralConfiguration> generalConfigurationOptions)
|
||||
public SetupWizard(IIOManager ioManager, IConsole console, IHostingEnvironment hostingEnvironment, IApplication application, IDBConnectionFactory dbConnectionFactory, ILogger<SetupWizard> logger, IOptions<GeneralConfiguration> generalConfigurationOptions)
|
||||
{
|
||||
this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager));
|
||||
this.console = console ?? throw new ArgumentNullException(nameof(console));
|
||||
this.hostingEnvironment = hostingEnvironment ?? throw new ArgumentNullException(nameof(hostingEnvironment));
|
||||
this.application = application ?? throw new ArgumentNullException(nameof(application));
|
||||
this.dbConnectionFactory = dbConnectionFactory ?? throw new ArgumentNullException(nameof(dbConnectionFactory));
|
||||
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
generalConfiguration = generalConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(generalConfigurationOptions));
|
||||
}
|
||||
@@ -189,7 +196,13 @@ namespace Tgstation.Server.Host.Core
|
||||
await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
DbConnection testConnection;
|
||||
void CreateTestConnection(string connectionString)
|
||||
{
|
||||
testConnection = dbConnectionFactory.CreateConnection(connectionString, databaseConfiguration.DatabaseType);
|
||||
}
|
||||
|
||||
if (databaseConfiguration.DatabaseType == DatabaseType.SqlServer)
|
||||
{
|
||||
var csb = new SqlConnectionStringBuilder
|
||||
@@ -204,11 +217,8 @@ namespace Tgstation.Server.Host.Core
|
||||
csb.UserID = username;
|
||||
csb.Password = password;
|
||||
}
|
||||
testConnection = new SqlConnection
|
||||
{
|
||||
ConnectionString = csb.ConnectionString
|
||||
};
|
||||
|
||||
CreateTestConnection(csb.ConnectionString);
|
||||
csb.InitialCatalog = databaseName;
|
||||
databaseConfiguration.ConnectionString = csb.ConnectionString;
|
||||
}
|
||||
@@ -220,10 +230,8 @@ namespace Tgstation.Server.Host.Core
|
||||
UserID = username,
|
||||
Password = password
|
||||
};
|
||||
testConnection = new MySqlConnection
|
||||
{
|
||||
ConnectionString = csb.ConnectionString
|
||||
};
|
||||
|
||||
CreateTestConnection(csb.ConnectionString);
|
||||
csb.Database = databaseName;
|
||||
databaseConfiguration.ConnectionString = csb.ConnectionString;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user