mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-27 23:17:20 +01:00
Switch to Pomelo MySQL EFCore provider
This commit is contained in:
@@ -43,12 +43,16 @@ Create an `appsettings.Production.json` file next to `appsettings.json`. This wi
|
||||
|
||||
- `Logging:LogLevel:Default`: Can be one of `Trace`, `Debug`, `Information`, `Warning`, `Error`, or `Critical`. Restricts what is put into the log files. Currently `Debug` is reccommended for help with error reporting.
|
||||
|
||||
- `Database:DatabaseType`: Can be one of `SqlServer` or `MySql`. Note that, at the time of this writing, there is a [blocking bug with the MySQL DBAL provider](https://bugs.mysql.com/bug.php?id=89855) which prevents its usage
|
||||
- `Database:DatabaseType`: Can be one of `SqlServer`, `MariaDB`, or `MySql`
|
||||
|
||||
- `Database:MySqlServerVersion`: The version of MySql/MariaDB the database resides on, can be left as null for attempted auto detection. Used by the MySQL/MariaDB provider for selection of [certain features](https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/blob/2.1.1/src/EFCore.MySql/Storage/Internal/ServerVersion.cs) ignore at your own risk. A string in the form `<major>.<minor>.<patch>`
|
||||
|
||||
- `Database:ConnectionString`: Connection string for your database. Click [here](https://www.developerfusion.com/tools/sql-connection-string/) for an SQL Server generator or see [here](https://www.connectionstrings.com/mysql/) for a MySQL guide.
|
||||
|
||||
### Database Configuration
|
||||
|
||||
If using MySQL, our provider library [recommends you set 'utf8mb4' as your default charset](https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql#1-recommended-server-charset) disregard at your own risk.
|
||||
|
||||
The user created for the application will need the privilege to create databases on the first run. Once the initial set of migrations is run, the create right may be revoked. The user should maintain DDL rights though for applying future migrations
|
||||
|
||||
### Starting
|
||||
|
||||
@@ -34,5 +34,10 @@
|
||||
/// If the database should be deleted on application startup. Should not be used in production!
|
||||
/// </summary>
|
||||
public bool DropDatabase { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="string"/> form of the <see cref="System.Version"/> of a target MySQL/MariaDB server
|
||||
/// </summary>
|
||||
public string MySqlServerVersion { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,12 @@
|
||||
/// </summary>
|
||||
SqlServer,
|
||||
/// <summary>
|
||||
/// Use MySQL/MariaDB
|
||||
/// Use MySQL
|
||||
/// </summary>
|
||||
MySql
|
||||
MySql,
|
||||
/// <summary>
|
||||
/// Use MariaDB
|
||||
/// </summary>
|
||||
MariaDB
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,6 +153,7 @@ namespace Tgstation.Server.Host.Core
|
||||
switch (dbType)
|
||||
{
|
||||
case DatabaseType.MySql:
|
||||
case DatabaseType.MariaDB:
|
||||
services.AddDbContext<MySqlDatabaseContext>(ConfigureDatabase);
|
||||
services.AddScoped<IDatabaseContext>(x => x.GetRequiredService<MySqlDatabaseContext>());
|
||||
break;
|
||||
|
||||
@@ -66,15 +66,10 @@ namespace Tgstation.Server.Host.Models
|
||||
/// </summary>
|
||||
protected ILogger Logger { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The connection string for the <see cref="DatabaseContext{TParentContext}"/>
|
||||
/// </summary>
|
||||
protected string ConnectionString => databaseConfiguration.ConnectionString;
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext{TParentContext}"/>
|
||||
/// </summary>
|
||||
readonly DatabaseConfiguration databaseConfiguration;
|
||||
protected DatabaseConfiguration DatabaseConfiguration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="IDatabaseSeeder"/> for the <see cref="DatabaseContext{TParentContext}"/>
|
||||
@@ -85,12 +80,12 @@ namespace Tgstation.Server.Host.Models
|
||||
/// Construct a <see cref="DatabaseContext{TParentContext}"/>
|
||||
/// </summary>
|
||||
/// <param name="dbContextOptions">The <see cref="DbContextOptions{TParentContext}"/> for the <see cref="DatabaseContext{TParentContext}"/></param>
|
||||
/// <param name="databaseConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="databaseConfiguration"/></param>
|
||||
/// <param name="databaseConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="DatabaseConfiguration"/></param>
|
||||
/// <param name="databaseSeeder">The value of <see cref="databaseSeeder"/></param>
|
||||
/// <param name="logger">The value of <see cref="Logger"/></param>
|
||||
public DatabaseContext(DbContextOptions<TParentContext> dbContextOptions, IOptions<DatabaseConfiguration> databaseConfigurationOptions, IDatabaseSeeder databaseSeeder, ILogger logger) : base(dbContextOptions)
|
||||
{
|
||||
databaseConfiguration = databaseConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(databaseConfigurationOptions));
|
||||
DatabaseConfiguration = databaseConfigurationOptions?.Value ?? throw new ArgumentNullException(nameof(databaseConfigurationOptions));
|
||||
this.databaseSeeder = databaseSeeder ?? throw new ArgumentNullException(nameof(databaseSeeder));
|
||||
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
@@ -140,14 +135,14 @@ namespace Tgstation.Server.Host.Models
|
||||
{
|
||||
Logger.LogInformation("Migrating database...");
|
||||
|
||||
if (databaseConfiguration.DropDatabase)
|
||||
if (DatabaseConfiguration.DropDatabase)
|
||||
{
|
||||
Logger.LogCritical("DropDatabase configuration option set! Dropping any existing database...");
|
||||
await Database.EnsureDeletedAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
var wasEmpty = false;
|
||||
if (databaseConfiguration.NoMigrations)
|
||||
if (DatabaseConfiguration.NoMigrations)
|
||||
{
|
||||
Logger.LogWarning("Using all or nothing migration strategy!");
|
||||
await Database.EnsureCreatedAsync(cancellationToken).ConfigureAwait(false);
|
||||
@@ -169,7 +164,7 @@ namespace Tgstation.Server.Host.Models
|
||||
else
|
||||
{
|
||||
Logger.LogDebug("No migrations applied!");
|
||||
if (databaseConfiguration.ResetAdminPassword)
|
||||
if (DatabaseConfiguration.ResetAdminPassword)
|
||||
{
|
||||
Logger.LogWarning("Enabling and resetting admin password due to configuration!");
|
||||
await databaseSeeder.ResetAdminPassword(this, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
|
||||
using System;
|
||||
using Tgstation.Server.Host.Configuration;
|
||||
|
||||
namespace Tgstation.Server.Host.Models
|
||||
@@ -24,7 +26,10 @@ namespace Tgstation.Server.Host.Models
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
||||
{
|
||||
base.OnConfiguring(options);
|
||||
options.UseMySQL(ConnectionString);
|
||||
if (DatabaseConfiguration.MySqlServerVersion != null)
|
||||
options.UseMySql(DatabaseConfiguration.ConnectionString, mySqlOptions => mySqlOptions.ServerVersion(Version.Parse(DatabaseConfiguration.MySqlServerVersion), DatabaseConfiguration.DatabaseType == DatabaseType.MariaDB ? ServerType.MariaDb : ServerType.MySql));
|
||||
else
|
||||
options.UseMySql(DatabaseConfiguration.ConnectionString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace Tgstation.Server.Host.Models
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
||||
{
|
||||
base.OnConfiguring(options);
|
||||
options.UseSqlServer(ConnectionString);
|
||||
options.UseSqlServer(DatabaseConfiguration.ConnectionString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,8 +39,8 @@
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.2" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.2" />
|
||||
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" />
|
||||
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.12" />
|
||||
<PackageReference Include="Octokit" Version="0.31.0" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.1" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0-dev-00024" />
|
||||
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="4.5.0" />
|
||||
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="4.5.0" />
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
"DropDatabase": false,
|
||||
"DatabaseType": "SqlServer",
|
||||
"ResetAdminPassword": false,
|
||||
"MySqlServerVersion": null,
|
||||
"ConnectionString": "Data Source=(local);Initial Catalog=TGS;Integrated Security=True"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user