MigrateActivityLogDb.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.IO;
  4. using Emby.Server.Implementations.Data;
  5. using Jellyfin.Data.Entities;
  6. using Jellyfin.Server.Implementations;
  7. using Microsoft.EntityFrameworkCore;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.Logging;
  10. using SQLitePCL.pretty;
  11. namespace Jellyfin.Server.Migrations.Routines
  12. {
  13. public class MigrateActivityLogDb : IMigrationRoutine
  14. {
  15. private const string DbFilename = "activitylog.db";
  16. public Guid Id => Guid.Parse("3793eb59-bc8c-456c-8b9f-bd5a62a42978");
  17. public string Name => "MigrateActivityLogDatabase";
  18. public void Perform(CoreAppHost host, ILogger logger)
  19. {
  20. var dataPath = host.ServerConfigurationManager.ApplicationPaths.DataPath;
  21. using (var connection = SQLite3.Open(
  22. Path.Combine(dataPath, DbFilename),
  23. ConnectionFlags.ReadOnly,
  24. null))
  25. {
  26. logger.LogInformation("Migrating the database may take a while, do not stop Jellyfin.");
  27. using var dbContext = host.ServiceProvider.GetService<JellyfinDb>();
  28. var queryResult = connection.Query("SELECT * FROM ActivityLog ORDER BY Id ASC");
  29. // Make sure that the database is empty in case of failed migration due to power outages, etc.
  30. dbContext.ActivityLogs.RemoveRange(dbContext.ActivityLogs);
  31. dbContext.SaveChanges();
  32. // Reset the autoincrement counter
  33. dbContext.Database.ExecuteSqlRaw("UPDATE sqlite_sequence SET seq = 0 WHERE name = 'ActivityLog';");
  34. dbContext.SaveChanges();
  35. foreach (var entry in queryResult)
  36. {
  37. var newEntry = new ActivityLog(
  38. entry[1].ToString(),
  39. entry[4].ToString(),
  40. entry[6].SQLiteType == SQLiteType.Null ? Guid.Empty : Guid.Parse(entry[6].ToString()),
  41. entry[7].ReadDateTime(),
  42. ParseLogLevel(entry[8].ToString()));
  43. if (entry[2].SQLiteType != SQLiteType.Null)
  44. {
  45. newEntry.Overview = entry[2].ToString();
  46. }
  47. if (entry[3].SQLiteType != SQLiteType.Null)
  48. {
  49. newEntry.ShortOverview = entry[3].ToString();
  50. }
  51. if (entry[5].SQLiteType != SQLiteType.Null)
  52. {
  53. newEntry.ItemId = entry[5].ToString();
  54. }
  55. dbContext.ActivityLogs.Add(newEntry);
  56. dbContext.SaveChanges();
  57. }
  58. }
  59. try
  60. {
  61. File.Move(Path.Combine(dataPath, DbFilename), Path.Combine(dataPath, DbFilename + ".old"));
  62. }
  63. catch (IOException e)
  64. {
  65. logger.LogError(e, "Error renaming legacy activity log database to 'activitylog.db.old'");
  66. }
  67. }
  68. private LogLevel ParseLogLevel(string entry)
  69. {
  70. if (string.Equals(entry, "Debug", StringComparison.OrdinalIgnoreCase))
  71. {
  72. return LogLevel.Debug;
  73. }
  74. if (string.Equals(entry, "Information", StringComparison.OrdinalIgnoreCase)
  75. || string.Equals(entry, "Info", StringComparison.OrdinalIgnoreCase))
  76. {
  77. return LogLevel.Information;
  78. }
  79. if (string.Equals(entry, "Warning", StringComparison.OrdinalIgnoreCase)
  80. || string.Equals(entry, "Warn", StringComparison.OrdinalIgnoreCase))
  81. {
  82. return LogLevel.Warning;
  83. }
  84. if (string.Equals(entry, "Error", StringComparison.OrdinalIgnoreCase))
  85. {
  86. return LogLevel.Error;
  87. }
  88. return LogLevel.Trace;
  89. }
  90. }
  91. }