Skip to content

Commit

Permalink
feat: add support for mysql remote
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Jul 21, 2024
1 parent 9be3b5c commit 6173279
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Hammer/Configuration/DatabaseConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ namespace Hammer.Configuration;
/// </summary>
internal sealed class DatabaseConfiguration
{
/// <summary>
/// Gets or sets the database name.
/// </summary>
/// <value>The database name.</value>
public string? Database { get; set; }

/// <summary>
/// Gets or sets the remote host.
/// </summary>
/// <value>The remote host.</value>
public string? Host { get; set; }

/// <summary>
/// Gets or sets the login password.
/// </summary>
/// <value>The login password.</value>
public string? Password { get; set; }

/// <summary>
/// Gets or sets the remote port.
/// </summary>
/// <value>The remote port.</value>
public int? Port { get; set; }

/// <summary>
/// Gets or sets the database provider.
/// </summary>
Expand All @@ -16,4 +40,10 @@ internal sealed class DatabaseConfiguration
/// </summary>
/// <value>The table prefix.</value>
public string TablePrefix { get; set; } = "Hammer_";

/// <summary>
/// Gets or sets the login username.
/// </summary>
/// <value>The login username.</value>
public string? Username { get; set; }
}
18 changes: 18 additions & 0 deletions Hammer/Data/HammerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Hammer.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using MySqlConnector;
using MuteConfiguration = Hammer.Data.EntityConfigurations.MuteConfiguration;

namespace Hammer.Data;
Expand Down Expand Up @@ -100,6 +101,23 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
DatabaseConfiguration databaseConfiguration = _configurationService.BotConfiguration.Database;
switch (databaseConfiguration.Provider)
{
case "mysql":
_logger.LogTrace("Using MySQL/MariaDB database provider");
var connectionStringBuilder = new MySqlConnectionStringBuilder
{
Server = databaseConfiguration.Host,
Database = databaseConfiguration.Database,
UserID = databaseConfiguration.Username,
Password = databaseConfiguration.Password
};

var connectionString = connectionStringBuilder.ToString();
ServerVersion version = ServerVersion.AutoDetect(connectionString);

_logger.LogTrace("Server version is {Version}", version);
optionsBuilder.UseMySql(connectionString, version);
break;

case "sqlite":
_logger.LogTrace("Using SQLite database provider");
optionsBuilder.UseSqlite("Data Source='data/hammer.db'");
Expand Down
1 change: 1 addition & 0 deletions Hammer/Hammer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7"/>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0"/>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2"/>
<PackageReference Include="Serilog" Version="4.0.0"/>
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0"/>
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0"/>
Expand Down

0 comments on commit 6173279

Please sign in to comment.