jellyfin-server/Jellyfin.Server/Migrations/Routines/MigrateAuthenticationDb.cs

150 lines
5.5 KiB
C#
Raw Normal View History

2021-06-18 21:07:30 +00:00
using System;
2021-06-24 13:38:37 +00:00
using System.Collections.Generic;
2021-06-18 21:07:30 +00:00
using System.IO;
using Emby.Server.Implementations.Data;
using Jellyfin.Data.Entities.Security;
using Jellyfin.Server.Implementations;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Library;
2023-08-21 13:31:02 +00:00
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
2021-06-18 21:07:30 +00:00
using Microsoft.Extensions.Logging;
namespace Jellyfin.Server.Migrations.Routines
{
/// <summary>
/// A migration that moves data from the authentication database into the new schema.
/// </summary>
public class MigrateAuthenticationDb : IMigrationRoutine
{
private const string DbFilename = "authentication.db";
private readonly ILogger<MigrateAuthenticationDb> _logger;
2023-01-16 17:14:44 +00:00
private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
2021-06-18 21:07:30 +00:00
private readonly IServerApplicationPaths _appPaths;
private readonly IUserManager _userManager;
2021-06-18 21:07:30 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="MigrateAuthenticationDb"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="dbProvider">The database provider.</param>
/// <param name="appPaths">The server application paths.</param>
/// <param name="userManager">The user manager.</param>
public MigrateAuthenticationDb(
ILogger<MigrateAuthenticationDb> logger,
2023-01-16 17:14:44 +00:00
IDbContextFactory<JellyfinDbContext> dbProvider,
IServerApplicationPaths appPaths,
IUserManager userManager)
2021-06-18 21:07:30 +00:00
{
_logger = logger;
_dbProvider = dbProvider;
_appPaths = appPaths;
_userManager = userManager;
2021-06-18 21:07:30 +00:00
}
/// <inheritdoc />
public Guid Id => Guid.Parse("5BD72F41-E6F3-4F60-90AA-09869ABE0E22");
/// <inheritdoc />
public string Name => "MigrateAuthenticationDatabase";
/// <inheritdoc />
public bool PerformOnNewInstall => false;
/// <inheritdoc />
public void Perform()
{
var dataPath = _appPaths.DataPath;
2023-08-21 13:31:02 +00:00
using (var connection = new SqliteConnection($"Filename={Path.Combine(dataPath, DbFilename)}"))
2021-06-18 21:07:30 +00:00
{
connection.Open();
using var dbContext = _dbProvider.CreateDbContext();
2021-06-18 21:07:30 +00:00
2021-06-24 13:38:37 +00:00
var authenticatedDevices = connection.Query("SELECT * FROM Tokens");
2021-06-18 21:07:30 +00:00
2021-06-24 13:38:37 +00:00
foreach (var row in authenticatedDevices)
2021-06-18 21:07:30 +00:00
{
2023-08-21 13:31:02 +00:00
var dateCreatedStr = row.GetString(9);
2022-03-01 03:44:17 +00:00
_ = DateTime.TryParse(dateCreatedStr, out var dateCreated);
2023-08-21 13:31:02 +00:00
var dateLastActivityStr = row.GetString(10);
2022-03-01 03:44:17 +00:00
_ = DateTime.TryParse(dateLastActivityStr, out var dateLastActivity);
2023-08-21 13:31:02 +00:00
if (row.IsDBNull(6))
2021-06-18 21:07:30 +00:00
{
2023-08-21 13:31:02 +00:00
dbContext.ApiKeys.Add(new ApiKey(row.GetString(3))
2021-06-18 21:07:30 +00:00
{
2023-08-21 13:31:02 +00:00
AccessToken = row.GetString(1),
2022-03-01 03:44:17 +00:00
DateCreated = dateCreated,
DateLastActivity = dateLastActivity
2021-06-18 21:07:30 +00:00
});
}
else
{
2023-08-21 13:31:02 +00:00
var userId = row.GetGuid(6);
var user = _userManager.GetUserById(userId);
if (user is null)
{
// User doesn't exist, don't bring over the device.
continue;
}
2021-06-18 21:07:30 +00:00
dbContext.Devices.Add(new Device(
2023-08-21 13:31:02 +00:00
userId,
row.GetString(3),
row.GetString(4),
row.GetString(5),
row.GetString(2))
2021-06-18 21:07:30 +00:00
{
2023-08-21 13:31:02 +00:00
AccessToken = row.GetString(1),
IsActive = row.GetBoolean(8),
2022-03-01 03:44:17 +00:00
DateCreated = dateCreated,
DateLastActivity = dateLastActivity
2021-06-18 21:07:30 +00:00
});
}
}
2021-06-24 13:38:37 +00:00
var deviceOptions = connection.Query("SELECT * FROM Devices");
var deviceIds = new HashSet<string>();
foreach (var row in deviceOptions)
{
2023-08-21 13:31:02 +00:00
if (row.IsDBNull(2))
2021-06-24 13:38:37 +00:00
{
continue;
}
2023-08-21 13:31:02 +00:00
var deviceId = row.GetString(2);
2021-06-24 13:38:37 +00:00
if (deviceIds.Contains(deviceId))
{
continue;
}
deviceIds.Add(deviceId);
dbContext.DeviceOptions.Add(new DeviceOptions(deviceId)
{
2023-08-21 13:31:02 +00:00
CustomName = row.IsDBNull(1) ? null : row.GetString(1)
2021-06-24 13:38:37 +00:00
});
}
2021-06-18 21:07:30 +00:00
dbContext.SaveChanges();
}
try
{
File.Move(Path.Combine(dataPath, DbFilename), Path.Combine(dataPath, DbFilename + ".old"));
var journalPath = Path.Combine(dataPath, DbFilename + "-journal");
if (File.Exists(journalPath))
{
File.Move(journalPath, Path.Combine(dataPath, DbFilename + ".old-journal"));
}
}
catch (IOException e)
{
_logger.LogError(e, "Error renaming legacy activity log database to 'authentication.db.old'");
}
}
}
}