mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-27 04:24:52 +01:00
Migrate activity db to EF Core
This commit is contained in:
103
Jellyfin.Server.Implementations/Activity/ActivityManager.cs
Normal file
103
Jellyfin.Server.Implementations/Activity/ActivityManager.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Data.Entities;
|
||||
using MediaBrowser.Model.Activity;
|
||||
using MediaBrowser.Model.Events;
|
||||
using MediaBrowser.Model.Querying;
|
||||
|
||||
namespace Jellyfin.Server.Implementations.Activity
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages the storage and retrieval of <see cref="ActivityLog"/> instances.
|
||||
/// </summary>
|
||||
public class ActivityManager : IActivityManager
|
||||
{
|
||||
private JellyfinDbProvider _provider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ActivityManager"/> class.
|
||||
/// </summary>
|
||||
/// <param name="provider">The Jellyfin database provider.</param>
|
||||
public ActivityManager(JellyfinDbProvider provider)
|
||||
{
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public event EventHandler<GenericEventArgs<ActivityLogEntry>> EntryCreated;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Create(ActivityLog entry)
|
||||
{
|
||||
using var dbContext = _provider.CreateContext();
|
||||
dbContext.ActivityLogs.Add(entry);
|
||||
dbContext.SaveChanges();
|
||||
|
||||
EntryCreated?.Invoke(this, new GenericEventArgs<ActivityLogEntry>(ConvertToOldModel(entry)));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task CreateAsync(ActivityLog entry)
|
||||
{
|
||||
using var dbContext = _provider.CreateContext();
|
||||
await dbContext.ActivityLogs.AddAsync(entry);
|
||||
await dbContext.SaveChangesAsync().ConfigureAwait(false);
|
||||
|
||||
EntryCreated?.Invoke(this, new GenericEventArgs<ActivityLogEntry>(ConvertToOldModel(entry)));
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public QueryResult<ActivityLogEntry> GetPagedResult(
|
||||
Func<IQueryable<ActivityLog>, IEnumerable<ActivityLog>> func,
|
||||
int? startIndex,
|
||||
int? limit)
|
||||
{
|
||||
using var dbContext = _provider.CreateContext();
|
||||
|
||||
var result = func.Invoke(dbContext.ActivityLogs).AsQueryable();
|
||||
|
||||
if (startIndex.HasValue)
|
||||
{
|
||||
result = result.Where(entry => entry.Id >= startIndex.Value);
|
||||
}
|
||||
|
||||
if (limit.HasValue)
|
||||
{
|
||||
result = result.OrderByDescending(entry => entry.DateCreated).Take(limit.Value);
|
||||
}
|
||||
|
||||
// This converts the objects from the new database model to the old for compatibility with the existing API.
|
||||
var list = result.Select(entry => ConvertToOldModel(entry)).ToList();
|
||||
|
||||
return new QueryResult<ActivityLogEntry>()
|
||||
{
|
||||
Items = list,
|
||||
TotalRecordCount = list.Count
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public QueryResult<ActivityLogEntry> GetPagedResult(int? startIndex, int? limit)
|
||||
{
|
||||
return GetPagedResult(logs => logs, startIndex, limit);
|
||||
}
|
||||
|
||||
private static ActivityLogEntry ConvertToOldModel(ActivityLog entry)
|
||||
{
|
||||
return new ActivityLogEntry
|
||||
{
|
||||
Id = entry.Id,
|
||||
Name = entry.Name,
|
||||
Overview = entry.Overview,
|
||||
ShortOverview = entry.ShortOverview,
|
||||
Type = entry.Type,
|
||||
ItemId = entry.ItemId,
|
||||
UserId = entry.UserId,
|
||||
Date = entry.DateCreated,
|
||||
Severity = entry.LogSeverity
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<CodeAnalysisRuleSet>../jellyfin.ruleset</CodeAnalysisRuleSet>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- Code analysers-->
|
||||
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" PrivateAssets="All" />
|
||||
<PackageReference Include="SerilogAnalyzer" Version="0.15.0" PrivateAssets="All" />
|
||||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" />
|
||||
<PackageReference Include="SmartAnalyzers.MultithreadingAnalyzer" Version="1.1.31" PrivateAssets="All" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\SharedVersion.cs" />
|
||||
<Compile Remove="Migrations\20200430214405_InitialSchema.cs" />
|
||||
<Compile Remove="Migrations\20200430214405_InitialSchema.Designer.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Jellyfin.Data\Jellyfin.Data.csproj" />
|
||||
<ProjectReference Include="..\MediaBrowser.Controller\MediaBrowser.Controller.csproj" />
|
||||
<ProjectReference Include="..\MediaBrowser.Model\MediaBrowser.Model.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
119
Jellyfin.Server.Implementations/JellyfinDb.cs
Normal file
119
Jellyfin.Server.Implementations/JellyfinDb.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
#pragma warning disable CS1591
|
||||
#pragma warning disable SA1201 // Constuctors should not follow properties
|
||||
#pragma warning disable SA1516 // Elements should be followed by a blank line
|
||||
#pragma warning disable SA1623 // Property's documentation should begin with gets or sets
|
||||
#pragma warning disable SA1629 // Documentation should end with a period
|
||||
#pragma warning disable SA1648 // Inheritdoc should be used with inheriting class
|
||||
|
||||
using System.Linq;
|
||||
using Jellyfin.Data;
|
||||
using Jellyfin.Data.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Jellyfin.Server.Implementations
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
public partial class JellyfinDb : DbContext
|
||||
{
|
||||
public virtual DbSet<ActivityLog> ActivityLogs { get; set; }
|
||||
public virtual DbSet<Artwork> Artwork { get; set; }
|
||||
public virtual DbSet<Book> Books { get; set; }
|
||||
public virtual DbSet<BookMetadata> BookMetadata { get; set; }
|
||||
public virtual DbSet<Chapter> Chapters { get; set; }
|
||||
public virtual DbSet<Collection> Collections { get; set; }
|
||||
public virtual DbSet<CollectionItem> CollectionItems { get; set; }
|
||||
public virtual DbSet<Company> Companies { get; set; }
|
||||
public virtual DbSet<CompanyMetadata> CompanyMetadata { get; set; }
|
||||
public virtual DbSet<CustomItem> CustomItems { get; set; }
|
||||
public virtual DbSet<CustomItemMetadata> CustomItemMetadata { get; set; }
|
||||
public virtual DbSet<Episode> Episodes { get; set; }
|
||||
public virtual DbSet<EpisodeMetadata> EpisodeMetadata { get; set; }
|
||||
public virtual DbSet<Genre> Genres { get; set; }
|
||||
public virtual DbSet<Group> Groups { get; set; }
|
||||
public virtual DbSet<Library> Libraries { get; set; }
|
||||
public virtual DbSet<LibraryItem> LibraryItems { get; set; }
|
||||
public virtual DbSet<LibraryRoot> LibraryRoot { get; set; }
|
||||
public virtual DbSet<MediaFile> MediaFiles { get; set; }
|
||||
public virtual DbSet<MediaFileStream> MediaFileStream { get; set; }
|
||||
public virtual DbSet<Metadata> Metadata { get; set; }
|
||||
public virtual DbSet<MetadataProvider> MetadataProviders { get; set; }
|
||||
public virtual DbSet<MetadataProviderId> MetadataProviderIds { get; set; }
|
||||
public virtual DbSet<Movie> Movies { get; set; }
|
||||
public virtual DbSet<MovieMetadata> MovieMetadata { get; set; }
|
||||
public virtual DbSet<MusicAlbum> MusicAlbums { get; set; }
|
||||
public virtual DbSet<MusicAlbumMetadata> MusicAlbumMetadata { get; set; }
|
||||
public virtual DbSet<Permission> Permissions { get; set; }
|
||||
public virtual DbSet<Person> People { get; set; }
|
||||
public virtual DbSet<PersonRole> PersonRoles { get; set; }
|
||||
public virtual DbSet<Photo> Photo { get; set; }
|
||||
public virtual DbSet<PhotoMetadata> PhotoMetadata { get; set; }
|
||||
public virtual DbSet<Preference> Preferences { get; set; }
|
||||
public virtual DbSet<ProviderMapping> ProviderMappings { get; set; }
|
||||
public virtual DbSet<Rating> Ratings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Repository for global::Jellyfin.Data.Entities.RatingSource - This is the entity to
|
||||
/// store review ratings, not age ratings
|
||||
/// </summary>
|
||||
public virtual DbSet<RatingSource> RatingSources { get; set; }
|
||||
public virtual DbSet<Release> Releases { get; set; }
|
||||
public virtual DbSet<Season> Seasons { get; set; }
|
||||
public virtual DbSet<SeasonMetadata> SeasonMetadata { get; set; }
|
||||
public virtual DbSet<Series> Series { get; set; }
|
||||
public virtual DbSet<SeriesMetadata> SeriesMetadata { get; set; }
|
||||
public virtual DbSet<Track> Tracks { get; set; }
|
||||
public virtual DbSet<TrackMetadata> TrackMetadata { get; set; }
|
||||
public virtual DbSet<User> Users { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the default connection string.
|
||||
/// </summary>
|
||||
public static string ConnectionString { get; set; } = @"Data Source=jellyfin.db";
|
||||
|
||||
/// <inheritdoc />
|
||||
public JellyfinDb(DbContextOptions<JellyfinDb> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
partial void CustomInit(DbContextOptionsBuilder optionsBuilder);
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
CustomInit(optionsBuilder);
|
||||
}
|
||||
|
||||
partial void OnModelCreatingImpl(ModelBuilder modelBuilder);
|
||||
partial void OnModelCreatedImpl(ModelBuilder modelBuilder);
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
OnModelCreatingImpl(modelBuilder);
|
||||
|
||||
modelBuilder.HasDefaultSchema("jellyfin");
|
||||
|
||||
modelBuilder.Entity<Artwork>().HasIndex(t => t.Kind);
|
||||
|
||||
modelBuilder.Entity<Genre>().HasIndex(t => t.Name)
|
||||
.IsUnique();
|
||||
|
||||
modelBuilder.Entity<LibraryItem>().HasIndex(t => t.UrlId)
|
||||
.IsUnique();
|
||||
|
||||
OnModelCreatedImpl(modelBuilder);
|
||||
}
|
||||
|
||||
public override int SaveChanges()
|
||||
{
|
||||
foreach (var entity in ChangeTracker.Entries().Where(e => e.State == EntityState.Modified))
|
||||
{
|
||||
var saveEntity = entity.Entity as ISavingChanges;
|
||||
saveEntity.OnSavingChanges();
|
||||
}
|
||||
|
||||
return base.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
33
Jellyfin.Server.Implementations/JellyfinDbProvider.cs
Normal file
33
Jellyfin.Server.Implementations/JellyfinDbProvider.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Jellyfin.Server.Implementations
|
||||
{
|
||||
/// <summary>
|
||||
/// Factory class for generating new <see cref="JellyfinDb"/> instances.
|
||||
/// </summary>
|
||||
public class JellyfinDbProvider
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JellyfinDbProvider"/> class.
|
||||
/// </summary>
|
||||
/// <param name="serviceProvider">The application's service provider.</param>
|
||||
public JellyfinDbProvider(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
serviceProvider.GetService<JellyfinDb>().Database.Migrate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="JellyfinDb"/> context.
|
||||
/// </summary>
|
||||
/// <returns>The newly created context.</returns>
|
||||
public JellyfinDb CreateContext()
|
||||
{
|
||||
return _serviceProvider.GetService<JellyfinDb>();
|
||||
}
|
||||
}
|
||||
}
|
||||
1513
Jellyfin.Server.Implementations/Migrations/20200430215054_InitialSchema.Designer.cs
generated
Normal file
1513
Jellyfin.Server.Implementations/Migrations/20200430215054_InitialSchema.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,20 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
|
||||
namespace Jellyfin.Server.Implementations.Migrations
|
||||
{
|
||||
/// <summary>
|
||||
/// The design time factory for <see cref="JellyfinDb"/>.
|
||||
/// This is only used for the creation of migrations and not during runtime.
|
||||
/// </summary>
|
||||
internal class DesignTimeJellyfinDbFactory : IDesignTimeDbContextFactory<JellyfinDb>
|
||||
{
|
||||
public JellyfinDb CreateDbContext(string[] args)
|
||||
{
|
||||
var optionsBuilder = new DbContextOptionsBuilder<JellyfinDb>();
|
||||
optionsBuilder.UseSqlite("Data Source=jellyfin.db");
|
||||
|
||||
return new JellyfinDb(optionsBuilder.Options);
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user