Removed key value service initialization logic.

This commit is contained in:
Andy Butland
2020-03-28 15:04:36 +01:00
parent 3e40389819
commit f01ccb49dd
6 changed files with 7 additions and 157 deletions
@@ -1,6 +1,5 @@
using Umbraco.Core.Persistence.Repositories;
using Umbraco.Core.Persistence.Repositories.Implement;
using Umbraco.Infrastructure.Migrations.Custom;
namespace Umbraco.Core.Composing.CompositionExtensions
{
@@ -49,7 +48,6 @@ namespace Umbraco.Core.Composing.CompositionExtensions
composition.RegisterUnique<IStylesheetRepository, StylesheetRepository>();
composition.RegisterUnique<IContentTypeCommonRepository, ContentTypeCommonRepository>();
composition.RegisterUnique<IKeyValueRepository, KeyValueRepository>();
composition.RegisterUnique<IKeyValueServiceInitialization, KeyValueServiceInitialization>();
composition.RegisterUnique<IInstallationRepository, InstallationRepository>();
composition.RegisterUnique<IUpgradeCheckRepository, UpgradeCheckRepository>();
@@ -1,9 +0,0 @@
using Umbraco.Core.Persistence;
namespace Umbraco.Infrastructure.Migrations.Custom
{
public interface IKeyValueServiceInitialization
{
void PerformInitialMigration(IUmbracoDatabase database);
}
}
@@ -1,80 +0,0 @@
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Migrations;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Dtos;
namespace Umbraco.Infrastructure.Migrations.Custom
{
public class KeyValueServiceInitialization : IKeyValueServiceInitialization
{
private readonly ILogger _logger;
public KeyValueServiceInitialization(ILogger logger)
{
_logger = logger;
}
public void PerformInitialMigration(IUmbracoDatabase database)
{
var context = new MigrationContext(database, _logger);
var initMigration = new InitialMigration(context);
initMigration.Migrate();
}
/// <summary>
/// A custom migration that executes standalone during the Initialize phase of the KeyValueService.
/// </summary>
internal class InitialMigration : MigrationBase
{
public InitialMigration(IMigrationContext context)
: base(context)
{ }
public override void Migrate()
{
// as long as we are still running 7 this migration will be invoked,
// but due to multiple restarts during upgrades, maybe the table
// exists already
if (TableExists(Constants.DatabaseSchema.Tables.KeyValue))
return;
Logger.Info<KeyValueServiceInitialization>("Creating KeyValue structure.");
// the locks table was initially created with an identity (auto-increment) primary key,
// but we don't want this, especially as we are about to insert a new row into the table,
// so here we drop that identity
DropLockTableIdentity();
// insert the lock object for key/value
Insert.IntoTable(Constants.DatabaseSchema.Tables.Lock).Row(new { id = Constants.Locks.KeyValues, name = "KeyValues", value = 1 }).Do();
// create the key-value table
Create.Table<KeyValueDto>().Do();
}
private void DropLockTableIdentity()
{
// one cannot simply drop an identity, that requires a bit of work
// create a temp. id column and copy values
Alter.Table(Constants.DatabaseSchema.Tables.Lock).AddColumn("nid").AsInt32().Nullable().Do();
Execute.Sql("update umbracoLock set nid = id").Do();
// drop the id column entirely (cannot just drop identity)
Delete.PrimaryKey("PK_umbracoLock").FromTable(Constants.DatabaseSchema.Tables.Lock).Do();
Delete.Column("id").FromTable(Constants.DatabaseSchema.Tables.Lock).Do();
// recreate the id column without identity and copy values
Alter.Table(Constants.DatabaseSchema.Tables.Lock).AddColumn("id").AsInt32().Nullable().Do();
Execute.Sql("update umbracoLock set id = nid").Do();
// drop the temp. id column
Delete.Column("nid").FromTable(Constants.DatabaseSchema.Tables.Lock).Do();
// complete the primary key
Alter.Table(Constants.DatabaseSchema.Tables.Lock).AlterColumn("id").AsInt32().NotNullable().PrimaryKey("PK_umbracoLock").Do();
}
}
}
}
@@ -1,5 +1,4 @@
using System;
using System.IO;
using Umbraco.Core.Cache;
using Umbraco.Core.Composing;
using Umbraco.Core.Composing.CompositionExtensions;
@@ -7,9 +6,8 @@ using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.Grid;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Dashboards;
using Umbraco.Core.Hosting;
using Umbraco.Core.Dictionary;
using Umbraco.Core.IO;
using Umbraco.Core.Hosting;
using Umbraco.Core.Logging;
using Umbraco.Core.Manifest;
using Umbraco.Core.Migrations;
@@ -25,16 +23,15 @@ using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;
using Umbraco.Core.Strings;
using Umbraco.Core.Sync;
using Umbraco.Web.Models.PublishedContent;
using Umbraco.Web.PublishedCache;
using Umbraco.Web;
using Umbraco.Web.Migrations.PostMigrations;
using Umbraco.Web.Install;
using Umbraco.Web.Trees;
using Umbraco.Web.Migrations.PostMigrations;
using Umbraco.Web.Models.PublishedContent;
using Umbraco.Web.PropertyEditors;
using Umbraco.Web.PublishedCache;
using Umbraco.Web.Services;
using Umbraco.Web.Trees;
using IntegerValidator = Umbraco.Core.PropertyEditors.Validators.IntegerValidator;
using Umbraco.Infrastructure.Migrations.Custom;
namespace Umbraco.Core.Runtime
{
@@ -149,7 +146,6 @@ namespace Umbraco.Core.Runtime
.Append<DefaultUrlSegmentProvider>();
composition.RegisterUnique<IMigrationBuilder>(factory => new MigrationBuilder(factory));
composition.RegisterUnique<IKeyValueServiceInitialization, KeyValueServiceInitialization>();
// by default, register a noop factory
composition.RegisterUnique<IPublishedModelFactory, NoopPublishedModelFactory>();
@@ -1,74 +1,24 @@
using System;
using Umbraco.Core.Configuration;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence.Repositories;
using Umbraco.Core.Scoping;
using Umbraco.Infrastructure.Migrations.Custom;
namespace Umbraco.Core.Services.Implement
{
internal class KeyValueService : IKeyValueService
{
private readonly object _initialock = new object();
private readonly IScopeProvider _scopeProvider;
private readonly IKeyValueRepository _repository;
private readonly IKeyValueServiceInitialization _initialization;
private readonly ILogger _logger;
private readonly IUmbracoVersion _umbracoVersion;
private bool _initialized;
public KeyValueService(IScopeProvider scopeProvider, IKeyValueRepository repository, IKeyValueServiceInitialization initialization, ILogger logger, IUmbracoVersion umbracoVersion)
public KeyValueService(IScopeProvider scopeProvider, IKeyValueRepository repository)
{
_scopeProvider = scopeProvider;
_repository = repository;
_initialization = initialization;
_logger = logger;
_umbracoVersion = umbracoVersion;
}
private void EnsureInitialized()
{
lock (_initialock)
{
if (_initialized) return;
Initialize();
}
}
private void Initialize()
{
// the key/value service is entirely self-managed, because it is used by the
// upgrader and anything we might change need to happen before everything else
// if already running 8, either following an upgrade or an install,
// then everything should be ok (the table should exist, etc)
if (_umbracoVersion.LocalVersion != null && _umbracoVersion.LocalVersion.Major >= 8)
{
_initialized = true;
return;
}
// else we are upgrading from 7, we can assume that the locks table
// exists, but we need to create everything for key/value
using (var scope = _scopeProvider.CreateScope())
{
_initialization.PerformInitialMigration(scope.Database);
scope.Complete();
}
// but don't assume we are initializing
// we are upgrading from v7 and if anything goes wrong,
// the table and everything will be rolled back
}
/// <inheritdoc />
public string GetValue(string key)
{
EnsureInitialized();
using (var scope = _scopeProvider.CreateScope())
{
return _repository.Get(key)?.Value;
@@ -78,8 +28,6 @@ namespace Umbraco.Core.Services.Implement
/// <inheritdoc />
public void SetValue(string key, string value)
{
EnsureInitialized();
using (var scope = _scopeProvider.CreateScope())
{
scope.WriteLock(Constants.Locks.KeyValues);
@@ -116,8 +64,6 @@ namespace Umbraco.Core.Services.Implement
/// <inheritdoc />
public bool TrySetValue(string key, string originalValue, string newValue)
{
EnsureInitialized();
using (var scope = _scopeProvider.CreateScope())
{
scope.WriteLock(Constants.Locks.KeyValues);
+1 -2
View File
@@ -21,7 +21,6 @@ using Umbraco.Core.Scoping;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;
using Umbraco.Core.Strings;
using Umbraco.Infrastructure.Migrations.Custom;
using Umbraco.Tests.TestHelpers.Stubs;
using Current = Umbraco.Web.Composing.Current;
@@ -194,7 +193,7 @@ namespace Umbraco.Tests.TestHelpers
var tagService = GetLazyService<ITagService>(factory, c => new TagService(scopeProvider, logger, eventMessagesFactory, GetRepo<ITagRepository>(c)));
var redirectUrlService = GetLazyService<IRedirectUrlService>(factory, c => new RedirectUrlService(scopeProvider, logger, eventMessagesFactory, GetRepo<IRedirectUrlRepository>(c)));
var consentService = GetLazyService<IConsentService>(factory, c => new ConsentService(scopeProvider, logger, eventMessagesFactory, GetRepo<IConsentRepository>(c)));
var keyValueService = GetLazyService<IKeyValueService>(factory, c => new KeyValueService(scopeProvider, GetRepo<IKeyValueRepository>(c), Mock.Of<IKeyValueServiceInitialization>(), logger, umbracoVersion));
var keyValueService = GetLazyService<IKeyValueService>(factory, c => new KeyValueService(scopeProvider, GetRepo<IKeyValueRepository>(c)));
var contentTypeServiceBaseFactory = GetLazyService<IContentTypeBaseServiceProvider>(factory, c => new ContentTypeBaseServiceProvider(factory.GetInstance<IContentTypeService>(),factory.GetInstance<IMediaTypeService>(),factory.GetInstance<IMemberTypeService>()));
return new ServiceContext(