Implement DatabaseConnectionInfo

Co-authored-by: Qstick <qstick@gmail.com>
pull/9457/head
Bogdan 11 months ago
parent 2673d1eee4
commit 229d91fe40

@ -9,8 +9,8 @@ namespace NzbDrone.Core.Datastore
{ {
public interface IConnectionStringFactory public interface IConnectionStringFactory
{ {
string MainDbConnectionString { get; } DatabaseConnectionInfo MainDbConnection { get; }
string LogDbConnectionString { get; } DatabaseConnectionInfo LogDbConnection { get; }
string GetDatabasePath(string connectionString); string GetDatabasePath(string connectionString);
} }
@ -22,15 +22,15 @@ namespace NzbDrone.Core.Datastore
{ {
_configFileProvider = configFileProvider; _configFileProvider = configFileProvider;
MainDbConnectionString = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresMainDb) : MainDbConnection = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresMainDb) :
GetConnectionString(appFolderInfo.GetDatabase()); GetConnectionString(appFolderInfo.GetDatabase());
LogDbConnectionString = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresLogDb) : LogDbConnection = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresLogDb) :
GetConnectionString(appFolderInfo.GetLogDatabase()); GetConnectionString(appFolderInfo.GetLogDatabase());
} }
public string MainDbConnectionString { get; private set; } public DatabaseConnectionInfo MainDbConnection { get; private set; }
public string LogDbConnectionString { get; private set; } public DatabaseConnectionInfo LogDbConnection { get; private set; }
public string GetDatabasePath(string connectionString) public string GetDatabasePath(string connectionString)
{ {
@ -39,7 +39,7 @@ namespace NzbDrone.Core.Datastore
return connectionBuilder.DataSource; return connectionBuilder.DataSource;
} }
private static string GetConnectionString(string dbPath) private static DatabaseConnectionInfo GetConnectionString(string dbPath)
{ {
var connectionBuilder = new SQLiteConnectionStringBuilder var connectionBuilder = new SQLiteConnectionStringBuilder
{ {
@ -57,21 +57,22 @@ namespace NzbDrone.Core.Datastore
connectionBuilder.Add("Full FSync", true); connectionBuilder.Add("Full FSync", true);
} }
return connectionBuilder.ConnectionString; return new DatabaseConnectionInfo(DatabaseType.SQLite, connectionBuilder.ConnectionString);
} }
private string GetPostgresConnectionString(string dbName) private DatabaseConnectionInfo GetPostgresConnectionString(string dbName)
{ {
var connectionBuilder = new NpgsqlConnectionStringBuilder(); var connectionBuilder = new NpgsqlConnectionStringBuilder
{
connectionBuilder.Database = dbName; Database = dbName,
connectionBuilder.Host = _configFileProvider.PostgresHost; Host = _configFileProvider.PostgresHost,
connectionBuilder.Username = _configFileProvider.PostgresUser; Username = _configFileProvider.PostgresUser,
connectionBuilder.Password = _configFileProvider.PostgresPassword; Password = _configFileProvider.PostgresPassword,
connectionBuilder.Port = _configFileProvider.PostgresPort; Port = _configFileProvider.PostgresPort,
connectionBuilder.Enlist = false; Enlist = false
};
return connectionBuilder.ConnectionString; return new DatabaseConnectionInfo(DatabaseType.PostgreSQL, connectionBuilder.ConnectionString);
} }
} }
} }

@ -0,0 +1,14 @@
namespace NzbDrone.Core.Datastore
{
public class DatabaseConnectionInfo
{
public DatabaseConnectionInfo(DatabaseType databaseType, string connectionString)
{
DatabaseType = databaseType;
ConnectionString = connectionString;
}
public DatabaseType DatabaseType { get; internal set; }
public string ConnectionString { get; internal set; }
}
}

