Commit 560c3c3e authored by tidusjar's avatar tidusjar

Finished it!

parent 1a086e6f
......@@ -7,12 +7,12 @@ namespace Ombi.Core.Rule.Rules.Request
{
public class SonarrCacheRequestRule : BaseRequestRule, IRules<BaseRequest>
{
public SonarrCacheRequestRule(IExternalContext ctx)
public SonarrCacheRequestRule(ExternalContext ctx)
{
_ctx = ctx;
}
private readonly IExternalContext _ctx;
private readonly ExternalContext _ctx;
public Task<RuleResult> Execute(BaseRequest obj)
{
......
......@@ -34,12 +34,12 @@ namespace Ombi.Core.Rule.Rules.Search
{
public class SonarrCacheSearchRule : BaseSearchRule, IRules<SearchViewModel>
{
public SonarrCacheSearchRule(IExternalContext ctx)
public SonarrCacheSearchRule(ExternalContext ctx)
{
_ctx = ctx;
}
private readonly IExternalContext _ctx;
private readonly ExternalContext _ctx;
public Task<RuleResult> Execute(SearchViewModel obj)
{
......
......@@ -10,12 +10,12 @@ namespace Ombi.Core.Rule.Rules
{
public class SonarrCacheRule
{
public SonarrCacheRule(IExternalContext ctx)
public SonarrCacheRule(ExternalContext ctx)
{
_ctx = ctx;
}
private readonly IExternalContext _ctx;
private readonly ExternalContext _ctx;
public async Task<RuleResult> Execute(BaseRequest obj)
{
......
......@@ -136,12 +136,12 @@ namespace Ombi.DependencyInjection
public static void RegisterStore(this IServiceCollection services) {
//services.AddDbContext<OmbiContext>();
services.AddDbContext<SettingsContext>();
services.AddDbContext<ExternalContext>();
//services.AddDbContext<SettingsContext>();
//services.AddDbContext<ExternalContext>();
//services.AddScoped<OmbiContext, OmbiContext>(); // https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6
services.AddScoped<ISettingsContext, SettingsContext>(); // https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6
services.AddScoped<IExternalContext, ExternalContext>(); // https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6
//services.AddScoped<ISettingsContext, SettingsContext>(); // https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6
//services.AddScoped<ExternalContext, ExternalContext>(); // https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6
services.AddScoped<ISettingsRepository, SettingsJsonRepository>();
services.AddScoped<ISettingsResolver, SettingsResolver>();
services.AddScoped<IPlexContentRepository, PlexServerContentRepository>();
......
......@@ -43,7 +43,7 @@ namespace Ombi.Schedule.Jobs.Couchpotato
public class CouchPotatoSync : ICouchPotatoSync
{
public CouchPotatoSync(ISettingsService<CouchPotatoSettings> cpSettings,
ICouchPotatoApi api, ILogger<CouchPotatoSync> log, IExternalContext ctx)
ICouchPotatoApi api, ILogger<CouchPotatoSync> log, ExternalContext ctx)
{
_settings = cpSettings;
_api = api;
......@@ -55,7 +55,7 @@ namespace Ombi.Schedule.Jobs.Couchpotato
private readonly ISettingsService<CouchPotatoSettings> _settings;
private readonly ICouchPotatoApi _api;
private readonly ILogger<CouchPotatoSync> _log;
private readonly IExternalContext _ctx;
private readonly ExternalContext _ctx;
public async Task Execute(IJobExecutionContext job)
{
......
......@@ -17,7 +17,7 @@ namespace Ombi.Schedule.Jobs.Lidarr
{
public class LidarrAlbumSync : ILidarrAlbumSync
{
public LidarrAlbumSync(ISettingsService<LidarrSettings> lidarr, ILidarrApi lidarrApi, ILogger<LidarrAlbumSync> log, IExternalContext ctx,
public LidarrAlbumSync(ISettingsService<LidarrSettings> lidarr, ILidarrApi lidarrApi, ILogger<LidarrAlbumSync> log, ExternalContext ctx,
IBackgroundJobClient job, ILidarrAvailabilityChecker availability)
{
_lidarrSettings = lidarr;
......@@ -31,7 +31,7 @@ namespace Ombi.Schedule.Jobs.Lidarr
private readonly ISettingsService<LidarrSettings> _lidarrSettings;
private readonly ILidarrApi _lidarrApi;
private readonly ILogger _logger;
private readonly IExternalContext _ctx;
private readonly ExternalContext _ctx;
private readonly IBackgroundJobClient _job;
private readonly ILidarrAvailabilityChecker _availability;
......
......@@ -18,7 +18,7 @@ namespace Ombi.Schedule.Jobs.Lidarr
{
public class LidarrArtistSync : ILidarrArtistSync
{
public LidarrArtistSync(ISettingsService<LidarrSettings> lidarr, ILidarrApi lidarrApi, ILogger<LidarrArtistSync> log, IExternalContext ctx,
public LidarrArtistSync(ISettingsService<LidarrSettings> lidarr, ILidarrApi lidarrApi, ILogger<LidarrArtistSync> log, ExternalContext ctx,
IBackgroundJobClient background, ILidarrAlbumSync album)
{
_lidarrSettings = lidarr;
......@@ -32,7 +32,7 @@ namespace Ombi.Schedule.Jobs.Lidarr
private readonly ISettingsService<LidarrSettings> _lidarrSettings;
private readonly ILidarrApi _lidarrApi;
private readonly ILogger _logger;
private readonly IExternalContext _ctx;
private readonly ExternalContext _ctx;
private readonly IBackgroundJobClient _job;
private readonly ILidarrAlbumSync _albumSync;
......
......@@ -17,7 +17,7 @@ namespace Ombi.Schedule.Jobs.Radarr
{
public class RadarrSync : IRadarrSync
{
public RadarrSync(ISettingsService<RadarrSettings> radarr, IRadarrApi radarrApi, ILogger<RadarrSync> log, IExternalContext ctx)
public RadarrSync(ISettingsService<RadarrSettings> radarr, IRadarrApi radarrApi, ILogger<RadarrSync> log, ExternalContext ctx)
{
RadarrSettings = radarr;
RadarrApi = radarrApi;
......@@ -29,7 +29,7 @@ namespace Ombi.Schedule.Jobs.Radarr
private ISettingsService<RadarrSettings> RadarrSettings { get; }
private IRadarrApi RadarrApi { get; }
private ILogger<RadarrSync> Logger { get; }
private readonly IExternalContext _ctx;
private readonly ExternalContext _ctx;
private static readonly SemaphoreSlim SemaphoreSlim = new SemaphoreSlim(1, 1);
......
......@@ -17,7 +17,7 @@ namespace Ombi.Schedule.Jobs.SickRage
{
public class SickRageSync : ISickRageSync
{
public SickRageSync(ISettingsService<SickRageSettings> s, ISickRageApi api, ILogger<SickRageSync> l, IExternalContext ctx)
public SickRageSync(ISettingsService<SickRageSettings> s, ISickRageApi api, ILogger<SickRageSync> l, ExternalContext ctx)
{
_settings = s;
_api = api;
......@@ -29,7 +29,7 @@ namespace Ombi.Schedule.Jobs.SickRage
private readonly ISettingsService<SickRageSettings> _settings;
private readonly ISickRageApi _api;
private readonly ILogger<SickRageSync> _log;
private readonly IExternalContext _ctx;
private readonly ExternalContext _ctx;
public async Task Execute(IJobExecutionContext job)
{
......
......@@ -20,7 +20,7 @@ namespace Ombi.Schedule.Jobs.Sonarr
{
public class SonarrSync : ISonarrSync
{
public SonarrSync(ISettingsService<SonarrSettings> s, ISonarrApi api, ILogger<SonarrSync> l, IExternalContext ctx)
public SonarrSync(ISettingsService<SonarrSettings> s, ISonarrApi api, ILogger<SonarrSync> l, ExternalContext ctx)
{
_settings = s;
_api = api;
......@@ -32,7 +32,7 @@ namespace Ombi.Schedule.Jobs.Sonarr
private readonly ISettingsService<SonarrSettings> _settings;
private readonly ISonarrApi _api;
private readonly ILogger<SonarrSync> _log;
private readonly IExternalContext _ctx;
private readonly ExternalContext _ctx;
public async Task Execute(IJobExecutionContext job)
{
......
......@@ -5,16 +5,22 @@ using Ombi.Store.Entities;
namespace Ombi.Store.Context
{
public sealed class ExternalContext : DbContext, IExternalContext
public abstract class ExternalContext : DbContext
{
private static bool _created;
public ExternalContext()
protected ExternalContext(DbContextOptions<ExternalContext> options) : base(options)
{
if (_created) return;
_created = true;
Database.SetCommandTimeout(60);
Database.Migrate();
}
/// <summary>
/// This allows a sub class to call the base class 'DbContext' non typed constructor
/// This is need because instances of the subclasses will use a specific typed DbContextOptions
/// which can not be converted into the parameter in the above constructor
/// </summary>
/// <param name="options"></param>
protected ExternalContext(DbContextOptions options)
: base(options)
{
}
public DbSet<PlexServerContent> PlexServerContent { get; set; }
......@@ -32,16 +38,6 @@ namespace Ombi.Store.Context
public DbSet<SickRageCache> SickRageCache { get; set; }
public DbSet<SickRageEpisodeCache> SickRageEpisodeCache { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var i = StoragePathSingleton.Instance;
if (string.IsNullOrEmpty(i.StoragePath))
{
i.StoragePath = string.Empty;
}
optionsBuilder.UseSqlite($"Data Source={Path.Combine(i.StoragePath, "OmbiExternal.db")}");
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<PlexServerContent>().HasMany(x => x.Episodes)
......@@ -57,18 +53,5 @@ namespace Ombi.Store.Context
base.OnModelCreating(builder);
}
public void Seed()
{
// VACUUM;
Database.ExecuteSqlCommand("VACUUM;");
using (var tran = Database.BeginTransaction())
{
SaveChanges();
tran.Commit();
}
}
}
}
\ No newline at end of file
using Microsoft.EntityFrameworkCore;
using Ombi.Store.Entities;
namespace Ombi.Store.Context
{
public interface IExternalContext : IDbContext
{
DbSet<CouchPotatoCache> CouchPotatoCache { get; set; }
DbSet<EmbyContent> EmbyContent { get; set; }
DbSet<EmbyEpisode> EmbyEpisode { get; set; }
DbSet<LidarrAlbumCache> LidarrAlbumCache { get; set; }
DbSet<LidarrArtistCache> LidarrArtistCache { get; set; }
DbSet<PlexEpisode> PlexEpisode { get; set; }
DbSet<PlexServerContent> PlexServerContent { get; set; }
DbSet<RadarrCache> RadarrCache { get; set; }
DbSet<SickRageCache> SickRageCache { get; set; }
DbSet<SickRageEpisodeCache> SickRageEpisodeCache { get; set; }
DbSet<SonarrCache> SonarrCache { get; set; }
DbSet<SonarrEpisodeCache> SonarrEpisodeCache { get; set; }
}
}
\ No newline at end of file
using Microsoft.EntityFrameworkCore;
using Ombi.Store.Entities;
namespace Ombi.Store.Context
{
public interface ISettingsContext : IDbContext
{
DbSet<ApplicationConfiguration> ApplicationConfigurations { get; set; }
DbSet<GlobalSettings> Settings { get; set; }
}
}
\ No newline at end of file
using Microsoft.EntityFrameworkCore;
namespace Ombi.Store.Context.MySql
{
public sealed class ExternalMySqlContext : ExternalContext
{
private static bool _created;
public ExternalMySqlContext(DbContextOptions<ExternalMySqlContext> options) : base(options)
{
if (_created) return;
_created = true;
Database.Migrate();
}
}
}
\ No newline at end of file
using Microsoft.EntityFrameworkCore;
using Ombi.Store.Entities;
namespace Ombi.Store.Context
namespace Ombi.Store.Context.MySql
{
public sealed class OmbiMySqlContext : OmbiContext
{
......
using Microsoft.EntityFrameworkCore;
namespace Ombi.Store.Context.MySql
{
public sealed class SettingsMySqlContext : SettingsContext
{
private static bool _created;
public SettingsMySqlContext(DbContextOptions<SettingsMySqlContext> options) : base(options)
{
if (_created) return;
_created = true;
Database.Migrate();
}
}
}
\ No newline at end of file
......@@ -50,21 +50,6 @@ namespace Ombi.Store.Context
public DbSet<UserNotificationPreferences> UserNotificationPreferences { get; set; }
public DbSet<UserQualityProfiles> UserQualityProfileses { get; set; }
public DbSet<RequestQueue> RequestQueue { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<PlexServerContent>().HasMany(x => x.Episodes)
.WithOne(x => x.Series)
.HasPrincipalKey(x => x.Key)
.HasForeignKey(x => x.GrandparentKey);
builder.Entity<EmbyEpisode>()
.HasOne(p => p.Series)
.WithMany(b => b.Episodes)
.HasPrincipalKey(x => x.EmbyId)
.HasForeignKey(p => p.ParentId);
base.OnModelCreating(builder);
}
public void Seed()
{
......
using System.IO;
using System.Linq;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Ombi.Helpers;
using Ombi.Store.Entities;
namespace Ombi.Store.Context
{
public sealed class SettingsContext : DbContext, ISettingsContext
public abstract class SettingsContext : DbContext
{
private static bool _created;
public SettingsContext()
protected SettingsContext(DbContextOptions<SettingsContext> options) : base(options)
{
if (_created) return;
_created = true;
Database.SetCommandTimeout(60);
Database.Migrate();
}
public DbSet<GlobalSettings> Settings { get; set; }
public DbSet<ApplicationConfiguration> ApplicationConfigurations { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
/// <summary>
/// This allows a sub class to call the base class 'DbContext' non typed constructor
/// This is need because instances of the subclasses will use a specific typed DbContextOptions
/// which can not be converted into the parameter in the above constructor
/// </summary>
/// <param name="options"></param>
protected SettingsContext(DbContextOptions options)
: base(options)
{
var i = StoragePathSingleton.Instance;
if (string.IsNullOrEmpty(i.StoragePath))
{
i.StoragePath = string.Empty;
}
optionsBuilder.UseSqlite($"Data Source={Path.Combine(i.StoragePath, "OmbiSettings.db")}");
}
public DbSet<GlobalSettings> Settings { get; set; }
public DbSet<ApplicationConfiguration> ApplicationConfigurations { get; set; }
public void Seed()
{
using (var tran = Database.BeginTransaction())
{
// Add the tokens
......
using Microsoft.EntityFrameworkCore;
namespace Ombi.Store.Context.Sqlite
{
public sealed class ExternalSqliteContext : ExternalContext
{
private static bool _created;
public ExternalSqliteContext(DbContextOptions<ExternalSqliteContext> options) : base(options)
{
if (_created) return;
_created = true;
Database.SetCommandTimeout(60);
Database.Migrate();
}
}
}
\ No newline at end of file
using Microsoft.EntityFrameworkCore;
namespace Ombi.Store.Context
namespace Ombi.Store.Context.Sqlite
{
public sealed class OmbiSqliteContext : OmbiContext
{
......
using Microsoft.EntityFrameworkCore;
namespace Ombi.Store.Context.Sqlite
{
public sealed class SettingsSqliteContext : SettingsContext
{
private static bool _created;
public SettingsSqliteContext(DbContextOptions<SettingsSqliteContext> options) : base(options)
{
if (_created) return;
_created = true;
Database.SetCommandTimeout(60);
Database.Migrate();
}
}
}
\ No newline at end of file
dotnet ef migrations add Inital --context OmbiContext --startup-project ../Ombi/Ombi.csproj
\ No newline at end of file
dotnet ef migrations add Inital --context OmbiContext --startup-project ../Ombi/Ombi.csproj
If running migrations for any db provider other than Sqlite, then ensure the database.json is pointing at the correct DB type
\ No newline at end of file
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Ombi.Store.Context.MySql;
namespace Ombi.Store.Migrations.ExternalMySql
{
[DbContext(typeof(ExternalMySqlContext))]
[Migration("20191103213915_Inital")]
partial class Inital
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.6-servicing-10079")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("Ombi.Store.Entities.CouchPotatoCache", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("TheMovieDbId");
b.HasKey("Id");
b.ToTable("CouchPotatoCache");
});
modelBuilder.Entity("Ombi.Store.Entities.EmbyContent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<DateTime>("AddedAt");
b.Property<string>("EmbyId")
.IsRequired();
b.Property<string>("ImdbId");
b.Property<string>("ProviderId");
b.Property<string>("TheMovieDbId");
b.Property<string>("Title");
b.Property<string>("TvDbId");
b.Property<int>("Type");
b.Property<string>("Url");
b.HasKey("Id");
b.ToTable("EmbyContent");
});
modelBuilder.Entity("Ombi.Store.Entities.EmbyEpisode", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<DateTime>("AddedAt");
b.Property<string>("EmbyId");
b.Property<int>("EpisodeNumber");
b.Property<string>("ImdbId");
b.Property<string>("ParentId");
b.Property<string>("ProviderId");
b.Property<int>("SeasonNumber");
b.Property<string>("TheMovieDbId");
b.Property<string>("Title");
b.Property<string>("TvDbId");
b.HasKey("Id");
b.HasIndex("ParentId");
b.ToTable("EmbyEpisode");
});
modelBuilder.Entity("Ombi.Store.Entities.LidarrAlbumCache", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<DateTime>("AddedAt");
b.Property<int>("ArtistId");
b.Property<string>("ForeignAlbumId");
b.Property<bool>("Monitored");
b.Property<decimal>("PercentOfTracks");
b.Property<DateTime>("ReleaseDate");
b.Property<string>("Title");
b.Property<int>("TrackCount");
b.HasKey("Id");
b.ToTable("LidarrAlbumCache");
});
modelBuilder.Entity("Ombi.Store.Entities.LidarrArtistCache", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("ArtistId");
b.Property<string>("ArtistName");
b.Property<string>("ForeignArtistId");
b.Property<bool>("Monitored");
b.HasKey("Id");
b.ToTable("LidarrArtistCache");
});
modelBuilder.Entity("Ombi.Store.Entities.PlexEpisode", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("EpisodeNumber");
b.Property<int>("GrandparentKey");
b.Property<int>("Key");
b.Property<int>("ParentKey");
b.Property<int>("SeasonNumber");
b.Property<string>("Title");
b.HasKey("Id");
b.HasIndex("GrandparentKey");
b.ToTable("PlexEpisode");
});
modelBuilder.Entity("Ombi.Store.Entities.PlexSeasonsContent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("ParentKey");
b.Property<int>("PlexContentId");
b.Property<int?>("PlexServerContentId");
b.Property<int>("SeasonKey");
b.Property<int>("SeasonNumber");
b.HasKey("Id");
b.HasIndex("PlexServerContentId");
b.ToTable("PlexSeasonsContent");
});
modelBuilder.Entity("Ombi.Store.Entities.PlexServerContent", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<DateTime>("AddedAt");
b.Property<string>("ImdbId");
b.Property<int>("Key");
b.Property<string>("Quality");
b.Property<string>("ReleaseYear");
b.Property<int?>("RequestId");
b.Property<string>("TheMovieDbId");
b.Property<string>("Title");
b.Property<string>("TvDbId");
b.Property<int>("Type");
b.Property<string>("Url");
b.HasKey("Id");
b.ToTable("PlexServerContent");
});
modelBuilder.Entity("Ombi.Store.Entities.RadarrCache", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<bool>("HasFile");
b.Property<int>("TheMovieDbId");