MigrateActivityLogDb.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Emby.Server.Implementations.Data;
  5. using Jellyfin.Data.Entities;
  6. using Jellyfin.Server.Implementations;
  7. using MediaBrowser.Controller;
  8. using Microsoft.EntityFrameworkCore;
  9. using Microsoft.Extensions.Logging;
  10. using SQLitePCL.pretty;
  11. namespace Jellyfin.Server.Migrations.Routines
  12. {
  13. /// <summary>
  14. /// The migration routine for migrating the activity log database to EF Core.
  15. /// </summary>
  16. public class MigrateActivityLogDb : IMigrationRoutine
  17. {
  18. private const string DbFilename = "activitylog.db";
  19. private readonly ILogger<MigrateActivityLogDb> _logger;
  20. private readonly JellyfinDbProvider _provider;
  21. private readonly IServerApplicationPaths _paths;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="MigrateActivityLogDb"/> class.
  24. /// </summary>
  25. /// <param name="logger">The logger.</param>
  26. /// <param name="paths">The server application paths.</param>
  27. /// <param name="provider">The database provider.</param>
  28. public MigrateActivityLogDb(ILogger<MigrateActivityLogDb> logger, IServerApplicationPaths paths, JellyfinDbProvider provider)
  29. {
  30. _logger = logger;
  31. _provider = provider;
  32. _paths = paths;
  33. }
  34. /// <inheritdoc/>
  35. public Guid Id => Guid.Parse("3793eb59-bc8c-456c-8b9f-bd5a62a42978");
  36. /// <inheritdoc/>
  37. public string Name => "MigrateActivityLogDatabase";
  38. /// <inheritdoc/>
  39. public void Perform()
  40. {
  41. var logLevelDictionary = new Dictionary<string, LogLevel>(StringComparer.OrdinalIgnoreCase)
  42. {
  43. { "None", LogLevel.None },
  44. { "Trace", LogLevel.Trace },
  45. { "Debug", LogLevel.Debug },
  46. { "Information", LogLevel.Information },
  47. { "Info", LogLevel.Information },
  48. { "Warn", LogLevel.Warning },
  49. { "Warning", LogLevel.Warning },
  50. { "Error", LogLevel.Error },
  51. { "Critical", LogLevel.Critical }
  52. };
  53. var dataPath = _paths.DataPath;
  54. using (var connection = SQLite3.Open(
  55. Path.Combine(dataPath, DbFilename),
  56. ConnectionFlags.ReadOnly,
  57. null))
  58. {
  59. _logger.LogWarning("Migrating the activity database may take a while, do not stop Jellyfin.");
  60. using var dbContext = _provider.CreateContext();
  61. var queryResult = connection.Query("SELECT * FROM ActivityLog ORDER BY Id ASC");
  62. // Make sure that the database is empty in case of failed migration due to power outages, etc.
  63. dbContext.ActivityLogs.RemoveRange(dbContext.ActivityLogs);
  64. dbContext.SaveChanges();
  65. // Reset the autoincrement counter
  66. dbContext.Database.ExecuteSqlRaw("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'ActivityLog';");
  67. dbContext.SaveChanges();
  68. foreach (var entry in queryResult)
  69. {
  70. if (!logLevelDictionary.TryGetValue(entry[8].ToString(), out var severity))
  71. {
  72. severity = LogLevel.Trace;
  73. }
  74. var newEntry = new ActivityLog(
  75. entry[1].ToString(),
  76. entry[4].ToString(),
  77. entry[6].SQLiteType == SQLiteType.Null ? Guid.Empty : Guid.Parse(entry[6].ToString()))
  78. {
  79. DateCreated = entry[7].ReadDateTime(),
  80. LogSeverity = severity
  81. };
  82. if (entry[2].SQLiteType != SQLiteType.Null)
  83. {
  84. newEntry.Overview = entry[2].ToString();
  85. }
  86. if (entry[3].SQLiteType != SQLiteType.Null)
  87. {
  88. newEntry.ShortOverview = entry[3].ToString();
  89. }
  90. if (entry[5].SQLiteType != SQLiteType.Null)
  91. {
  92. newEntry.ItemId = entry[5].ToString();
  93. }
  94. // Since code references the Id of the entries, this needs to be inserted in order.
  95. // In order to do that, we insert one by one because EF Core doesn't provide a way to guarantee ordering for bulk inserts.
  96. dbContext.ActivityLogs.Add(newEntry);
  97. dbContext.SaveChanges();
  98. }
  99. }
  100. try
  101. {
  102. File.Move(Path.Combine(dataPath, DbFilename), Path.Combine(dataPath, DbFilename + ".old"));
  103. }
  104. catch (IOException e)
  105. {
  106. _logger.LogError(e, "Error renaming legacy activity log database to 'activitylog.db.old'");
  107. }
  108. }
  109. }
  110. }