Got MySql working

pull/3241/head
tidusjar 5 years ago
parent 37aca4e423
commit 1a086e6f19

1
.gitignore vendored

@ -246,3 +246,4 @@ _Pvt_Extensions
# Ignore local vscode config
*.vscode
/src/Ombi/database.json

@ -8,7 +8,6 @@ using Microsoft.EntityFrameworkCore;
using Ombi.Core.Authentication;
using Ombi.Core.Rule.Interfaces;
using Ombi.Helpers;
using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests;
namespace Ombi.Core.Rule.Rules.Request

@ -12,7 +12,7 @@ namespace Ombi.Schedule.Tests
[Test]
[Ignore("Cannot get this to work")]
public async Task Test()
public Task Test()
{
var scheduleMock = new Mock<IScheduler>();
scheduleMock.Setup(x => x.TriggerJob(It.IsAny<JobKey>(),
@ -23,6 +23,8 @@ namespace Ombi.Schedule.Tests
scheduleMock.Verify(x => x.TriggerJob(It.Is<JobKey>(j => j.Name == "ABC"),
default(CancellationToken)), Times.Once);
return Task.CompletedTask;
}
}
public class QuartzMock : OmbiQuartz

@ -12,12 +12,9 @@ namespace Ombi.Store.Context
{
public abstract class OmbiContext : IdentityDbContext<OmbiUser>
{
private static bool _created;
protected OmbiContext(DbContextOptions<OmbiContext> options) : base(options)
{
if (_created) return;
_created = true;
}
/// <summary>
@ -33,15 +30,6 @@ namespace Ombi.Store.Context
public DbSet<NotificationTemplates> NotificationTemplates { get; set; }
public DbSet<ApplicationConfiguration> ApplicationConfigurations { get; set; }
public DbSet<PlexServerContent> PlexServerContent { get; set; }
public DbSet<PlexSeasonsContent> PlexSeasonsContent { get; set; }
public DbSet<PlexEpisode> PlexEpisode { get; set; }
public DbSet<GlobalSettings> Settings { get; set; }
public DbSet<RadarrCache> RadarrCache { get; set; }
public DbSet<CouchPotatoCache> CouchPotatoCache { get; set; }
public DbSet<EmbyContent> EmbyContent { get; set; }
public DbSet<EmbyEpisode> EmbyEpisode { get; set; }
public DbSet<MovieRequests> MovieRequests { get; set; }
public DbSet<AlbumRequest> AlbumRequests { get; set; }
@ -58,16 +46,25 @@ namespace Ombi.Store.Context
public DbSet<Audit> Audit { get; set; }
public DbSet<Tokens> Tokens { get; set; }
public DbSet<SonarrCache> SonarrCache { get; set; }
public DbSet<LidarrArtistCache> LidarrArtistCache { get; set; }
public DbSet<LidarrAlbumCache> LidarrAlbumCache { get; set; }
public DbSet<SonarrEpisodeCache> SonarrEpisodeCache { get; set; }
public DbSet<SickRageCache> SickRageCache { get; set; }
public DbSet<SickRageEpisodeCache> SickRageEpisodeCache { get; set; }
public DbSet<RequestSubscription> RequestSubscription { get; set; }
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()
{

@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Ombi.Store.Entities;
namespace Ombi.Store.Context
{
public sealed class OmbiMySqlContext : OmbiContext
{
private static bool _created;
public OmbiMySqlContext(DbContextOptions<OmbiMySqlContext> options) : base(options)
{
if (_created) return;
_created = true;
Database.Migrate();
}
}
}

@ -1,5 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Ombi.Store.Entities;
namespace Ombi.Store.Context
{
@ -15,21 +14,5 @@ namespace Ombi.Store.Context
Database.SetCommandTimeout(60);
Database.Migrate();
}
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);
}
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

@ -15,9 +15,10 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.6" />
<PackageReference Include="Nito.AsyncEx" Version="5.0.0-pre-05" />
<PackageReference Include="Polly" Version="7.1.0" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="1.1.9" />
<!--<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="1.1.9" />-->
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />

@ -105,13 +105,6 @@ namespace Ombi.Store.Repository.Requests
await InternalSaveChanges();
}
public async Task<TvRequests> Add(TvRequests request)
{
await Db.TvRequests.AddAsync(request);
await InternalSaveChanges();
return request;
}
public async Task<ChildRequests> AddChild(ChildRequests request)
{
await Db.ChildRequests.AddAsync(request);
@ -120,12 +113,6 @@ namespace Ombi.Store.Repository.Requests
return request;
}
public async Task Delete(TvRequests request)
{
Db.TvRequests.Remove(request);
await InternalSaveChanges();
}
public async Task DeleteChild(ChildRequests request)
{
Db.ChildRequests.Remove(request);

@ -150,7 +150,7 @@ namespace Ombi.Controllers
/// <summary>
/// Returns similar movies to the movie id passed in
/// </summary>
/// <param name="theMovieDbId">ID of the movie</param>
/// <param name="model">the movie</param>
/// <remarks>
/// We use TheMovieDb as the Movie Provider
/// </remarks>

@ -1,26 +0,0 @@
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Ombi.Helpers;
using Ombi.Store.Context;
namespace Ombi
{
public static class DatabaseExtensions
{
public static void ConfigureDatabases(this IServiceCollection services)
{
services.AddDbContext<OmbiContext, OmbiSqliteContext>(ConfigureSqlite);
}
private static void ConfigureSqlite(DbContextOptionsBuilder options)
{
var i = StoragePathSingleton.Instance;
if (string.IsNullOrEmpty(i.StoragePath))
{
i.StoragePath = string.Empty;
}
options.UseSqlite($"Data Source={Path.Combine(i.StoragePath, "Ombi.db")}");
}
}
}

@ -0,0 +1,106 @@
using System;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Ombi.Helpers;
using Ombi.Store.Context;
using SQLitePCL;
namespace Ombi.Extensions
{
public static class DatabaseExtensions
{
private const string SqliteDatabase = "Sqlite";
private const string MySqlDatabase = "MySQL";
public static void ConfigureDatabases(this IServiceCollection services)
{
var i = StoragePathSingleton.Instance;
if (string.IsNullOrEmpty(i.StoragePath))
{
i.StoragePath = string.Empty;
}
var databaseFileLocation = Path.Combine(i.StoragePath, "database.json");
var configuration = GetDatabaseConfiguration(databaseFileLocation, i.StoragePath);
// Ombi db
switch (configuration.OmbiDatabase.Type)
{
case var type when type.Equals(SqliteDatabase, StringComparison.InvariantCultureIgnoreCase):
services.AddDbContext<OmbiContext, OmbiSqliteContext>(x => ConfigureSqlite(x, configuration.OmbiDatabase));
break;
case var type when type.Equals(MySqlDatabase, StringComparison.InvariantCultureIgnoreCase):
services.AddDbContext<OmbiContext, OmbiMySqlContext>(x => ConfigureMySql(x, configuration.OmbiDatabase));
break;
}
}
private static DatabaseConfiguration GetDatabaseConfiguration(string databaseFileLocation, string storagePath)
{
var configuration = new DatabaseConfiguration(storagePath);
if (File.Exists(databaseFileLocation))
{
var databaseJson = File.ReadAllText(databaseFileLocation);
if (string.IsNullOrEmpty(databaseJson))
{
throw new Exception("Found 'database.json' but it was empty!");
}
configuration = JsonConvert.DeserializeObject<DatabaseConfiguration>(databaseJson);
}
return configuration;
}
private static void ConfigureSqlite(DbContextOptionsBuilder options, PerDatabaseConfiguration config)
{
SQLitePCL.Batteries.Init();
SQLitePCL.raw.sqlite3_config(raw.SQLITE_CONFIG_MULTITHREAD);
options.UseSqlite(config.ConnectionString);
}
private static void ConfigureMySql(DbContextOptionsBuilder options, PerDatabaseConfiguration config)
{
options.UseMySql(config.ConnectionString);
}
private class DatabaseConfiguration
{
public DatabaseConfiguration()
{
}
public DatabaseConfiguration(string defaultSqlitePath)
{
OmbiDatabase = new PerDatabaseConfiguration(SqliteDatabase, $"Data Source={Path.Combine(defaultSqlitePath, "Ombi.db")}");
SettingsDatabase = new PerDatabaseConfiguration(SqliteDatabase, $"Data Source={Path.Combine(defaultSqlitePath, "Settings.db")}");
ExternalDatabase = new PerDatabaseConfiguration(SqliteDatabase, $"Data Source={Path.Combine(defaultSqlitePath, "External.db")}");
}
public PerDatabaseConfiguration OmbiDatabase { get; set; }
public PerDatabaseConfiguration SettingsDatabase { get; set; }
public PerDatabaseConfiguration ExternalDatabase { get; set; }
}
private class PerDatabaseConfiguration
{
public PerDatabaseConfiguration(string type, string connectionString)
{
Type = type;
ConnectionString = connectionString;
}
// Used in Deserialization
public PerDatabaseConfiguration()
{
}
public string Type { get; set; }
public string ConnectionString { get; set; }
}
}
}

@ -19,6 +19,7 @@ using Microsoft.Extensions.Logging;
using Ombi.Core.Authentication;
using Ombi.Core.Settings;
using Ombi.DependencyInjection;
using Ombi.Extensions;
using Ombi.Helpers;
using Ombi.Mapping;
using Ombi.Schedule;
@ -98,7 +99,6 @@ namespace Ombi
x.UseActivator(new IoCJobActivator(services.BuildServiceProvider()));
});
SQLitePCL.raw.sqlite3_config(raw.SQLITE_CONFIG_MULTITHREAD);
services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
{

Loading…
Cancel
Save