fix broken migration abstraction, fixes restart after upgrade so the runtimestate is correct, fixes sql alter column migration to actually run if its nullable.

This commit is contained in:
Shannon
2021-06-24 13:35:57 -06:00
parent d6a6016801
commit 3d5d04550a
95 changed files with 271 additions and 263 deletions

View File

@@ -1,10 +1,7 @@
using System.Configuration;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Infrastructure.PublishedCache.Persistence;
namespace Umbraco.Cms.Infrastructure.PublishedCache
{
@@ -13,55 +10,25 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
/// </summary>
public class NuCacheStartupHandler : INotificationHandler<UmbracoApplicationStartingNotification>
{
// TODO: Eventually we should kill this since at some stage we shouldn't even support JSON since we know
// this is faster.
internal const string Nucache_Serializer_Key = "Umbraco.Web.PublishedCache.NuCache.Serializer";
private const string JSON_SERIALIZER_VALUE = "JSON";
private readonly IPublishedSnapshotService _service;
private readonly IKeyValueService _keyValueService;
private readonly IProfilingLogger _profilingLogger;
private readonly ILogger<NuCacheStartupHandler> _logger;
private readonly INuCacheContentService _nuCacheContentService;
private readonly IRuntimeState _runtimeState;
public NuCacheStartupHandler(
IPublishedSnapshotService service,
IKeyValueService keyValueService,
IProfilingLogger profilingLogger,
ILogger<NuCacheStartupHandler> logger)
INuCacheContentService nuCacheContentService,
IRuntimeState runtimeState)
{
_service = service;
_keyValueService = keyValueService;
_profilingLogger = profilingLogger;
_logger = logger;
_nuCacheContentService = nuCacheContentService;
_runtimeState = runtimeState;
}
public void Handle(UmbracoApplicationStartingNotification notification)
=> RebuildDatabaseCacheIfSerializerChanged();
private void RebuildDatabaseCacheIfSerializerChanged()
{
var serializer = ConfigurationManager.AppSettings[Nucache_Serializer_Key];
var currentSerializer = _keyValueService.GetValue(Nucache_Serializer_Key);
if (currentSerializer == null)
if (_runtimeState.Level == Core.RuntimeLevel.Run)
{
currentSerializer = JSON_SERIALIZER_VALUE;
}
if (serializer == null)
{
serializer = JSON_SERIALIZER_VALUE;
}
if (serializer != currentSerializer)
{
_logger.LogWarning("Database NuCache was serialized using {CurrentSerializer}. Currently configured NuCache serializer {Serializer}. Rebuilding Nucache", currentSerializer, serializer);
using (_profilingLogger.TraceDuration<NuCacheStartupHandler>($"Rebuilding NuCache database with {currentSerializer} serializer"))
{
_service.Rebuild();
_keyValueService.SetValue(Nucache_Serializer_Key, serializer);
}
_nuCacheContentService.RebuildDatabaseCacheIfSerializerChanged();
}
}
}
}

View File

@@ -8,6 +8,12 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache.Persistence
/// </summary>
public interface INuCacheContentService
{
/// <summary>
/// Used during startup to see if the configured serialized is different from the persisted serialize type.
/// If they are different, this will rebuild the nucache DB table with the configured serializer.
/// </summary>
void RebuildDatabaseCacheIfSerializerChanged();
// TODO: For these required sort orders, would sorting on Path 'just work'?
ContentNodeKit GetContentSource(int id);

View File

@@ -128,6 +128,8 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache.Persistence
RebuildContentDbCache(serializer, _nucacheSettings.Value.SqlPageSize, contentTypeIds);
RebuildMediaDbCache(serializer, _nucacheSettings.Value.SqlPageSize, mediaTypeIds);
RebuildMemberDbCache(serializer, _nucacheSettings.Value.SqlPageSize, memberTypeIds);
}
// assumes content tree lock

View File

@@ -1,9 +1,14 @@
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.Implement;
namespace Umbraco.Cms.Infrastructure.PublishedCache.Persistence
@@ -11,15 +16,45 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache.Persistence
public class NuCacheContentService : RepositoryService, INuCacheContentService
{
private readonly INuCacheContentRepository _repository;
private readonly IKeyValueService _keyValueService;
private readonly IProfilingLogger _profilingLogger;
private readonly IOptions<NuCacheSettings> _nucacheSettings;
private readonly ILogger<NuCacheContentService> _logger;
private const string NuCacheSerializerKey = "Umbraco.Web.PublishedCache.NuCache.Serializer";
public NuCacheContentService(
INuCacheContentRepository repository,
IKeyValueService keyValueService,
IScopeProvider provider,
ILoggerFactory loggerFactory,
IEventMessagesFactory eventMessagesFactory)
IProfilingLogger profilingLogger,
IEventMessagesFactory eventMessagesFactory,
IOptions<NuCacheSettings> nucacheSettings)
: base(provider, loggerFactory, eventMessagesFactory)
{
_repository = repository;
_keyValueService = keyValueService;
_profilingLogger = profilingLogger;
_nucacheSettings = nucacheSettings;
_logger = loggerFactory.CreateLogger<NuCacheContentService>();
}
public void RebuildDatabaseCacheIfSerializerChanged()
{
NuCacheSerializerType serializer = _nucacheSettings.Value.NuCacheSerializerType;
var currentSerializerValue = _keyValueService.GetValue(NuCacheSerializerKey);
if (!Enum.TryParse(currentSerializerValue, out NuCacheSerializerType currentSerializer)
|| serializer != currentSerializer)
{
_logger.LogWarning("Database NuCache was serialized using {CurrentSerializer}. Currently configured NuCache serializer {Serializer}. Rebuilding Nucache", currentSerializer, serializer);
using (_profilingLogger.TraceDuration<NuCacheContentService>($"Rebuilding NuCache database with {serializer} serializer"))
{
Rebuild();
_keyValueService.SetValue(NuCacheSerializerKey, serializer.ToString());
}
}
}
/// <inheritdoc/>
@@ -92,6 +127,11 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache.Persistence
scope.ReadLock(Constants.Locks.MemberTree);
_repository.Rebuild(contentTypeIds, mediaTypeIds, memberTypeIds);
// Save a key/value of the serialized type. This is used during startup to see
// if the serialized type changed and if so it will rebuild with the correct type.
_keyValueService.SetValue(NuCacheSerializerKey, _nucacheSettings.Value.NuCacheSerializerType.ToString());
scope.Complete();
}
}