2020-05-02 15:32:22 -07:00
|
|
|
using System;
|
2020-05-13 12:03:35 -07:00
|
|
|
using System.Collections.Generic;
|
2020-05-02 15:32:22 -07:00
|
|
|
using System.IO;
|
2020-05-15 11:08:46 -07:00
|
|
|
using System.Linq;
|
2020-05-02 15:32:22 -07:00
|
|
|
using Emby.Server.Implementations.Data;
|
|
|
|
using Jellyfin.Data.Entities;
|
|
|
|
using Jellyfin.Server.Implementations;
|
2020-05-12 09:50:17 -07:00
|
|
|
using MediaBrowser.Controller;
|
2020-05-02 15:32:22 -07:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using SQLitePCL.pretty;
|
|
|
|
|
|
|
|
namespace Jellyfin.Server.Migrations.Routines
|
|
|
|
{
|
2020-05-13 12:03:35 -07:00
|
|
|
/// <summary>
|
|
|
|
/// The migration routine for migrating the activity log database to EF Core.
|
|
|
|
/// </summary>
|
2020-05-02 15:32:22 -07:00
|
|
|
public class MigrateActivityLogDb : IMigrationRoutine
|
|
|
|
{
|
|
|
|
private const string DbFilename = "activitylog.db";
|
|
|
|
|
2020-05-12 09:50:17 -07:00
|
|
|
private readonly ILogger<MigrateActivityLogDb> _logger;
|
|
|
|
private readonly JellyfinDbProvider _provider;
|
|
|
|
private readonly IServerApplicationPaths _paths;
|
|
|
|
|
2020-05-13 12:03:35 -07: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>
|
2020-05-12 09:50:17 -07:00
|
|
|
public MigrateActivityLogDb(ILogger<MigrateActivityLogDb> logger, IServerApplicationPaths paths, JellyfinDbProvider provider)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_provider = provider;
|
|
|
|
_paths = paths;
|
|
|
|
}
|
|
|
|
|
2020-05-13 12:03:35 -07:00
|
|
|
/// <inheritdoc/>
|
2020-05-02 15:32:22 -07:00
|
|
|
public Guid Id => Guid.Parse("3793eb59-bc8c-456c-8b9f-bd5a62a42978");
|
|
|
|
|
2020-05-13 12:03:35 -07:00
|
|
|
/// <inheritdoc/>
|
2020-05-02 15:32:22 -07:00
|
|
|
public string Name => "MigrateActivityLogDatabase";
|
|
|
|
|
2020-05-13 12:03:35 -07:00
|
|
|
/// <inheritdoc/>
|
2020-05-12 09:50:17 -07:00
|
|
|
public void Perform()
|
2020-05-02 15:32:22 -07:00
|
|
|
{
|
2020-05-13 12:03:35 -07: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 09:50:17 -07:00
|
|
|
var dataPath = _paths.DataPath;
|
2020-05-02 15:32:22 -07:00
|
|
|
using (var connection = SQLite3.Open(
|
|
|
|
Path.Combine(dataPath, DbFilename),
|
|
|
|
ConnectionFlags.ReadOnly,
|
|
|
|
null))
|
|
|
|
{
|
2020-06-01 17:12:34 -07:00
|
|
|
using var userDbConnection = SQLite3.Open(Path.Combine(dataPath, "users.db"), ConnectionFlags.ReadOnly, null);
|
2020-05-13 12:03:35 -07:00
|
|
|
_logger.LogWarning("Migrating the activity database may take a while, do not stop Jellyfin.");
|
2020-05-12 09:50:17 -07:00
|
|
|
using var dbContext = _provider.CreateContext();
|
2020-05-02 15:32:22 -07:00
|
|
|
|
|
|
|
var queryResult = connection.Query("SELECT * FROM ActivityLog ORDER BY Id ASC");
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2020-06-01 17:12:34 -07:00
|
|
|
var newEntries = new List<ActivityLog>();
|
|
|
|
|
|
|
|
foreach (var entry in queryResult)
|
2020-05-02 15:32:22 -07:00
|
|
|
{
|
2020-05-13 14:55:31 -07:00
|
|
|
if (!logLevelDictionary.TryGetValue(entry[8].ToString(), out var severity))
|
|
|
|
{
|
|
|
|
severity = LogLevel.Trace;
|
|
|
|
}
|
|
|
|
|
2020-06-01 17:12:34 -07:00
|
|
|
Guid guid;
|
|
|
|
if (entry[6].SQLiteType == SQLiteType.Null)
|
|
|
|
{
|
|
|
|
guid = Guid.Empty;
|
|
|
|
}
|
|
|
|
else if (!Guid.TryParse(entry[6].ToString(), out guid))
|
|
|
|
{
|
|
|
|
// This is not a valid Guid, see if it is an internal ID from an old Emby schema
|
|
|
|
var userEntry = userDbConnection
|
|
|
|
.Query($"SELECT guid FROM LocalUsersv2 WHERE Id = {entry[6].ToString()}")
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
if (userEntry.Count == 0 || !Guid.TryParse(userEntry[0].ToString(), out guid))
|
|
|
|
{
|
|
|
|
// Give up, just use Guid.Empty
|
|
|
|
guid = Guid.Empty;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 15:32:22 -07:00
|
|
|
var newEntry = new ActivityLog(
|
|
|
|
entry[1].ToString(),
|
|
|
|
entry[4].ToString(),
|
2020-06-01 17:12:34 -07:00
|
|
|
guid)
|
2020-05-13 12:03:35 -07:00
|
|
|
{
|
|
|
|
DateCreated = entry[7].ReadDateTime(),
|
2020-05-13 14:55:31 -07:00
|
|
|
LogSeverity = severity
|
2020-05-13 12:03:35 -07:00
|
|
|
};
|
2020-05-02 15:32:22 -07:00
|
|
|
|
|
|
|
if (entry[2].SQLiteType != SQLiteType.Null)
|
|
|
|
{
|
2020-05-15 11:33:36 -07:00
|
|
|
newEntry.Overview = entry[2].ToString();
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entry[3].SQLiteType != SQLiteType.Null)
|
|
|
|
{
|
2020-05-15 11:33:36 -07:00
|
|
|
newEntry.ShortOverview = entry[3].ToString();
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (entry[5].SQLiteType != SQLiteType.Null)
|
|
|
|
{
|
2020-05-15 11:33:36 -07:00
|
|
|
newEntry.ItemId = entry[5].ToString();
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
|
2020-06-01 17:12:34 -07:00
|
|
|
newEntries.Add(newEntry);
|
|
|
|
}
|
2020-05-15 09:51:18 -07:00
|
|
|
|
2020-05-15 11:08:46 -07:00
|
|
|
dbContext.ActivityLogs.AddRange(newEntries);
|
2020-05-15 09:51:18 -07:00
|
|
|
dbContext.SaveChanges();
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
File.Move(Path.Combine(dataPath, DbFilename), Path.Combine(dataPath, DbFilename + ".old"));
|
2020-05-15 11:08:46 -07:00
|
|
|
|
|
|
|
var journalPath = Path.Combine(dataPath, DbFilename + "-journal");
|
|
|
|
if (File.Exists(journalPath))
|
|
|
|
{
|
|
|
|
File.Move(journalPath, Path.Combine(dataPath, DbFilename + ".old-journal"));
|
|
|
|
}
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
2020-05-12 09:50:17 -07:00
|
|
|
_logger.LogError(e, "Error renaming legacy activity log database to 'activitylog.db.old'");
|
2020-05-02 15:32:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|