diff --git a/src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs b/src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs index ff97ec020..19c938737 100644 --- a/src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs +++ b/src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs @@ -9,8 +9,8 @@ namespace NzbDrone.Core.Datastore { public interface IConnectionStringFactory { - string MainDbConnectionString { get; } - string LogDbConnectionString { get; } + DatabaseConnectionInfo MainDbConnection { get; } + DatabaseConnectionInfo LogDbConnection { get; } string GetDatabasePath(string connectionString); } @@ -22,15 +22,15 @@ namespace NzbDrone.Core.Datastore { _configFileProvider = configFileProvider; - MainDbConnectionString = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresMainDb) : + MainDbConnection = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresMainDb) : GetConnectionString(appFolderInfo.GetDatabase()); - LogDbConnectionString = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresLogDb) : + LogDbConnection = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresLogDb) : GetConnectionString(appFolderInfo.GetLogDatabase()); } - public string MainDbConnectionString { get; private set; } - public string LogDbConnectionString { get; private set; } + public DatabaseConnectionInfo MainDbConnection { get; private set; } + public DatabaseConnectionInfo LogDbConnection { get; private set; } public string GetDatabasePath(string connectionString) { @@ -39,7 +39,7 @@ namespace NzbDrone.Core.Datastore return connectionBuilder.DataSource; } - private static string GetConnectionString(string dbPath) + private static DatabaseConnectionInfo GetConnectionString(string dbPath) { var connectionBuilder = new SQLiteConnectionStringBuilder { @@ -57,21 +57,22 @@ namespace NzbDrone.Core.Datastore 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(); - - connectionBuilder.Database = dbName; - connectionBuilder.Host = _configFileProvider.PostgresHost; - connectionBuilder.Username = _configFileProvider.PostgresUser; - connectionBuilder.Password = _configFileProvider.PostgresPassword; - connectionBuilder.Port = _configFileProvider.PostgresPort; - connectionBuilder.Enlist = false; + var connectionBuilder = new NpgsqlConnectionStringBuilder + { + Database = dbName, + Host = _configFileProvider.PostgresHost, + Username = _configFileProvider.PostgresUser, + Password = _configFileProvider.PostgresPassword, + Port = _configFileProvider.PostgresPort, + Enlist = false + }; - return connectionBuilder.ConnectionString; + return new DatabaseConnectionInfo(DatabaseType.PostgreSQL, connectionBuilder.ConnectionString); } } } diff --git a/src/NzbDrone.Core/Datastore/DatabaseConnectionInfo.cs b/src/NzbDrone.Core/Datastore/DatabaseConnectionInfo.cs new file mode 100644 index 000000000..5b53f086f --- /dev/null +++ b/src/NzbDrone.Core/Datastore/DatabaseConnectionInfo.cs @@ -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; } + } +} diff --git a/src/NzbDrone.Core/Datastore/DbFactory.cs b/src/NzbDrone.Core/Datastore/DbFactory.cs index 0367b4973..608b8fd22 100644 --- a/src/NzbDrone.Core/Datastore/DbFactory.cs +++ b/src/NzbDrone.Core/Datastore/DbFactory.cs @@ -2,6 +2,7 @@ using System; using System.Data.Common; using System.Data.SQLite; using System.Net.Sockets; +using System.Threading; using NLog; using Npgsql; using NzbDrone.Common.Disk; @@ -60,22 +61,22 @@ namespace NzbDrone.Core.Datastore public IDatabase Create(MigrationContext migrationContext) { - string connectionString; + DatabaseConnectionInfo connectionInfo; switch (migrationContext.MigrationType) { case MigrationType.Main: { - connectionString = _connectionStringFactory.MainDbConnectionString; - CreateMain(connectionString, migrationContext); + connectionInfo = _connectionStringFactory.MainDbConnection; + CreateMain(connectionInfo.ConnectionString, migrationContext); break; } case MigrationType.Log: { - connectionString = _connectionStringFactory.LogDbConnectionString; - CreateLog(connectionString, migrationContext); + connectionInfo = _connectionStringFactory.LogDbConnection; + CreateLog(connectionInfo.ConnectionString, migrationContext); break; } @@ -90,14 +91,14 @@ namespace NzbDrone.Core.Datastore { DbConnection conn; - if (connectionString.Contains(".db")) + if (connectionInfo.DatabaseType == DatabaseType.SQLite) { conn = SQLiteFactory.Instance.CreateConnection(); - conn.ConnectionString = connectionString; + conn.ConnectionString = connectionInfo.ConnectionString; } else { - conn = new NpgsqlConnection(connectionString); + conn = new NpgsqlConnection(connectionInfo.ConnectionString); } conn.Open(); @@ -135,15 +136,17 @@ namespace NzbDrone.Core.Datastore { Logger.Error(e, "Failure to connect to Postgres DB, {0} retries remaining", retryCount); + Thread.Sleep(5000); + try { _migrationController.Migrate(connectionString, migrationContext); + return; } catch (Exception ex) { if (--retryCount > 0) { - System.Threading.Thread.Sleep(5000); continue; } diff --git a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs index 16b025fa7..896b274a6 100644 --- a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs +++ b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs @@ -88,16 +88,15 @@ namespace NzbDrone.Core.Instrumentation 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 (connectionString.Contains(".db")) + if (connectionInfo.DatabaseType == DatabaseType.SQLite) { - WriteSqliteLog(log, connectionString); + WriteSqliteLog(log, connectionInfo.ConnectionString); } else { - WritePostgresLog(log, connectionString); + WritePostgresLog(log, connectionInfo.ConnectionString); } } catch (NpgsqlException ex) @@ -136,8 +135,10 @@ namespace NzbDrone.Core.Instrumentation private void WriteSqliteLog(Log log, string connectionString) { using (var connection = - new SQLiteConnection(connectionString).OpenAndReturn()) + SQLiteFactory.Instance.CreateConnection()) { + connection.ConnectionString = connectionString; + connection.Open(); using (var sqlCommand = connection.CreateCommand()) { sqlCommand.CommandText = INSERT_COMMAND;