add clean db task

This commit is contained in:
Luke Pulverenti 2015-08-26 01:10:04 -04:00
parent 71c6bc74cd
commit 3e1bbcdcea
4 changed files with 113 additions and 2 deletions

View File

@ -96,6 +96,8 @@ namespace MediaBrowser.Controller.Entities
internal List<Guid> ItemIdsFromPersonFilters { get; set; }
public int? MaxParentalRating { get; set; }
public bool? IsCurrentSchema { get; set; }
public InternalItemsQuery()
{
Tags = new string[] { };

View File

@ -241,6 +241,7 @@
<Compile Include="Logging\PatternsLogger.cs" />
<Compile Include="MediaEncoder\EncodingManager.cs" />
<Compile Include="Persistence\BaseSqliteRepository.cs" />
<Compile Include="Persistence\CleanDatabaseScheduledTask.cs" />
<Compile Include="Social\SharingManager.cs" />
<Compile Include="Social\SharingRepository.cs" />
<Compile Include="Sorting\StartDateComparer.cs" />

View File

@ -0,0 +1,89 @@
using MediaBrowser.Common.Progress;
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Logging;
namespace MediaBrowser.Server.Implementations.Persistence
{
class CleanDatabaseScheduledTask : IScheduledTask
{
private readonly ILibraryManager _libraryManager;
private readonly IItemRepository _itemRepo;
private readonly ILogger _logger;
public CleanDatabaseScheduledTask(ILibraryManager libraryManager, IItemRepository itemRepo, ILogger logger)
{
_libraryManager = libraryManager;
_itemRepo = itemRepo;
_logger = logger;
}
public string Name
{
get { return "Clean Database"; }
}
public string Description
{
get { return "Deletes obsolete content from the database."; }
}
public string Category
{
get { return "Library"; }
}
public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
{
var innerProgress = new ActionableProgress<double>();
innerProgress.RegisterAction(progress.Report);
await UpdateToLatestSchema(cancellationToken, innerProgress).ConfigureAwait(false);
}
private async Task UpdateToLatestSchema(CancellationToken cancellationToken, IProgress<double> progress)
{
var itemIds = _libraryManager.GetItemIds(new InternalItemsQuery
{
IsCurrentSchema = false,
Limit = 50000
});
var numComplete = 0;
var numItems = itemIds.Count;
_logger.Debug("Upgrading schema for {0} items", numItems);
foreach (var itemId in itemIds)
{
var item = _libraryManager.GetItemById(itemId);
if (item != null)
{
await _itemRepo.SaveItem(item, cancellationToken).ConfigureAwait(false);
}
numComplete++;
double percent = numComplete;
percent /= numItems;
progress.Report(percent * 100);
}
progress.Report(100);
}
public IEnumerable<ITaskTrigger> GetDefaultTriggers()
{
return new ITaskTrigger[]
{
new IntervalTrigger{ Interval = TimeSpan.FromDays(1)}
};
}
}
}

View File

@ -71,6 +71,9 @@ namespace MediaBrowser.Server.Implementations.Persistence
private IDbCommand _deletePeopleCommand;
private IDbCommand _savePersonCommand;
private const int LatestSchemaVersion = 2;
/// <summary>
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.
/// </summary>
@ -159,6 +162,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
_connection.AddColumn(_logger, "TypedBaseItems", "ParentId", "GUID");
_connection.AddColumn(_logger, "TypedBaseItems", "Genres", "Text");
_connection.AddColumn(_logger, "TypedBaseItems", "ParentalRatingValue", "INT");
_connection.AddColumn(_logger, "TypedBaseItems", "SchemaVersion", "INT");
PrepareStatements();
@ -201,10 +205,11 @@ namespace MediaBrowser.Server.Implementations.Persistence
"ProductionYear",
"ParentId",
"Genres",
"ParentalRatingValue"
"ParentalRatingValue",
"SchemaVersion"
};
_saveItemCommand = _connection.CreateCommand();
_saveItemCommand.CommandText = "replace into TypedBaseItems (" + string.Join(",", saveColumns.ToArray()) + ") values (@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19, @20, @21, @22, @23, @24)";
_saveItemCommand.CommandText = "replace into TypedBaseItems (" + string.Join(",", saveColumns.ToArray()) + ") values (@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19, @20, @21, @22, @23, @24, @25)";
for (var i = 1; i <= saveColumns.Count; i++)
{
_saveItemCommand.Parameters.Add(_saveItemCommand, "@" + i.ToString(CultureInfo.InvariantCulture));
@ -350,6 +355,8 @@ namespace MediaBrowser.Server.Implementations.Persistence
_saveItemCommand.GetParameter(index++).Value = string.Join("|", item.Genres.ToArray());
_saveItemCommand.GetParameter(index++).Value = item.GetParentalRatingValue();
_saveItemCommand.GetParameter(index++).Value = LatestSchemaVersion;
_saveItemCommand.Transaction = transaction;
_saveItemCommand.ExecuteNonQuery();
@ -883,6 +890,18 @@ namespace MediaBrowser.Server.Implementations.Persistence
{
var whereClauses = new List<string>();
if (query.IsCurrentSchema.HasValue)
{
if (query.IsCurrentSchema.Value)
{
whereClauses.Add("(SchemaVersion not null AND SchemaVersion=@SchemaVersion)");
}
else
{
whereClauses.Add("(SchemaVersion is null or SchemaVersion<>@SchemaVersion)");
}
cmd.Parameters.Add(cmd, "@SchemaVersion", DbType.Int32).Value = LatestSchemaVersion;
}
if (query.IsMovie.HasValue)
{
whereClauses.Add("IsMovie=@IsMovie");