-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7360a11
commit 2e00364
Showing
6 changed files
with
236 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using DSharpPlus; | ||
using DSharpPlus.Entities; | ||
using DSharpPlus.SlashCommands; | ||
using DSharpPlus.SlashCommands.Attributes; | ||
using Hammer.Services; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Hammer.Commands; | ||
|
||
internal sealed class MigrateDatabaseCommand : ApplicationCommandModule | ||
{ | ||
private readonly ILogger<MigrateDatabaseCommand> _logger; | ||
private readonly DatabaseService _databaseService; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MigrateDatabaseCommand" /> class. | ||
/// </summary> | ||
/// <param name="logger">The logger.</param> | ||
/// <param name="databaseService">The database service.</param> | ||
public MigrateDatabaseCommand(ILogger<MigrateDatabaseCommand> logger, DatabaseService databaseService) | ||
{ | ||
_logger = logger; | ||
_databaseService = databaseService; | ||
} | ||
|
||
[SlashCommand("migratedb", "Migrates the SQLite database to MySQL/MariaDB.", false)] | ||
[SlashRequireGuild] | ||
public async Task MigrateDatabaseAsync(InteractionContext context) | ||
{ | ||
var embed = new DiscordEmbedBuilder(); | ||
embed.WithColor(DiscordColor.Orange); | ||
embed.WithTitle("⏳ Migration in progress"); | ||
embed.WithDescription("Please wait while the database is migrated..."); | ||
|
||
await context.CreateResponseAsync(embed); | ||
try | ||
{ | ||
int rows = await _databaseService.MigrateAsync(); | ||
|
||
var builder = new DiscordWebhookBuilder(); | ||
embed.WithColor(DiscordColor.Green); | ||
embed.WithTitle("✅ Migration complete"); | ||
embed.WithDescription($"All data has been successfully migrated. {rows:N0} rows affected"); | ||
builder.AddEmbed(embed); | ||
await context.EditResponseAsync(builder); | ||
} | ||
catch (Exception exception) | ||
{ | ||
_logger.LogError(exception, "Failed to migrate"); | ||
var builder = new DiscordWebhookBuilder(); | ||
embed.WithColor(DiscordColor.Red); | ||
embed.WithTitle($"⚠️ Migration failed: {exception.GetType().Name}"); | ||
embed.WithDescription($"{exception.GetType().Name} was thrown during migration: {exception.Message}\n\n" + | ||
"View the log for more details"); | ||
builder.AddEmbed(embed); | ||
await context.EditResponseAsync(builder); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using Hammer.Data.EntityConfigurations; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Logging; | ||
using MuteConfiguration = Hammer.Data.EntityConfigurations.MuteConfiguration; | ||
|
||
namespace Hammer.Data; | ||
|
||
/// <summary> | ||
/// Represents a session with the <c>hammer.db</c> database. | ||
/// </summary> | ||
internal sealed class MigrationContext : DbContext | ||
{ | ||
private readonly ILogger<MigrationContext> _logger; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="MigrationContext" /> class. | ||
/// </summary> | ||
/// <param name="logger">The logger.</param> | ||
public MigrationContext(ILogger<MigrationContext> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the set of alt accounts. | ||
/// </summary> | ||
/// <value>The set of alt accounts.</value> | ||
public DbSet<AltAccount> AltAccounts { get; private set; } = null!; | ||
|
||
/// <summary> | ||
/// Gets the set of users who are blocked from making reports. | ||
/// </summary> | ||
/// <value>The set of blocked reporters.</value> | ||
public DbSet<BlockedReporter> BlockedReporters { get; private set; } = null!; | ||
|
||
/// <summary> | ||
/// Gets the set of staff-deleted messages. | ||
/// </summary> | ||
/// <value>The set of staff-deleted messages.</value> | ||
public DbSet<DeletedMessage> DeletedMessages { get; private set; } = null!; | ||
|
||
/// <summary> | ||
/// Gets the set of infractions. | ||
/// </summary> | ||
/// <value>The set of infractions.</value> | ||
public DbSet<Infraction> Infractions { get; private set; } = null!; | ||
|
||
/// <summary> | ||
/// Gets the set of member notes. | ||
/// </summary> | ||
/// <value>The set of member notes.</value> | ||
public DbSet<MemberNote> MemberNotes { get; private set; } = null!; | ||
|
||
/// <summary> | ||
/// Gets the set of mutes. | ||
/// </summary> | ||
/// <value>The set of mutes.</value> | ||
public DbSet<Mute> Mutes { get; private set; } = null!; | ||
|
||
/// <summary> | ||
/// Gets the set of reported messages. | ||
/// </summary> | ||
/// <value>The set of reported messages.</value> | ||
public DbSet<ReportedMessage> ReportedMessages { get; private set; } = null!; | ||
|
||
/// <summary> | ||
/// Gets the set of rules. | ||
/// </summary> | ||
/// <value>The set of rules.</value> | ||
public DbSet<Rule> Rules { get; private set; } = null!; | ||
|
||
/// <summary> | ||
/// Gets the set of staff messages. | ||
/// </summary> | ||
/// <value>The set of staff messages.</value> | ||
public DbSet<StaffMessage> StaffMessages { get; private set; } = null!; | ||
|
||
/// <summary> | ||
/// Gets the set of temporary bans. | ||
/// </summary> | ||
/// <value>The set of temporary bans.</value> | ||
public DbSet<TemporaryBan> TemporaryBans { get; private set; } = null!; | ||
|
||
/// <summary> | ||
/// Gets the set of tracked messages. | ||
/// </summary> | ||
/// <value>The set of tracked messages.</value> | ||
public DbSet<TrackedMessage> TrackedMessages { get; private set; } = null!; | ||
|
||
/// <inheritdoc /> | ||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
base.OnConfiguring(optionsBuilder); | ||
|
||
_logger.LogDebug("Using SQLite database provider"); | ||
optionsBuilder.UseSqlite("Data Source='data/hammer.db'"); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
base.OnModelCreating(modelBuilder); | ||
|
||
modelBuilder.ApplyConfiguration(new AltAccountConfiguration(false)); | ||
modelBuilder.ApplyConfiguration(new BlockedReporterConfiguration(false)); | ||
modelBuilder.ApplyConfiguration(new DeletedMessageConfiguration(false)); | ||
modelBuilder.ApplyConfiguration(new InfractionConfiguration(false)); | ||
modelBuilder.ApplyConfiguration(new MemberNoteConfiguration(false)); | ||
modelBuilder.ApplyConfiguration(new MuteConfiguration(false)); | ||
modelBuilder.ApplyConfiguration(new StaffMessageConfiguration()); | ||
modelBuilder.ApplyConfiguration(new ReportedMessageConfiguration()); | ||
modelBuilder.ApplyConfiguration(new TemporaryBanConfiguration(false)); | ||
modelBuilder.ApplyConfiguration(new TrackedMessageConfiguration(false)); | ||
modelBuilder.ApplyConfiguration(new RuleConfiguration()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters