2020-12-12 17:20:48 +01:00
|
|
|
#nullable disable
|
2020-05-03 00:32:22 +02:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2020-11-25 15:44:11 +01:00
|
|
|
using System;
|
2020-05-03 00:32:22 +02:00
|
|
|
using System.Linq;
|
|
|
|
using Jellyfin.Data.Entities;
|
2021-04-01 17:08:39 +02:00
|
|
|
using Jellyfin.Data.Entities.Security;
|
2020-08-31 00:50:54 +02:00
|
|
|
using Jellyfin.Data.Interfaces;
|
2020-05-03 00:32:22 +02:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
namespace Jellyfin.Server.Implementations
|
|
|
|
{
|
|
|
|
/// <inheritdoc/>
|
2020-09-05 23:54:34 +02:00
|
|
|
public class JellyfinDb : DbContext
|
2020-05-03 00:32:22 +02:00
|
|
|
{
|
2020-05-23 02:20:18 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="JellyfinDb"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="options">The database context options.</param>
|
|
|
|
public JellyfinDb(DbContextOptions<JellyfinDb> options) : base(options)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the default connection string.
|
|
|
|
/// </summary>
|
|
|
|
public static string ConnectionString { get; set; } = @"Data Source=jellyfin.db";
|
|
|
|
|
2020-06-13 04:47:09 +02:00
|
|
|
public virtual DbSet<AccessSchedule> AccessSchedules { get; set; }
|
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<ActivityLog> ActivityLogs { get; set; }
|
2020-05-15 23:24:01 +02:00
|
|
|
|
2021-04-01 17:08:39 +02:00
|
|
|
public virtual DbSet<ApiKey> ApiKeys { get; set; }
|
|
|
|
|
2021-04-10 22:15:59 +02:00
|
|
|
public virtual DbSet<Device> Devices { get; set; }
|
|
|
|
|
|
|
|
public virtual DbSet<DeviceOptions> DeviceOptions { get; set; }
|
|
|
|
|
2020-07-01 03:44:41 +02:00
|
|
|
public virtual DbSet<DisplayPreferences> DisplayPreferences { get; set; }
|
|
|
|
|
2020-06-13 04:47:09 +02:00
|
|
|
public virtual DbSet<ImageInfo> ImageInfos { get; set; }
|
|
|
|
|
2020-07-28 02:40:21 +02:00
|
|
|
public virtual DbSet<ItemDisplayPreferences> ItemDisplayPreferences { get; set; }
|
|
|
|
|
2020-12-03 21:51:12 +01:00
|
|
|
public virtual DbSet<CustomItemDisplayPreferences> CustomItemDisplayPreferences { get; set; }
|
|
|
|
|
2020-05-15 23:24:01 +02:00
|
|
|
public virtual DbSet<Permission> Permissions { get; set; }
|
|
|
|
|
|
|
|
public virtual DbSet<Preference> Preferences { get; set; }
|
|
|
|
|
2020-05-21 06:22:43 +02:00
|
|
|
public virtual DbSet<User> Users { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 01:26:24 +02:00
|
|
|
/*public virtual DbSet<Artwork> Artwork { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Book> Books { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<BookMetadata> BookMetadata { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Chapter> Chapters { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Collection> Collections { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<CollectionItem> CollectionItems { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Company> Companies { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<CompanyMetadata> CompanyMetadata { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<CustomItem> CustomItems { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<CustomItemMetadata> CustomItemMetadata { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Episode> Episodes { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<EpisodeMetadata> EpisodeMetadata { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Genre> Genres { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-31 04:11:53 +02:00
|
|
|
public virtual DbSet<Group> Groups { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Library> Libraries { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<LibraryItem> LibraryItems { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<LibraryRoot> LibraryRoot { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<MediaFile> MediaFiles { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<MediaFileStream> MediaFileStream { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Metadata> Metadata { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<MetadataProvider> MetadataProviders { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<MetadataProviderId> MetadataProviderIds { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Movie> Movies { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<MovieMetadata> MovieMetadata { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<MusicAlbum> MusicAlbums { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<MusicAlbumMetadata> MusicAlbumMetadata { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Person> People { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<PersonRole> PersonRoles { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Photo> Photo { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<PhotoMetadata> PhotoMetadata { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<ProviderMapping> ProviderMappings { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Rating> Ratings { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Repository for global::Jellyfin.Data.Entities.RatingSource - This is the entity to
|
2020-06-16 00:37:52 +02:00
|
|
|
/// store review ratings, not age ratings.
|
2020-05-03 00:32:22 +02:00
|
|
|
/// </summary>
|
|
|
|
public virtual DbSet<RatingSource> RatingSources { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Release> Releases { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Season> Seasons { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<SeasonMetadata> SeasonMetadata { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Series> Series { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<SeriesMetadata> SeriesMetadata { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-03 00:32:22 +02:00
|
|
|
public virtual DbSet<Track> Tracks { get; set; }
|
2020-06-15 23:43:52 +02:00
|
|
|
|
2020-05-13 04:25:45 +02:00
|
|
|
public virtual DbSet<TrackMetadata> TrackMetadata { get; set; }*/
|
2020-05-03 00:32:22 +02:00
|
|
|
|
2020-05-23 02:20:18 +02:00
|
|
|
/// <inheritdoc/>
|
|
|
|
public override int SaveChanges()
|
2020-05-03 00:32:22 +02:00
|
|
|
{
|
2020-05-23 02:20:18 +02:00
|
|
|
foreach (var saveEntity in ChangeTracker.Entries()
|
|
|
|
.Where(e => e.State == EntityState.Modified)
|
2020-05-31 23:00:57 +02:00
|
|
|
.Select(entry => entry.Entity)
|
2020-08-31 00:50:54 +02:00
|
|
|
.OfType<IHasConcurrencyToken>())
|
2020-05-23 02:20:18 +02:00
|
|
|
{
|
|
|
|
saveEntity.OnSavingChanges();
|
|
|
|
}
|
2020-05-03 00:32:22 +02:00
|
|
|
|
2020-05-23 02:20:18 +02:00
|
|
|
return base.SaveChanges();
|
|
|
|
}
|
2020-05-03 00:32:22 +02:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
{
|
2020-11-25 15:44:11 +01:00
|
|
|
modelBuilder.SetDefaultDateTimeKind(DateTimeKind.Utc);
|
2020-05-03 00:32:22 +02:00
|
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
|
|
|
|
modelBuilder.HasDefaultSchema("jellyfin");
|
|
|
|
|
2021-03-19 01:24:37 +01:00
|
|
|
// Collations
|
|
|
|
|
|
|
|
modelBuilder.Entity<User>()
|
|
|
|
.Property(user => user.Username)
|
|
|
|
.UseCollation("NOCASE");
|
|
|
|
|
|
|
|
// Delete behavior
|
|
|
|
|
2021-03-18 02:41:52 +01:00
|
|
|
modelBuilder.Entity<User>()
|
|
|
|
.HasOne(u => u.ProfileImage)
|
|
|
|
.WithOne()
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
|
|
modelBuilder.Entity<User>()
|
|
|
|
.HasMany(u => u.Permissions)
|
|
|
|
.WithOne()
|
2021-03-19 05:26:00 +01:00
|
|
|
.HasForeignKey(p => p.UserId)
|
2021-03-18 02:41:52 +01:00
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
|
|
modelBuilder.Entity<User>()
|
|
|
|
.HasMany(u => u.Preferences)
|
|
|
|
.WithOne()
|
2021-03-19 05:26:00 +01:00
|
|
|
.HasForeignKey(p => p.UserId)
|
2021-03-18 02:41:52 +01:00
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
|
|
modelBuilder.Entity<User>()
|
|
|
|
.HasMany(u => u.AccessSchedules)
|
|
|
|
.WithOne()
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
|
|
modelBuilder.Entity<User>()
|
|
|
|
.HasMany(u => u.DisplayPreferences)
|
|
|
|
.WithOne()
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
2021-03-19 05:26:07 +01:00
|
|
|
modelBuilder.Entity<User>()
|
|
|
|
.HasMany(u => u.ItemDisplayPreferences)
|
|
|
|
.WithOne()
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
|
|
modelBuilder.Entity<DisplayPreferences>()
|
|
|
|
.HasMany(d => d.HomeSections)
|
|
|
|
.WithOne()
|
|
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
2021-03-19 01:24:37 +01:00
|
|
|
// Indexes
|
|
|
|
|
2021-04-01 17:08:39 +02:00
|
|
|
modelBuilder.Entity<ApiKey>()
|
|
|
|
.HasIndex(entity => entity.AccessToken)
|
|
|
|
.IsUnique();
|
|
|
|
|
2021-03-19 01:24:37 +01:00
|
|
|
modelBuilder.Entity<User>()
|
|
|
|
.HasIndex(entity => entity.Username)
|
|
|
|
.IsUnique();
|
2021-03-19 01:09:11 +01:00
|
|
|
|
2020-09-05 23:54:34 +02:00
|
|
|
modelBuilder.Entity<DisplayPreferences>()
|
2020-12-05 00:00:11 +01:00
|
|
|
.HasIndex(entity => new { entity.UserId, entity.ItemId, entity.Client })
|
2020-09-05 23:54:34 +02:00
|
|
|
.IsUnique();
|
2020-12-03 21:51:12 +01:00
|
|
|
|
|
|
|
modelBuilder.Entity<CustomItemDisplayPreferences>()
|
2020-12-05 00:00:11 +01:00
|
|
|
.HasIndex(entity => new { entity.UserId, entity.ItemId, entity.Client, entity.Key })
|
2020-12-03 21:51:12 +01:00
|
|
|
.IsUnique();
|
2021-03-19 01:09:11 +01:00
|
|
|
|
|
|
|
// Used to get a user's permissions or a specific permission for a user.
|
|
|
|
// Also prevents multiple values being created for a user.
|
|
|
|
// Filtered over non-null user ids for when other entities (groups, API keys) get permissions
|
|
|
|
modelBuilder.Entity<Permission>()
|
|
|
|
.HasIndex(p => new { p.UserId, p.Kind })
|
|
|
|
.HasFilter("[UserId] IS NOT NULL")
|
|
|
|
.IsUnique();
|
|
|
|
|
|
|
|
modelBuilder.Entity<Preference>()
|
|
|
|
.HasIndex(p => new { p.UserId, p.Kind })
|
|
|
|
.HasFilter("[UserId] IS NOT NULL")
|
|
|
|
.IsUnique();
|
2020-05-03 00:32:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|