|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
using System.IO;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Ombi.Helpers;
|
|
|
|
|
using Ombi.Store.Context;
|
|
|
|
@ -17,7 +18,7 @@ namespace Ombi.Extensions
|
|
|
|
|
public const string SqliteDatabase = "Sqlite";
|
|
|
|
|
public const string MySqlDatabase = "MySQL";
|
|
|
|
|
|
|
|
|
|
public static void ConfigureDatabases(this IServiceCollection services)
|
|
|
|
|
public static void ConfigureDatabases(this IServiceCollection services, IHealthChecksBuilder hcBuilder)
|
|
|
|
|
{
|
|
|
|
|
var configuration = GetDatabaseConfiguration();
|
|
|
|
|
|
|
|
|
@ -26,9 +27,11 @@ namespace Ombi.Extensions
|
|
|
|
|
{
|
|
|
|
|
case var type when type.Equals(SqliteDatabase, StringComparison.InvariantCultureIgnoreCase):
|
|
|
|
|
services.AddDbContext<OmbiContext, OmbiSqliteContext>(x => ConfigureSqlite(x, configuration.OmbiDatabase));
|
|
|
|
|
AddSqliteHealthCheck(hcBuilder, "Ombi Database", configuration.OmbiDatabase);
|
|
|
|
|
break;
|
|
|
|
|
case var type when type.Equals(MySqlDatabase, StringComparison.InvariantCultureIgnoreCase):
|
|
|
|
|
services.AddDbContext<OmbiContext, OmbiMySqlContext>(x => ConfigureMySql(x, configuration.OmbiDatabase));
|
|
|
|
|
AddMySqlHealthCheck(hcBuilder, "Ombi Database", configuration.OmbiDatabase);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -36,9 +39,11 @@ namespace Ombi.Extensions
|
|
|
|
|
{
|
|
|
|
|
case var type when type.Equals(SqliteDatabase, StringComparison.InvariantCultureIgnoreCase):
|
|
|
|
|
services.AddDbContext<ExternalContext, ExternalSqliteContext>(x => ConfigureSqlite(x, configuration.ExternalDatabase));
|
|
|
|
|
AddSqliteHealthCheck(hcBuilder, "External Database", configuration.ExternalDatabase);
|
|
|
|
|
break;
|
|
|
|
|
case var type when type.Equals(MySqlDatabase, StringComparison.InvariantCultureIgnoreCase):
|
|
|
|
|
services.AddDbContext<ExternalContext, ExternalMySqlContext>(x => ConfigureMySql(x, configuration.ExternalDatabase));
|
|
|
|
|
AddMySqlHealthCheck(hcBuilder, "External Database", configuration.ExternalDatabase);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -46,9 +51,11 @@ namespace Ombi.Extensions
|
|
|
|
|
{
|
|
|
|
|
case var type when type.Equals(SqliteDatabase, StringComparison.InvariantCultureIgnoreCase):
|
|
|
|
|
services.AddDbContext<SettingsContext, SettingsSqliteContext>(x => ConfigureSqlite(x, configuration.SettingsDatabase));
|
|
|
|
|
AddSqliteHealthCheck(hcBuilder, "Settings Database", configuration.SettingsDatabase);
|
|
|
|
|
break;
|
|
|
|
|
case var type when type.Equals(MySqlDatabase, StringComparison.InvariantCultureIgnoreCase):
|
|
|
|
|
services.AddDbContext<SettingsContext, SettingsMySqlContext>(x => ConfigureMySql(x, configuration.SettingsDatabase));
|
|
|
|
|
AddMySqlHealthCheck(hcBuilder, "Settings Database", configuration.SettingsDatabase);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -62,7 +69,7 @@ namespace Ombi.Extensions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var databaseFileLocation = Path.Combine(i.StoragePath, "database.json");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var configuration = new DatabaseConfiguration(i.StoragePath);
|
|
|
|
|
if (File.Exists(databaseFileLocation))
|
|
|
|
|
{
|
|
|
|
@ -78,11 +85,36 @@ namespace Ombi.Extensions
|
|
|
|
|
return configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void AddSqliteHealthCheck(IHealthChecksBuilder builder, string dbName, PerDatabaseConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
if (builder != null)
|
|
|
|
|
{
|
|
|
|
|
builder.AddSqlite(
|
|
|
|
|
sqliteConnectionString: config.ConnectionString,
|
|
|
|
|
name: dbName,
|
|
|
|
|
failureStatus: HealthStatus.Unhealthy,
|
|
|
|
|
tags: new string[] { "db" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AddMySqlHealthCheck(IHealthChecksBuilder builder, string dbName, PerDatabaseConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
if (builder != null)
|
|
|
|
|
{
|
|
|
|
|
builder.AddMySql(
|
|
|
|
|
connectionString: config.ConnectionString,
|
|
|
|
|
name: dbName,
|
|
|
|
|
failureStatus: HealthStatus.Unhealthy,
|
|
|
|
|
tags: new string[] { "db" }
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ConfigureSqlite(DbContextOptionsBuilder options, PerDatabaseConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
SQLitePCL.Batteries.Init();
|
|
|
|
|
SQLitePCL.raw.sqlite3_config(raw.SQLITE_CONFIG_MULTITHREAD);
|
|
|
|
|
|
|
|
|
|
options.UseSqlite(config.ConnectionString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|