forked from umbraco/Umbraco-CMS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented ContentVersionCleanup scheduled task.
Note: adding ref to Microsoft.NET.Test.Sdk fixes AutoFixture AutoDataAttribute (and sub classes)
- Loading branch information
Paul Johnson
committed
Oct 26, 2021
1 parent
a1ac730
commit bba089c
Showing
30 changed files
with
1,416 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...s/IContentVersionCleanupPolicySettings.cs → ...tentVersionCleanupPolicyGlobalSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/Umbraco.Core/Models/ContentVersionCleanupPolicySettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
|
||
namespace Umbraco.Core.Models | ||
{ | ||
public class ContentVersionCleanupPolicySettings | ||
{ | ||
public int ContentTypeId { get; set; } | ||
public int? KeepAllVersionsNewerThanDays { get; set; } | ||
public int? KeepLatestVersionPerDayForDays { get; set; } | ||
public bool PreventCleanup { get; set; } | ||
public DateTime Updated { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
|
||
namespace Umbraco.Core.Models | ||
{ | ||
public class HistoricContentVersionMeta | ||
{ | ||
public int ContentId { get; } | ||
public int ContentTypeId { get; } | ||
public int VersionId { get; } | ||
public DateTime VersionDate { get; } | ||
|
||
public HistoricContentVersionMeta() { } | ||
|
||
public HistoricContentVersionMeta(int contentId, int contentTypeId, int versionId, DateTime versionDate) | ||
{ | ||
ContentId = contentId; | ||
ContentTypeId = contentTypeId; | ||
VersionId = versionId; | ||
VersionDate = versionDate; | ||
} | ||
|
||
public override string ToString() => $"HistoricContentVersionMeta(versionId: {VersionId}, versionDate: {VersionDate:s}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Umbraco.Core/Persistence/Repositories/IDocumentVersionRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Collections.Generic; | ||
using Umbraco.Core.Models; | ||
|
||
namespace Umbraco.Core.Persistence.Repositories | ||
{ | ||
public interface IDocumentVersionRepository : IRepository | ||
{ | ||
/// <summary> | ||
/// Gets a list of all historic content versions. | ||
/// </summary> | ||
public IReadOnlyCollection<HistoricContentVersionMeta> GetDocumentVersionsEligibleForCleanup(); | ||
|
||
/// <summary> | ||
/// Gets cleanup policy override settings per content type. | ||
/// </summary> | ||
public IReadOnlyCollection<ContentVersionCleanupPolicySettings> GetCleanupPolicies(); | ||
|
||
/// <summary> | ||
/// Deletes multiple content versions by ID. | ||
/// </summary> | ||
void DeleteVersions(IEnumerable<int> versionIds); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/Umbraco.Core/Persistence/Repositories/Implement/DocumentVersionRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Umbraco.Core.Models; | ||
using Umbraco.Core.Persistence.Dtos; | ||
using Umbraco.Core.Scoping; | ||
|
||
namespace Umbraco.Core.Persistence.Repositories.Implement | ||
{ | ||
internal class DocumentVersionRepository : IDocumentVersionRepository | ||
{ | ||
private readonly IScopeAccessor _scopeAccessor; | ||
|
||
public DocumentVersionRepository(IScopeAccessor scopeAccessor) | ||
{ | ||
_scopeAccessor = scopeAccessor ?? throw new ArgumentNullException(nameof(scopeAccessor)); | ||
} | ||
|
||
/// <inheritdoc /> | ||
/// <remarks> | ||
/// Never includes current draft version. <br/> | ||
/// Never includes current published version.<br/> | ||
/// Never includes versions marked as "preventCleanup".<br/> | ||
/// </remarks> | ||
public IReadOnlyCollection<HistoricContentVersionMeta> GetDocumentVersionsEligibleForCleanup() | ||
{ | ||
var query = _scopeAccessor.AmbientScope.SqlContext.Sql(); | ||
|
||
query.Select(@"umbracoDocument.nodeId as contentId, | ||
umbracoContent.contentTypeId as contentTypeId, | ||
umbracoContentVersion.id as versionId, | ||
umbracoContentVersion.versionDate as versionDate") | ||
.From<DocumentDto>() | ||
.InnerJoin<ContentDto>() | ||
.On<DocumentDto, ContentDto>(left => left.NodeId, right => right.NodeId) | ||
.InnerJoin<ContentVersionDto>() | ||
.On<ContentDto, ContentVersionDto>(left => left.NodeId, right => right.NodeId) | ||
.InnerJoin<DocumentVersionDto>() | ||
.On<ContentVersionDto, DocumentVersionDto>(left => left.Id, right => right.Id) | ||
.Where<ContentVersionDto>(x => !x.Current) // Never delete current draft version | ||
.Where<ContentVersionDto>(x => !x.PreventCleanup) // Never delete "pinned" versions | ||
.Where<DocumentVersionDto>(x => !x.Published); // Never delete published version | ||
|
||
return _scopeAccessor.AmbientScope.Database.Fetch<HistoricContentVersionMeta>(query); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IReadOnlyCollection<ContentVersionCleanupPolicySettings> GetCleanupPolicies() | ||
{ | ||
var query = _scopeAccessor.AmbientScope.SqlContext.Sql(); | ||
|
||
query.Select<ContentVersionCleanupPolicyDto>() | ||
.From<ContentVersionCleanupPolicyDto>(); | ||
|
||
return _scopeAccessor.AmbientScope.Database.Fetch<ContentVersionCleanupPolicySettings>(query); | ||
} | ||
|
||
/// <inheritdoc /> | ||
/// <remarks> | ||
/// Deletes in batches of <see cref="Constants.Sql.MaxParameterCount"/> | ||
/// </remarks> | ||
public void DeleteVersions(IEnumerable<int> versionIds) | ||
{ | ||
foreach (var group in versionIds.InGroupsOf(Constants.Sql.MaxParameterCount)) | ||
{ | ||
var groupedVersionIds = group.ToList(); | ||
|
||
// Note: We had discussed doing this in a single SQL Command. | ||
// If you can work out how to make that work with SQL CE, let me know! | ||
// Can use test PerformContentVersionCleanup_WithNoKeepPeriods_DeletesEverythingExceptActive to try things out. | ||
|
||
var query = _scopeAccessor.AmbientScope.SqlContext.Sql() | ||
.Delete<PropertyDataDto>() | ||
.WhereIn<PropertyDataDto>(x => x.VersionId, groupedVersionIds); | ||
_scopeAccessor.AmbientScope.Database.Execute(query); | ||
|
||
query = _scopeAccessor.AmbientScope.SqlContext.Sql() | ||
.Delete<ContentVersionCultureVariationDto>() | ||
.WhereIn<ContentVersionCultureVariationDto>(x => x.VersionId, groupedVersionIds); | ||
_scopeAccessor.AmbientScope.Database.Execute(query); | ||
|
||
query = _scopeAccessor.AmbientScope.SqlContext.Sql() | ||
.Delete<DocumentVersionDto>() | ||
.WhereIn<DocumentVersionDto>(x => x.Id, groupedVersionIds); | ||
_scopeAccessor.AmbientScope.Database.Execute(query); | ||
|
||
query = _scopeAccessor.AmbientScope.SqlContext.Sql() | ||
.Delete<ContentVersionDto>() | ||
.WhereIn<ContentVersionDto>(x => x.Id, groupedVersionIds); | ||
_scopeAccessor.AmbientScope.Database.Execute(query); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Umbraco.Core.Models; | ||
|
||
namespace Umbraco.Core.Services | ||
{ | ||
/// <summary> | ||
/// Used to filter historic content versions for cleanup. | ||
/// </summary> | ||
public interface IContentVersionCleanupPolicy | ||
{ | ||
/// <summary> | ||
/// Filters a set of candidates historic content versions for cleanup according to policy settings. | ||
/// </summary> | ||
IEnumerable<HistoricContentVersionMeta> Apply(DateTime asAtDate, IEnumerable<HistoricContentVersionMeta> items); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Umbraco.Core/Services/IContentVersionCleanupService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Umbraco.Core.Models; | ||
|
||
namespace Umbraco.Core.Services | ||
{ | ||
public interface IContentVersionCleanupService | ||
{ | ||
/// <summary> | ||
/// Removes historic content versions according to a policy. | ||
/// </summary> | ||
IReadOnlyCollection<HistoricContentVersionMeta> PerformContentVersionCleanup(DateTime asAtDate); | ||
} | ||
} |
Oops, something went wrong.