diff --git a/src/NzbDrone.Api/System/SystemModule.cs b/src/NzbDrone.Api/System/SystemModule.cs index be22c390e..5acd4d881 100644 --- a/src/NzbDrone.Api/System/SystemModule.cs +++ b/src/NzbDrone.Api/System/SystemModule.cs @@ -4,6 +4,7 @@ using NzbDrone.Api.Extensions; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Core.Configuration; +using NzbDrone.Core.Datastore; namespace NzbDrone.Api.System { @@ -13,14 +14,16 @@ public class SystemModule : NzbDroneApiModule private readonly IRuntimeInfo _runtimeInfo; private readonly IRouteCacheProvider _routeCacheProvider; private readonly IConfigFileProvider _configFileProvider; + private readonly IDatabase _database; - public SystemModule(IAppFolderInfo appFolderInfo, IRuntimeInfo runtimeInfo, IRouteCacheProvider routeCacheProvider, IConfigFileProvider configFileProvider) + public SystemModule(IAppFolderInfo appFolderInfo, IRuntimeInfo runtimeInfo, IRouteCacheProvider routeCacheProvider, IConfigFileProvider configFileProvider, IDatabase database) : base("system") { _appFolderInfo = appFolderInfo; _runtimeInfo = runtimeInfo; _routeCacheProvider = routeCacheProvider; _configFileProvider = configFileProvider; + _database = database; Get["/status"] = x => GetStatus(); Get["/routes"] = x => GetRoutes(); } @@ -44,6 +47,7 @@ private Response GetStatus() Branch = _configFileProvider.Branch, Authentication = _configFileProvider.AuthenticationEnabled, StartOfWeek = (int)OsInfo.FirstDayOfWeek, + SqliteVersion = _database.Version, UrlBase = _configFileProvider.UrlBase }.AsResponse(); diff --git a/src/NzbDrone.Core.Test/Datastore/DatabaseFixture.cs b/src/NzbDrone.Core.Test/Datastore/DatabaseFixture.cs new file mode 100644 index 000000000..e1942d6c3 --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/DatabaseFixture.cs @@ -0,0 +1,36 @@ +using System; +using System.Linq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.Test.Datastore +{ + public class DatabaseFixture : DbTest + { + [Test] + public void SingleOrDefault_should_return_null_on_empty_db() + { + Mocker.Resolve() + .GetDataMapper().Query() + .SingleOrDefault(c => c.CleanTitle == "SomeTitle") + .Should() + .BeNull(); + } + + + [Test] + public void vaccume() + { + Mocker.Resolve().Vacuum(); + } + + [Test] + public void get_version() + { + Mocker.Resolve().Version.Should().BeGreaterThan(new Version("3.0.0")); + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs b/src/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs index 30f412d1b..84037c050 100644 --- a/src/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs +++ b/src/NzbDrone.Core.Test/Datastore/ObjectDatabaseFixture.cs @@ -7,24 +7,9 @@ using NzbDrone.Core.Datastore; using NzbDrone.Core.Jobs; using NzbDrone.Core.Test.Framework; -using NzbDrone.Core.Tv; namespace NzbDrone.Core.Test.Datastore { - - public class DatabaseFixture : DbTest - { - [Test] - public void SingleOrDefault_should_return_null_on_empty_db() - { - Mocker.Resolve() - .GetDataMapper().Query() - .SingleOrDefault(c => c.CleanTitle == "SomeTitle") - .Should() - .BeNull(); - } - } - [TestFixture] public class ObjectDatabaseFixture : DbTest, ScheduledTask> { diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index fe5a842f8..2da4a6aae 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -106,6 +106,7 @@ + diff --git a/src/NzbDrone.Core/Datastore/BasicRepository.cs b/src/NzbDrone.Core/Datastore/BasicRepository.cs index 68e44343e..bb1173b3b 100644 --- a/src/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/src/NzbDrone.Core/Datastore/BasicRepository.cs @@ -27,7 +27,7 @@ namespace NzbDrone.Core.Datastore void InsertMany(IList model); void UpdateMany(IList model); void DeleteMany(List model); - void Purge(); + void Purge(bool vacuum = false); bool HasItems(); void DeleteMany(IEnumerable ids); void SetFields(TModel model, params Expression>[] properties); @@ -216,9 +216,18 @@ public void DeleteMany(IEnumerable ids) } } - public void Purge() + public void Purge(bool vacuum = false) { DataMapper.Delete(c => c.Id > -1); + if (vacuum) + { + Vacuum(); + } + } + + protected void Vacuum() + { + _database.Vacuum(); } public bool HasItems() @@ -258,11 +267,6 @@ protected virtual SortBuilder GetPagedQuery(QueryBuilder query, .Take(pagingSpec.PageSize); } - public void DeleteAll() - { - DataMapper.Delete(c => c.Id > 0); - } - protected void ModelCreated(TModel model) { PublishModelEvent(model, ModelAction.Created); diff --git a/src/NzbDrone.Core/Datastore/Database.cs b/src/NzbDrone.Core/Datastore/Database.cs index 0890699fc..d7e674eb8 100644 --- a/src/NzbDrone.Core/Datastore/Database.cs +++ b/src/NzbDrone.Core/Datastore/Database.cs @@ -1,25 +1,58 @@ using System; +using System.Data.SQLite; using Marr.Data; +using NLog; +using NzbDrone.Common.Instrumentation; namespace NzbDrone.Core.Datastore { public interface IDatabase { IDataMapper GetDataMapper(); + Version Version { get; } + void Vacuum(); } public class Database : IDatabase { - private readonly Func _dataMapperFactory; + private readonly string _connectionString; - public Database(Func dataMapperFactory) + private Logger logger = NzbDroneLogger.GetLogger(); + + public Database(string connectionString) { - _dataMapperFactory = dataMapperFactory; + _connectionString = connectionString; } public IDataMapper GetDataMapper() { - return _dataMapperFactory(); + return new DataMapper(SQLiteFactory.Instance, _connectionString) + { + SqlMode = SqlModes.Text, + }; + } + + public Version Version + { + get + { + var version = GetDataMapper().ExecuteScalar("SELECT sqlite_version()").ToString(); + return new Version(version); + } + } + + public void Vacuum() + { + try + { + logger.Info("Vacuuming database " + _connectionString); + GetDataMapper().ExecuteNonQuery("Vacuum;"); + logger.Info("Database Compressed " + _connectionString); + } + catch (Exception e) + { + logger.Error("An Error occurred while vacuuming database. " + _connectionString, e); + } } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Datastore/DbFactory.cs b/src/NzbDrone.Core/Datastore/DbFactory.cs index 21f10784e..0faf313f2 100644 --- a/src/NzbDrone.Core/Datastore/DbFactory.cs +++ b/src/NzbDrone.Core/Datastore/DbFactory.cs @@ -73,15 +73,11 @@ public IDatabase Create(MigrationType migrationType = MigrationType.Main) _migrationController.MigrateToLatest(connectionString, migrationType); - return new Database(() => - { - var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString) - { - SqlMode = SqlModes.Text, - }; + var db = new Database(connectionString); - return dataMapper; - }); + db.Vacuum(); + + return db; } } }