Restructured folders and project names, ci should be fixed

This commit is contained in:
Kwoth
2021-06-17 23:40:48 +02:00
parent 7aca29ae8a
commit 91ecf9ca41
788 changed files with 204 additions and 146 deletions

View File

@@ -0,0 +1,70 @@
using System;
using System.Data.Common;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Core.Services;
using Serilog;
namespace NadekoBot.Modules.Xp.Services
{
public sealed class XpConfigMigrator : IConfigMigrator
{
private readonly DbService _db;
private readonly XpConfigService _gss;
public XpConfigMigrator(DbService dbService, XpConfigService gss)
{
_db = dbService;
_gss = gss;
}
public void EnsureMigrated()
{
using var uow = _db.GetDbContext();
using var conn = uow._context.Database.GetDbConnection();
Migrate(conn);
}
private void Migrate(DbConnection conn)
{
using (var checkTableCommand = conn.CreateCommand())
{
// make sure table still exists
checkTableCommand.CommandText =
"SELECT name FROM sqlite_master WHERE type='table' AND name='BotConfig';";
var checkReader = checkTableCommand.ExecuteReader();
if (!checkReader.HasRows)
return;
}
using (var checkMigratedCommand = conn.CreateCommand())
{
checkMigratedCommand.CommandText =
"UPDATE BotConfig SET HasMigratedXpSettings = 1 WHERE HasMigratedXpSettings = 0;";
var changedRows = checkMigratedCommand.ExecuteNonQuery();
if (changedRows == 0)
return;
}
Log.Information("Migrating Xp settings...");
using var com = conn.CreateCommand();
com.CommandText = $@"SELECT XpPerMessage, XpMinutesTimeout, VoiceXpPerMinute, MaxXpMinutes
FROM BotConfig";
using var reader = com.ExecuteReader();
if (!reader.Read())
return;
_gss.ModifyConfig(ModifyAction(reader));
}
private static Action<XpConfig> ModifyAction(DbDataReader reader)
=> config =>
{
config.XpPerMessage = (int) (long) reader["XpPerMessage"];
config.MessageXpCooldown = (int) (long) reader["XpMinutesTimeout"];
config.VoiceMaxMinutes = (int) (long) reader["MaxXpMinutes"];
config.VoiceXpPerMinute = (double) reader["VoiceXpPerMinute"];
};
}
}