jellyfin/Jellyfin.Server/Migrations/Routines/MigrateActivityLogDb.cs

154 lines
6 KiB
C#
Raw Normal View History

2020-05-03 00:32:22 +02:00
using System;
2020-05-13 21:03:35 +02:00
using System.Collections.Generic;
2020-05-03 00:32:22 +02:00
using System.IO;
using Emby.Server.Implementations.Data;
using Jellyfin.Data.Entities;
using Jellyfin.Server.Implementations;
2020-05-12 18:50:17 +02:00
using MediaBrowser.Controller;
2023-08-21 15:31:02 +02:00
using Microsoft.Data.Sqlite;
2020-05-03 00:32:22 +02:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Server.Migrations.Routines
{
2020-05-13 21:03:35 +02:00
/// <summary>
/// The migration routine for migrating the activity log database to EF Core.
/// </summary>
2020-05-03 00:32:22 +02:00
public class MigrateActivityLogDb : IMigrationRoutine
{
private const string DbFilename = "activitylog.db";
2020-05-12 18:50:17 +02:00
private readonly ILogger<MigrateActivityLogDb> _logger;
2023-01-16 18:14:44 +01:00
private readonly IDbContextFactory<JellyfinDbContext> _provider;
2020-05-12 18:50:17 +02:00
private readonly IServerApplicationPaths _paths;
2020-05-13 21:03:35 +02:00
/// <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>
2023-01-16 18:14:44 +01:00
public MigrateActivityLogDb(ILogger<MigrateActivityLogDb> logger, IServerApplicationPaths paths, IDbContextFactory<JellyfinDbContext> provider)
2020-05-12 18:50:17 +02:00
{
_logger = logger;
_provider = provider;
_paths = paths;
}
2020-05-13 21:03:35 +02:00
/// <inheritdoc/>
2020-05-03 00:32:22 +02:00
public Guid Id => Guid.Parse("3793eb59-bc8c-456c-8b9f-bd5a62a42978");
2020-05-13 21:03:35 +02:00
/// <inheritdoc/>
2020-05-03 00:32:22 +02:00
public string Name => "MigrateActivityLogDatabase";
/// <inheritdoc/>
public bool PerformOnNewInstall => false;
2020-05-13 21:03:35 +02:00
/// <inheritdoc/>
2020-05-12 18:50:17 +02:00
public void Perform()
2020-05-03 00:32:22 +02:00
{
2020-05-13 21:03:35 +02:00
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 }
};
2020-05-12 18:50:17 +02:00
var dataPath = _paths.DataPath;
2023-08-21 15:31:02 +02:00
using (var connection = new SqliteConnection($"Filename={Path.Combine(dataPath, DbFilename)}"))
2020-05-03 00:32:22 +02:00
{
connection.Open();
2023-09-16 07:27:22 +02:00
using var userDbConnection = new SqliteConnection($"Filename={Path.Combine(dataPath, "users.db")}");
userDbConnection.Open();
2020-05-13 21:03:35 +02:00
_logger.LogWarning("Migrating the activity database may take a while, do not stop Jellyfin.");
using var dbContext = _provider.CreateDbContext();
2020-05-03 00:32:22 +02:00
// Make sure that the database is empty in case of failed migration due to power outages, etc.
dbContext.ActivityLogs.RemoveRange(dbContext.ActivityLogs);
dbContext.SaveChanges();
// Reset the autoincrement counter
dbContext.Database.ExecuteSqlRaw("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'ActivityLog';");
dbContext.SaveChanges();
var newEntries = new List<ActivityLog>();
2023-08-21 15:31:02 +02:00
var queryResult = connection.Query("SELECT * FROM ActivityLog ORDER BY Id");
foreach (var entry in queryResult)
2020-05-03 00:32:22 +02:00
{
2023-08-21 15:31:02 +02:00
if (!logLevelDictionary.TryGetValue(entry.GetString(8), out var severity))
2020-05-13 23:55:31 +02:00
{
severity = LogLevel.Trace;
}
2020-06-04 20:54:43 +02:00
var guid = Guid.Empty;
2023-08-21 15:31:02 +02:00
if (!entry.IsDBNull(6) && !entry.TryGetGuid(6, out guid))
{
2023-08-21 15:31:02 +02:00
var id = entry.GetString(6);
// This is not a valid Guid, see if it is an internal ID from an old Emby schema
2023-08-21 15:31:02 +02:00
_logger.LogWarning("Invalid Guid in UserId column: {Guid}", id);
2020-06-04 20:54:43 +02:00
using var statement = userDbConnection.PrepareStatement("SELECT guid FROM LocalUsersv2 WHERE Id=@Id");
2023-08-21 15:31:02 +02:00
statement.TryBind("@Id", id);
2023-08-21 15:31:02 +02:00
using var reader = statement.ExecuteReader();
if (reader.HasRows && reader.Read() && reader.TryGetGuid(0, out guid))
{
2023-08-21 15:31:02 +02:00
// Successfully parsed a Guid from the user table.
break;
}
}
2023-08-21 15:31:02 +02:00
var newEntry = new ActivityLog(entry.GetString(1), entry.GetString(4), guid)
2020-05-13 21:03:35 +02:00
{
2023-08-21 15:31:02 +02:00
DateCreated = entry.GetDateTime(7),
2020-05-13 23:55:31 +02:00
LogSeverity = severity
2020-05-13 21:03:35 +02:00
};
2020-05-03 00:32:22 +02:00
2023-08-21 15:31:02 +02:00
if (entry.TryGetString(2, out var result))
2020-05-03 00:32:22 +02:00
{
2023-08-21 15:31:02 +02:00
newEntry.Overview = result;
2020-05-03 00:32:22 +02:00
}
2023-08-21 15:31:02 +02:00
if (entry.TryGetString(3, out result))
2020-05-03 00:32:22 +02:00
{
2023-08-21 15:31:02 +02:00
newEntry.ShortOverview = result;
2020-05-03 00:32:22 +02:00
}
2023-08-21 15:31:02 +02:00
if (entry.TryGetString(5, out result))
2020-05-03 00:32:22 +02:00
{
2023-08-21 15:31:02 +02:00
newEntry.ItemId = result;
2020-05-03 00:32:22 +02:00
}
newEntries.Add(newEntry);
}
2020-05-15 18:51:18 +02:00
2020-05-15 20:08:46 +02:00
dbContext.ActivityLogs.AddRange(newEntries);
2020-05-15 18:51:18 +02:00
dbContext.SaveChanges();
2020-05-03 00:32:22 +02:00
}
try
{
File.Move(Path.Combine(dataPath, DbFilename), Path.Combine(dataPath, DbFilename + ".old"));
2020-05-15 20:08:46 +02:00
var journalPath = Path.Combine(dataPath, DbFilename + "-journal");
if (File.Exists(journalPath))
{
File.Move(journalPath, Path.Combine(dataPath, DbFilename + ".old-journal"));
}
2020-05-03 00:32:22 +02:00
}
catch (IOException e)
{
2020-05-12 18:50:17 +02:00
_logger.LogError(e, "Error renaming legacy activity log database to 'activitylog.db.old'");
2020-05-03 00:32:22 +02:00
}
}
}
}