Apply review suggestions

This commit is contained in:
Patrick Barron
2020-05-13 15:03:35 -04:00
parent bac4bf96a0
commit 511d20a100
10 changed files with 88 additions and 314 deletions

View File

@@ -1,6 +1,5 @@
#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.IO;
using Emby.Server.Implementations.Data;
using Jellyfin.Data.Entities;
@@ -12,6 +11,9 @@ using SQLitePCL.pretty;
namespace Jellyfin.Server.Migrations.Routines
{
/// <summary>
/// The migration routine for migrating the activity log database to EF Core.
/// </summary>
public class MigrateActivityLogDb : IMigrationRoutine
{
private const string DbFilename = "activitylog.db";
@@ -20,6 +22,12 @@ namespace Jellyfin.Server.Migrations.Routines
private readonly JellyfinDbProvider _provider;
private readonly IServerApplicationPaths _paths;
/// <summary>
/// Initializes a new instance of the <see cref="MigrateActivityLogDb"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="paths">The server application paths.</param>
/// <param name="provider">The database provider.</param>
public MigrateActivityLogDb(ILogger<MigrateActivityLogDb> logger, IServerApplicationPaths paths, JellyfinDbProvider provider)
{
_logger = logger;
@@ -27,19 +35,35 @@ namespace Jellyfin.Server.Migrations.Routines
_paths = paths;
}
/// <inheritdoc/>
public Guid Id => Guid.Parse("3793eb59-bc8c-456c-8b9f-bd5a62a42978");
/// <inheritdoc/>
public string Name => "MigrateActivityLogDatabase";
/// <inheritdoc/>
public void Perform()
{
var logLevelDictionary = new Dictionary<string, LogLevel>(StringComparer.OrdinalIgnoreCase)
{
{ "None", LogLevel.None },
{ "Trace", LogLevel.Trace },
{ "Debug", LogLevel.Debug },
{ "Information", LogLevel.Information },
{ "Info", LogLevel.Information },
{ "Warn", LogLevel.Warning },
{ "Warning", LogLevel.Warning },
{ "Error", LogLevel.Error },
{ "Critical", LogLevel.Critical }
};
var dataPath = _paths.DataPath;
using (var connection = SQLite3.Open(
Path.Combine(dataPath, DbFilename),
ConnectionFlags.ReadOnly,
null))
{
_logger.LogInformation("Migrating the database may take a while, do not stop Jellyfin.");
_logger.LogWarning("Migrating the activity database may take a while, do not stop Jellyfin.");
using var dbContext = _provider.CreateContext();
var queryResult = connection.Query("SELECT * FROM ActivityLog ORDER BY Id ASC");
@@ -56,9 +80,11 @@ namespace Jellyfin.Server.Migrations.Routines
var newEntry = new ActivityLog(
entry[1].ToString(),
entry[4].ToString(),
entry[6].SQLiteType == SQLiteType.Null ? Guid.Empty : Guid.Parse(entry[6].ToString()),
entry[7].ReadDateTime(),
ParseLogLevel(entry[8].ToString()));
entry[6].SQLiteType == SQLiteType.Null ? Guid.Empty : Guid.Parse(entry[6].ToString()))
{
DateCreated = entry[7].ReadDateTime(),
LogSeverity = logLevelDictionary[entry[8].ToString()]
};
if (entry[2].SQLiteType != SQLiteType.Null)
{
@@ -75,6 +101,8 @@ namespace Jellyfin.Server.Migrations.Routines
newEntry.ItemId = entry[5].ToString();
}
// Since code references the Id of the entries, this needs to be inserted in order.
// In order to do that, this is needed because EF Core doesn't provide a way to guarantee ordering for bulk inserts.
dbContext.ActivityLogs.Add(newEntry);
dbContext.SaveChanges();
}
@@ -89,32 +117,5 @@ namespace Jellyfin.Server.Migrations.Routines
_logger.LogError(e, "Error renaming legacy activity log database to 'activitylog.db.old'");
}
}
private LogLevel ParseLogLevel(string entry)
{
if (string.Equals(entry, "Debug", StringComparison.OrdinalIgnoreCase))
{
return LogLevel.Debug;
}
if (string.Equals(entry, "Information", StringComparison.OrdinalIgnoreCase)
|| string.Equals(entry, "Info", StringComparison.OrdinalIgnoreCase))
{
return LogLevel.Information;
}
if (string.Equals(entry, "Warning", StringComparison.OrdinalIgnoreCase)
|| string.Equals(entry, "Warn", StringComparison.OrdinalIgnoreCase))
{
return LogLevel.Warning;
}
if (string.Equals(entry, "Error", StringComparison.OrdinalIgnoreCase))
{
return LogLevel.Error;
}
return LogLevel.Trace;
}
}
}