MigrateAuthenticationDb.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.IO;
  3. using Emby.Server.Implementations.Data;
  4. using Jellyfin.Data.Entities.Security;
  5. using Jellyfin.Server.Implementations;
  6. using MediaBrowser.Controller;
  7. using Microsoft.Extensions.Logging;
  8. using SQLitePCL.pretty;
  9. namespace Jellyfin.Server.Migrations.Routines
  10. {
  11. /// <summary>
  12. /// A migration that moves data from the authentication database into the new schema.
  13. /// </summary>
  14. public class MigrateAuthenticationDb : IMigrationRoutine
  15. {
  16. private const string DbFilename = "authentication.db";
  17. private readonly ILogger<MigrateAuthenticationDb> _logger;
  18. private readonly JellyfinDbProvider _dbProvider;
  19. private readonly IServerApplicationPaths _appPaths;
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="MigrateAuthenticationDb"/> class.
  22. /// </summary>
  23. /// <param name="logger">The logger.</param>
  24. /// <param name="dbProvider">The database provider.</param>
  25. /// <param name="appPaths">The server application paths.</param>
  26. public MigrateAuthenticationDb(ILogger<MigrateAuthenticationDb> logger, JellyfinDbProvider dbProvider, IServerApplicationPaths appPaths)
  27. {
  28. _logger = logger;
  29. _dbProvider = dbProvider;
  30. _appPaths = appPaths;
  31. }
  32. /// <inheritdoc />
  33. public Guid Id => Guid.Parse("5BD72F41-E6F3-4F60-90AA-09869ABE0E22");
  34. /// <inheritdoc />
  35. public string Name => "MigrateAuthenticationDatabase";
  36. /// <inheritdoc />
  37. public bool PerformOnNewInstall => false;
  38. /// <inheritdoc />
  39. public void Perform()
  40. {
  41. var dataPath = _appPaths.DataPath;
  42. using (var connection = SQLite3.Open(
  43. Path.Combine(dataPath, DbFilename),
  44. ConnectionFlags.ReadOnly,
  45. null))
  46. {
  47. using var dbContext = _dbProvider.CreateContext();
  48. var queryResult = connection.Query("SELECT * FROM Tokens");
  49. foreach (var row in queryResult)
  50. {
  51. if (row[6].IsDbNull())
  52. {
  53. dbContext.ApiKeys.Add(new ApiKey(row[3].ToString())
  54. {
  55. AccessToken = row[1].ToGuid(),
  56. DateCreated = row[9].ToDateTime(),
  57. DateLastActivity = row[10].ToDateTime()
  58. });
  59. }
  60. else
  61. {
  62. dbContext.Devices.Add(new Device(
  63. row[6].ToGuid(),
  64. row[3].ToString(),
  65. row[4].ToString(),
  66. row[5].ToString(),
  67. row[2].ToString())
  68. {
  69. AccessToken = row[1].ToString(),
  70. IsActive = row[8].ToBool(),
  71. DateCreated = row[9].ToDateTime(),
  72. DateLastActivity = row[10].ToDateTime()
  73. });
  74. }
  75. }
  76. dbContext.SaveChanges();
  77. }
  78. try
  79. {
  80. File.Move(Path.Combine(dataPath, DbFilename), Path.Combine(dataPath, DbFilename + ".old"));
  81. var journalPath = Path.Combine(dataPath, DbFilename + "-journal");
  82. if (File.Exists(journalPath))
  83. {
  84. File.Move(journalPath, Path.Combine(dataPath, DbFilename + ".old-journal"));
  85. }
  86. }
  87. catch (IOException e)
  88. {
  89. _logger.LogError(e, "Error renaming legacy activity log database to 'authentication.db.old'");
  90. }
  91. }
  92. }
  93. }