mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-31 00:50:45 +01:00
Expose DatabaseType in IDatabaseContext
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
/// <summary>
|
||||
/// Type of database to user
|
||||
/// </summary>
|
||||
enum DatabaseType
|
||||
public enum DatabaseType
|
||||
{
|
||||
/// <summary>
|
||||
/// Use Microsoft SQL Server
|
||||
|
||||
@@ -16,9 +16,12 @@ using Tgstation.Server.Host.Models;
|
||||
namespace Tgstation.Server.Host.Database
|
||||
{
|
||||
/// <inheritdoc />
|
||||
#pragma warning disable CA1506 // TODO: Decomplexify
|
||||
#pragma warning disable CA1506 // TODO: Decomplexify
|
||||
abstract class DatabaseContext<TParentContext> : DbContext, IDatabaseContext where TParentContext : DbContext
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public DatabaseType DatabaseType => DatabaseConfiguration.DatabaseType;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="User"/>s in the <see cref="DatabaseContext{TParentContext}"/>.
|
||||
/// </summary>
|
||||
@@ -104,11 +107,6 @@ namespace Tgstation.Server.Host.Database
|
||||
/// </summary>
|
||||
protected DatabaseConfiguration DatabaseConfiguration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicationg whether the MY_ class of migrations should be used instead of the MS_ class
|
||||
/// </summary>
|
||||
protected abstract DatabaseType DatabaseType { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
IDatabaseCollection<User> IDatabaseContext.Users => usersCollection;
|
||||
|
||||
@@ -293,6 +291,8 @@ namespace Tgstation.Server.Host.Database
|
||||
/// <inheritdoc />
|
||||
public virtual async Task Initialize(CancellationToken cancellationToken)
|
||||
{
|
||||
ValidateDatabaseType();
|
||||
|
||||
if (DatabaseConfiguration.DropDatabase)
|
||||
{
|
||||
Logger.LogCritical("DropDatabase configuration option set! Dropping any existing database...");
|
||||
@@ -406,5 +406,10 @@ namespace Tgstation.Server.Host.Database
|
||||
Logger.LogCritical("Failed to migrate! Exception: {0}", e);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensure the <see cref="DatabaseType"/> is correct for the <see cref="DatabaseContext{TParentContext}"/>.
|
||||
/// </summary>
|
||||
protected abstract void ValidateDatabaseType();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Tgstation.Server.Host.Configuration;
|
||||
using Tgstation.Server.Host.Models;
|
||||
|
||||
namespace Tgstation.Server.Host.Database
|
||||
@@ -11,6 +12,11 @@ namespace Tgstation.Server.Host.Database
|
||||
/// </summary>
|
||||
public interface IDatabaseContext
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="DatabaseType"/>.
|
||||
/// </summary>
|
||||
DatabaseType DatabaseType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="User"/>s in the <see cref="IDatabaseContext"/>
|
||||
/// </summary>
|
||||
|
||||
@@ -13,9 +13,6 @@ namespace Tgstation.Server.Host.Database
|
||||
/// </summary>
|
||||
sealed class MySqlDatabaseContext : DatabaseContext<MySqlDatabaseContext>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override DatabaseType DatabaseType => DatabaseType.MySql;
|
||||
|
||||
/// <summary>
|
||||
/// Construct a <see cref="MySqlDatabaseContext"/>
|
||||
/// </summary>
|
||||
@@ -47,5 +44,12 @@ namespace Tgstation.Server.Host.Database
|
||||
else
|
||||
options.UseMySql(DatabaseConfiguration.ConnectionString);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void ValidateDatabaseType()
|
||||
{
|
||||
if (DatabaseType != DatabaseType.MariaDB && DatabaseType != DatabaseType.MySql)
|
||||
throw new InvalidOperationException("Invalid DatabaseType for MySqlDatabaseContext!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System;
|
||||
using Tgstation.Server.Host.Configuration;
|
||||
|
||||
namespace Tgstation.Server.Host.Database
|
||||
@@ -10,9 +11,6 @@ namespace Tgstation.Server.Host.Database
|
||||
/// </summary>
|
||||
sealed class SqlServerDatabaseContext : DatabaseContext<SqlServerDatabaseContext>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override DatabaseType DatabaseType => DatabaseType.SqlServer;
|
||||
|
||||
/// <summary>
|
||||
/// Construct a <see cref="SqlServerDatabaseContext"/>
|
||||
/// </summary>
|
||||
@@ -29,5 +27,12 @@ namespace Tgstation.Server.Host.Database
|
||||
base.OnConfiguring(options);
|
||||
options.UseSqlServer(DatabaseConfiguration.ConnectionString);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void ValidateDatabaseType()
|
||||
{
|
||||
if (DatabaseType != DatabaseType.Sqlite)
|
||||
throw new InvalidOperationException("Invalid DatabaseType for SqlServerDatabaseContext!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,6 @@ namespace Tgstation.Server.Host.Database
|
||||
/// </summary>
|
||||
sealed class SqliteDatabaseContext : DatabaseContext<SqliteDatabaseContext>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override DatabaseType DatabaseType => DatabaseType.Sqlite;
|
||||
|
||||
/// <summary>
|
||||
/// Construct a <see cref="MySqlDatabaseContext"/>
|
||||
/// </summary>
|
||||
@@ -35,5 +32,12 @@ namespace Tgstation.Server.Host.Database
|
||||
base.OnConfiguring(options);
|
||||
options.UseSqlite(DatabaseConfiguration.ConnectionString);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void ValidateDatabaseType()
|
||||
{
|
||||
if (DatabaseType != DatabaseType.Sqlite)
|
||||
throw new InvalidOperationException("Invalid DatabaseType for SqliteDatabaseContext!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user