Files
jellyfin-jellyfin-1/Jellyfin.Server.Implementations/JellyfinDbContext.cs

296 lines
9.9 KiB
C#
Raw Normal View History

using System;
2020-05-02 18:32:22 -04:00
using System.Linq;
using Jellyfin.Data.Entities;
using Jellyfin.Data.Entities.Security;
using Jellyfin.Data.Interfaces;
2020-05-02 18:32:22 -04:00
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
2024-10-09 15:20:42 +00:00
using Microsoft.Extensions.Logging;
2020-05-02 18:32:22 -04:00
namespace Jellyfin.Server.Implementations;
/// <inheritdoc/>
2024-10-09 15:20:42 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="JellyfinDbContext"/> class.
/// </summary>
/// <param name="options">The database context options.</param>
/// <param name="logger">Logger.</param>
public class JellyfinDbContext(DbContextOptions<JellyfinDbContext> options, ILogger<JellyfinDbContext> logger) : DbContext(options)
2020-05-02 18:32:22 -04:00
{
2023-01-16 12:13:06 -05:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the access schedules.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<AccessSchedule> AccessSchedules => Set<AccessSchedule>();
2023-01-16 12:13:06 -05:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the activity logs.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<ActivityLog> ActivityLogs => Set<ActivityLog>();
2020-05-15 17:24:01 -04:00
2023-01-16 12:13:06 -05:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the API keys.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<ApiKey> ApiKeys => Set<ApiKey>();
2023-01-16 12:13:06 -05:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the devices.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<Device> Devices => Set<Device>();
2021-04-10 16:15:59 -04:00
2023-01-16 12:13:06 -05:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the device options.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<DeviceOptions> DeviceOptions => Set<DeviceOptions>();
2021-04-10 16:15:59 -04:00
2023-01-16 12:13:06 -05:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the display preferences.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<DisplayPreferences> DisplayPreferences => Set<DisplayPreferences>();
2020-06-30 21:44:41 -04:00
2023-01-16 12:13:06 -05:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the image infos.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<ImageInfo> ImageInfos => Set<ImageInfo>();
2023-01-16 12:13:06 -05:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the item display preferences.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<ItemDisplayPreferences> ItemDisplayPreferences => Set<ItemDisplayPreferences>();
2023-01-16 12:13:06 -05:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the custom item display preferences.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<CustomItemDisplayPreferences> CustomItemDisplayPreferences => Set<CustomItemDisplayPreferences>();
2023-01-16 12:13:06 -05:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the permissions.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<Permission> Permissions => Set<Permission>();
2020-05-15 17:24:01 -04:00
2023-01-16 12:13:06 -05:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the preferences.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<Preference> Preferences => Set<Preference>();
2020-05-15 17:24:01 -04:00
2023-01-16 12:13:06 -05:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the users.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<User> Users => Set<User>();
2020-06-16 09:43:52 +12:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the trickplay metadata.
/// </summary>
public DbSet<TrickplayInfo> TrickplayInfos => Set<TrickplayInfo>();
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the media segments.
/// </summary>
public DbSet<MediaSegment> MediaSegments => Set<MediaSegment>();
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the user data.
/// </summary>
public DbSet<UserData> UserData => Set<UserData>();
2024-09-08 16:56:14 +00:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the user data.
/// </summary>
public DbSet<AncestorId> AncestorIds => Set<AncestorId>();
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the user data.
/// </summary>
public DbSet<AttachmentStreamInfo> AttachmentStreamInfos => Set<AttachmentStreamInfo>();
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the user data.
/// </summary>
public DbSet<BaseItemEntity> BaseItems => Set<BaseItemEntity>();
2024-09-08 16:56:14 +00:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the user data.
/// </summary>
public DbSet<Chapter> Chapters => Set<Chapter>();
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/>.
2024-09-08 16:56:14 +00:00
/// </summary>
public DbSet<ItemValue> ItemValues => Set<ItemValue>();
2024-10-10 14:32:49 +00:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/>.
/// </summary>
public DbSet<ItemValueMap> ItemValuesMap => Set<ItemValueMap>();
2024-09-08 16:56:14 +00:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/>.
2024-09-08 16:56:14 +00:00
/// </summary>
public DbSet<MediaStreamInfo> MediaStreamInfos => Set<MediaStreamInfo>();
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/>.
2024-09-08 16:56:14 +00:00
/// </summary>
public DbSet<People> Peoples => Set<People>();
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/>.
/// </summary>
public DbSet<PeopleBaseItemMap> PeopleBaseItemMap => Set<PeopleBaseItemMap>();
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/> containing the referenced Providers with ids.
/// </summary>
public DbSet<BaseItemProvider> BaseItemProviders => Set<BaseItemProvider>();
2024-10-09 23:01:54 +00:00
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/>.
/// </summary>
public DbSet<BaseItemImageInfo> BaseItemImageInfos => Set<BaseItemImageInfo>();
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/>.
/// </summary>
public DbSet<BaseItemMetadataField> BaseItemMetadataFields => Set<BaseItemMetadataField>();
/// <summary>
/// Gets the <see cref="DbSet{TEntity}"/>.
/// </summary>
public DbSet<BaseItemTrailerType> BaseItemTrailerTypes => Set<BaseItemTrailerType>();
2023-01-16 10:28:31 -05:00
/*public DbSet<Artwork> Artwork => Set<Artwork>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Book> Books => Set<Book>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<BookMetadata> BookMetadata => Set<BookMetadata>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Chapter> Chapters => Set<Chapter>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Collection> Collections => Set<Collection>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<CollectionItem> CollectionItems => Set<CollectionItem>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Company> Companies => Set<Company>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<CompanyMetadata> CompanyMetadata => Set<CompanyMetadata>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<CustomItem> CustomItems => Set<CustomItem>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<CustomItemMetadata> CustomItemMetadata => Set<CustomItemMetadata>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Episode> Episodes => Set<Episode>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<EpisodeMetadata> EpisodeMetadata => Set<EpisodeMetadata>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Genre> Genres => Set<Genre>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Group> Groups => Set<Groups>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Library> Libraries => Set<Library>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<LibraryItem> LibraryItems => Set<LibraryItems>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<LibraryRoot> LibraryRoot => Set<LibraryRoot>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<MediaFile> MediaFiles => Set<MediaFiles>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<MediaFileStream> MediaFileStream => Set<MediaFileStream>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Metadata> Metadata => Set<Metadata>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<MetadataProvider> MetadataProviders => Set<MetadataProvider>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<MetadataProviderId> MetadataProviderIds => Set<MetadataProviderId>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Movie> Movies => Set<Movie>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<MovieMetadata> MovieMetadata => Set<MovieMetadata>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<MusicAlbum> MusicAlbums => Set<MusicAlbum>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<MusicAlbumMetadata> MusicAlbumMetadata => Set<MusicAlbumMetadata>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Person> People => Set<Person>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<PersonRole> PersonRoles => Set<PersonRole>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Photo> Photo => Set<Photo>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<PhotoMetadata> PhotoMetadata => Set<PhotoMetadata>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<ProviderMapping> ProviderMappings => Set<ProviderMapping>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Rating> Ratings => Set<Rating>();
2020-05-02 18:32:22 -04:00
/// <summary>
/// Repository for global::Jellyfin.Data.Entities.RatingSource - This is the entity to
/// store review ratings, not age ratings.
/// </summary>
2023-01-16 10:28:31 -05:00
public DbSet<RatingSource> RatingSources => Set<RatingSource>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Release> Releases => Set<Release>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Season> Seasons => Set<Season>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<SeasonMetadata> SeasonMetadata => Set<SeasonMetadata>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Series> Series => Set<Series>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<SeriesMetadata> SeriesMetadata => Set<SeriesMetadata();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<Track> Tracks => Set<Track>();
2020-06-16 09:43:52 +12:00
2023-01-16 10:28:31 -05:00
public DbSet<TrackMetadata> TrackMetadata => Set<TrackMetadata>();*/
2020-05-02 18:32:22 -04:00
/// <inheritdoc/>
public override int SaveChanges()
{
foreach (var saveEntity in ChangeTracker.Entries()
.Where(e => e.State == EntityState.Modified)
.Select(entry => entry.Entity)
.OfType<IHasConcurrencyToken>())
2020-05-02 18:32:22 -04:00
{
saveEntity.OnSavingChanges();
2020-05-22 20:20:18 -04:00
}
2020-05-02 18:32:22 -04:00
2024-10-09 15:20:42 +00:00
try
{
return base.SaveChanges();
}
catch (Exception e)
{
logger.LogError(e, "Error trying to save changes.");
throw;
}
}
2020-05-02 18:32:22 -04:00
/// <inheritdoc />
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.SetDefaultDateTimeKind(DateTimeKind.Utc);
base.OnModelCreating(modelBuilder);
// Configuration for each entity is in its own class inside 'ModelConfiguration'.
2023-01-16 12:14:44 -05:00
modelBuilder.ApplyConfigurationsFromAssembly(typeof(JellyfinDbContext).Assembly);
2020-05-02 18:32:22 -04:00
}
/// <inheritdoc/>
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Conventions.Add(_ => new DoNotUseReturningClauseConvention());
}
private class DoNotUseReturningClauseConvention : IModelFinalizingConvention
{
public void ProcessModelFinalizing(
IConventionModelBuilder modelBuilder,
IConventionContext<IConventionModelBuilder> context)
{
foreach (var entityType in modelBuilder.Metadata.GetEntityTypes())
{
entityType.UseSqlReturningClause(false);
}
}
}
2020-05-02 18:32:22 -04:00
}