diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index 36e0d53ae..c5a4f887a 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -56,7 +56,7 @@ namespace NzbDrone.Core LogConfiguration.Setup(); - Migrations.Run(); + Migrations.Run(Connection.MainConnectionString); ForceMigration(_kernel.Get()); SetupDefaultQualityProfiles(_kernel.Get()); //Setup the default QualityProfiles on start-up @@ -97,9 +97,9 @@ namespace NzbDrone.Core _kernel.Bind().ToSelf().InSingletonScope(); _kernel.Bind().ToSelf().InSingletonScope(); - _kernel.Bind().ToConstant(Connection.MainDataRepository).InSingletonScope(); - _kernel.Bind().ToConstant(Connection.LogDataRepository).WhenInjectedInto().InSingletonScope(); - _kernel.Bind().ToConstant(Connection.LogDataRepository).WhenInjectedInto().InSingletonScope(); + _kernel.Bind().ToConstant(Connection.CreateSimpleRepository(Connection.MainConnectionString)).InSingletonScope(); + _kernel.Bind().ToConstant(Connection.CreateSimpleRepository(Connection.LogConnectionString)).WhenInjectedInto().InSingletonScope(); + _kernel.Bind().ToConstant(Connection.CreateSimpleRepository(Connection.LogConnectionString)).WhenInjectedInto().InSingletonScope(); } } diff --git a/NzbDrone.Core/Datastore/Connection.cs b/NzbDrone.Core/Datastore/Connection.cs index e8e7b6a10..40a5f56cc 100644 --- a/NzbDrone.Core/Datastore/Connection.cs +++ b/NzbDrone.Core/Datastore/Connection.cs @@ -18,11 +18,16 @@ namespace NzbDrone.Core.Datastore } + public static string GetConnectionString(string path) + { + return String.Format("Data Source={0};Version=3;", path); + } + public static String MainConnectionString { get { - return String.Format("Data Source={0};Version=3;", Path.Combine(AppDataPath.FullName, "nzbdrone.db")); + return GetConnectionString(Path.Combine(AppDataPath.FullName, "nzbdrone.db")); } } @@ -30,70 +35,19 @@ namespace NzbDrone.Core.Datastore { get { - return String.Format("Data Source={0};Version=3;", Path.Combine(AppDataPath.FullName, "log.db")); + return GetConnectionString(Path.Combine(AppDataPath.FullName, "log.db")); } } - - private static IDataProvider _mainDataProvider; - public static IDataProvider MainDataProvider + public static IDataProvider GetDataProvider(string connectionString) { - get - { - if (_mainDataProvider == null) - { - _mainDataProvider = ProviderFactory.GetProvider(Connection.MainConnectionString, "System.Data.SQLite"); - } - return _mainDataProvider; - } - + return ProviderFactory.GetProvider(connectionString, "System.Data.SQLite"); } - private static IDataProvider _logDataProvider; - public static IDataProvider LogDataProvider + public static IRepository CreateSimpleRepository(string connectionString) { - get - { - if (_logDataProvider == null) - { - _logDataProvider = ProviderFactory.GetProvider(Connection.LogConnectionString, "System.Data.SQLite"); - } - return _logDataProvider; - } - + return new SimpleRepository(GetDataProvider(connectionString), SimpleRepositoryOptions.RunMigrations); } - - private static SimpleRepository _mainDataRepository; - public static SimpleRepository MainDataRepository - { - get - { - if (_mainDataRepository == null) - { - _mainDataRepository = new SimpleRepository(MainDataProvider, SimpleRepositoryOptions.RunMigrations); - } - - return _mainDataRepository; - } - - } - - private static SimpleRepository _logDataRepository; - public static SimpleRepository LogDataRepository - { - get - { - if (_logDataRepository == null) - { - _logDataRepository = new SimpleRepository(LogDataProvider, SimpleRepositoryOptions.RunMigrations); - } - return _logDataRepository; - } - - } - - - } } diff --git a/NzbDrone.Core/Datastore/Migrations.cs b/NzbDrone.Core/Datastore/Migrations.cs index 4ce5a02be..ae677b957 100644 --- a/NzbDrone.Core/Datastore/Migrations.cs +++ b/NzbDrone.Core/Datastore/Migrations.cs @@ -16,16 +16,16 @@ namespace NzbDrone.Core.Datastore { private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - public static void Run() + public static void Run(string connetionString) { - Logger.Info("Preparing to migrate databse"); + Logger.Info("Preparing run database migration"); try { - var mig = new Migrator.Migrator("Sqlite", Connection.MainConnectionString, + var migrator = new Migrator.Migrator("Sqlite", connetionString, Assembly.GetAssembly(typeof(Migrations)), true, new MigrationLogger()); - mig.MigrateToLastVersion(); + migrator.MigrateToLastVersion(); Logger.Info("Database migration completed"); } @@ -111,10 +111,7 @@ namespace NzbDrone.Core.Datastore { public override void Up() { - if(Database.TableExists("Seasons")) - { - Database.RemoveTable("Seasons"); - } + Database.RemoveTable("Seasons"); Migrations.RemoveDeletedColumns(Database); Migrations.AddNewColumns(Database); diff --git a/NzbDrone.Core/Datastore/RepositoryProvider.cs b/NzbDrone.Core/Datastore/RepositoryProvider.cs index 7861b9526..11006811d 100644 --- a/NzbDrone.Core/Datastore/RepositoryProvider.cs +++ b/NzbDrone.Core/Datastore/RepositoryProvider.cs @@ -26,7 +26,7 @@ namespace NzbDrone.Core.Datastore public virtual ITable GetSchemaFromType(Type type) { - return type.ToSchemaTable(Connection.MainDataProvider); + return type.ToSchemaTable(Connection.GetDataProvider(Connection.MainConnectionString)); } public virtual Column[] GetColumnsFromDatabase(ITransformationProvider database, string tableName)