@ -2,6 +2,7 @@ using System;
using System.Data.Common; using System.Data.Common;
using System.Data.SQLite; using System.Data.SQLite;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading;
using NLog; using NLog;
using Npgsql; using Npgsql;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
@ -60,22 +61,22 @@ namespace NzbDrone.Core.Datastore
public IDatabase Create(MigrationContext migrationContext) public IDatabase Create(MigrationContext migrationContext)
{ {
string connectionString; DatabaseConnectionInfo connectionInfo;
switch (migrationContext.MigrationType) switch (migrationContext.MigrationType)
{ {
case MigrationType.Main: case MigrationType.Main:
{ {
connectionString = _connectionStringFactory.MainDbConnectionString; connectionInfo = _connectionStringFactory.MainDbConnection;
CreateMain(connectionString, migrationContext); CreateMain(connectionInfo.ConnectionString, migrationContext);
break; break;
} }
case MigrationType.Log: case MigrationType.Log:
{ {
connectionString = _connectionStringFactory.LogDbConnectionString; connectionInfo = _connectionStringFactory.LogDbConnection;
CreateLog(connectionString, migrationContext); CreateLog(connectionInfo.ConnectionString, migrationContext);
break; break;
} }
@ -90,14 +91,14 @@ namespace NzbDrone.Core.Datastore
{ {
DbConnection conn; DbConnection conn;
if (connectionString.Contains(".db")) if (connectionInfo.DatabaseType == DatabaseType.SQLite)
{ {
conn = SQLiteFactory.Instance.CreateConnection(); conn = SQLiteFactory.Instance.CreateConnection();
conn.ConnectionString = connectionString; conn.ConnectionString = connectionInfo.ConnectionString;
} }
else else
{ {
conn = new NpgsqlConnection(connectionString); conn = new NpgsqlConnection(connectionInfo.ConnectionString);
} }
conn.Open(); conn.Open();
@ -135,15 +136,17 @@ namespace NzbDrone.Core.Datastore
{ {
Logger.Error(e, "Failure to connect to Postgres DB, {0} retries remaining", retryCount); Logger.Error(e, "Failure to connect to Postgres DB, {0} retries remaining", retryCount);
Thread.Sleep(5000);
try try
{ {
_migrationController.Migrate(connectionString, migrationContext); _migrationController.Migrate(connectionString, migrationContext);
return;
} }
catch (Exception ex) catch (Exception ex)
{ {
if (--retryCount > 0) if (--retryCount > 0)
{ {
System.Threading.Thread.Sleep(5000);
continue; continue;
} }

@ -88,16 +88,15 @@ namespace NzbDrone.Core.Instrumentation
log.Level = logEvent.Level.Name; log.Level = logEvent.Level.Name;
var connectionString = _connectionStringFactory.LogDbConnectionString; var connectionInfo = _connectionStringFactory.LogDbConnection;
// TODO: Probably need more robust way to differentiate what's being used if (connectionInfo.DatabaseType == DatabaseType.SQLite)
if (connectionString.Contains(".db"))
{ {
WriteSqliteLog(log, connectionString); WriteSqliteLog(log, connectionInfo.ConnectionString);
} }
else else
{ {
WritePostgresLog(log, connectionString); WritePostgresLog(log, connectionInfo.ConnectionString);
} }
} }
catch (NpgsqlException ex) catch (NpgsqlException ex)
@ -136,8 +135,10 @@ namespace NzbDrone.Core.Instrumentation
private void WriteSqliteLog(Log log, string connectionString) private void WriteSqliteLog(Log log, string connectionString)
{ {
using (var connection = using (var connection =
new SQLiteConnection(connectionString).OpenAndReturn()) SQLiteFactory.Instance.CreateConnection())
{ {
connection.ConnectionString = connectionString;
connection.Open();
using (var sqlCommand = connection.CreateCommand()) using (var sqlCommand = connection.CreateCommand())
{ {
sqlCommand.CommandText = INSERT_COMMAND; sqlCommand.CommandText = INSERT_COMMAND;

Loading…
Cancel
Save