diff --git a/src/Marr.Data/DataMapper.cs b/src/Marr.Data/DataMapper.cs index b35b4596f..51423b988 100644 --- a/src/Marr.Data/DataMapper.cs +++ b/src/Marr.Data/DataMapper.cs @@ -894,10 +894,10 @@ private void UnbindEvents() ClosingConnection = null; } - public void BeginTransaction() + public void BeginTransaction(IsolationLevel isolationLevel) { OpenConnection(); - DbTransaction trans = Command.Connection.BeginTransaction(); + DbTransaction trans = Command.Connection.BeginTransaction(isolationLevel); Command.Transaction = trans; } diff --git a/src/Marr.Data/IDataMapper.cs b/src/Marr.Data/IDataMapper.cs index 4a300476f..6d1eca49e 100644 --- a/src/Marr.Data/IDataMapper.cs +++ b/src/Marr.Data/IDataMapper.cs @@ -90,7 +90,7 @@ public interface IDataMapper : IDisposable #region - Connections / Transactions - - void BeginTransaction(); + void BeginTransaction(IsolationLevel isolationLevel); void RollBack(); void Commit(); event EventHandler OpeningConnection; diff --git a/src/Marr.Data/UnitOfWork.cs b/src/Marr.Data/UnitOfWork.cs index 16327427c..7e071c77c 100644 --- a/src/Marr.Data/UnitOfWork.cs +++ b/src/Marr.Data/UnitOfWork.cs @@ -1,4 +1,5 @@ using System; +using System.Data; using System.Runtime.Serialization; namespace Marr.Data @@ -54,12 +55,12 @@ public UnitOfWorkSharedContext SharedContext } } - public void BeginTransaction() + public void BeginTransaction(IsolationLevel isolationLevel) { // Only allow one transaction to begin if (_transactionCount < 1) { - DB.BeginTransaction(); + DB.BeginTransaction(isolationLevel); } _transactionCount++; diff --git a/src/NzbDrone.Core/Backup/BackupService.cs b/src/NzbDrone.Core/Backup/BackupService.cs index 1e29b3bf7..2d1594dc5 100644 --- a/src/NzbDrone.Core/Backup/BackupService.cs +++ b/src/NzbDrone.Core/Backup/BackupService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.IO; using System.Linq; using System.Text.RegularExpressions; @@ -108,7 +109,7 @@ private void BackupDatabase() using (var unitOfWork = new UnitOfWork(() => _maindDb.GetDataMapper())) { - unitOfWork.BeginTransaction(); + unitOfWork.BeginTransaction(IsolationLevel.Serializable); var databaseFile = _appFolderInfo.GetNzbDroneDatabase(); var tempDatabaseFile = Path.Combine(_backupTempFolder, Path.GetFileName(databaseFile)); diff --git a/src/NzbDrone.Core/Datastore/BasicRepository.cs b/src/NzbDrone.Core/Datastore/BasicRepository.cs index ce5287472..4f2390abe 100644 --- a/src/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/src/NzbDrone.Core/Datastore/BasicRepository.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Linq.Expressions; using Marr.Data; @@ -145,7 +146,7 @@ public void InsertMany(IList models) { using (var unitOfWork = new UnitOfWork(() => DataMapper)) { - unitOfWork.BeginTransaction(); + unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted); foreach (var model in models) { @@ -160,7 +161,7 @@ public void UpdateMany(IList models) { using (var unitOfWork = new UnitOfWork(() => DataMapper)) { - unitOfWork.BeginTransaction(); + unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted); foreach (var model in models) { @@ -203,7 +204,7 @@ public void DeleteMany(IEnumerable ids) { using (var unitOfWork = new UnitOfWork(() => DataMapper)) { - unitOfWork.BeginTransaction(); + unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted); foreach (var id in ids) { diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs index d8c011341..4c55762b8 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs @@ -1,4 +1,5 @@ -using System.Reflection; +using System.Diagnostics; +using System.Reflection; using FluentMigrator.Runner; using FluentMigrator.Runner.Initialization; using FluentMigrator.Runner.Processors.SQLite; @@ -21,6 +22,8 @@ public MigrationController(IAnnouncer announcer) public void MigrateToLatest(string connectionString, MigrationType migrationType) { + var sw = Stopwatch.StartNew(); + _announcer.Heading("Migrating " + connectionString); var assembly = Assembly.GetExecutingAssembly(); @@ -39,6 +42,10 @@ public void MigrateToLatest(string connectionString, MigrationType migrationType var processor = factory.Create(connectionString, _announcer, options); var runner = new MigrationRunner(assembly, migrationContext, processor); runner.MigrateUp(true); + + sw.Stop(); + + _announcer.ElapsedTime(sw.Elapsed); } } } diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationDbFactory.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationDbFactory.cs new file mode 100644 index 000000000..b28923d1c --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationDbFactory.cs @@ -0,0 +1,14 @@ +using System.Data.Common; +using System.Data.SQLite; +using FluentMigrator.Runner.Processors; + +namespace NzbDrone.Core.Datastore.Migration.Framework +{ + public class MigrationDbFactory : DbFactoryBase + { + protected override DbProviderFactory CreateFactory() + { + return SQLiteFactory.Instance; + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationLogger.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationLogger.cs index 346d9d15b..acdb0d0b3 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationLogger.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationLogger.cs @@ -37,6 +37,7 @@ public void Sql(string sql) public void ElapsedTime(TimeSpan timeSpan) { + _logger.Debug("Took: {0}", timeSpan); } public void Error(string message) diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneSqliteProcessorFactory.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneSqliteProcessorFactory.cs index 6581bd74e..431c1ff83 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneSqliteProcessorFactory.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneSqliteProcessorFactory.cs @@ -10,7 +10,7 @@ public class NzbDroneSqliteProcessorFactory : SqliteProcessorFactory { public override IMigrationProcessor Create(String connectionString, IAnnouncer announcer, IMigrationProcessorOptions options) { - var factory = new SqliteDbFactory(); + var factory = new MigrationDbFactory(); var connection = factory.CreateConnection(connectionString); var generator = new SqliteGenerator() { compatabilityMode = CompatabilityMode.STRICT }; return new NzbDroneSqliteProcessor(connection, generator, announcer, options, factory); diff --git a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs index 244396565..66cff3142 100644 --- a/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs +++ b/src/NzbDrone.Core/Instrumentation/DatabaseTarget.cs @@ -22,7 +22,8 @@ public class DatabaseTarget : TargetWithLayout, IHandle +