Exemplo n.º 1
0
        /// <summary>
        /// Executes.
        /// </summary>
        /// <param name="scopeProvider">A scope provider.</param>
        /// <param name="migrationBuilder">A migration builder.</param>
        /// <param name="keyValueService">A key-value service.</param>
        /// <param name="logger">A logger.</param>
        public void Execute(IScopeProvider scopeProvider, IMigrationBuilder migrationBuilder, IKeyValueService keyValueService, ILogger logger)
        {
            if (scopeProvider == null)
            {
                throw new ArgumentNullException(nameof(scopeProvider));
            }
            if (migrationBuilder == null)
            {
                throw new ArgumentNullException(nameof(migrationBuilder));
            }
            if (keyValueService == null)
            {
                throw new ArgumentNullException(nameof(keyValueService));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            var plan = Plan;

            using (var scope = scopeProvider.CreateScope())
            {
                BeforeMigrations(scope, logger);

                // read current state
                var currentState = keyValueService.GetValue(StateValueKey);
                var forceState   = false;

                if (currentState == null)
                {
                    currentState = plan.InitialState;
                    forceState   = true;
                }

                // execute plan
                var state = plan.Execute(scope, currentState, migrationBuilder, logger);
                if (string.IsNullOrWhiteSpace(state))
                {
                    throw new Exception("Plan execution returned an invalid null or empty state.");
                }

                // save new state
                if (forceState)
                {
                    keyValueService.SetValue(StateValueKey, state);
                }
                else if (currentState != state)
                {
                    keyValueService.SetValue(StateValueKey, currentState, state);
                }

                AfterMigrations(scope, logger);

                scope.Complete();
            }
        }
Exemplo n.º 2
0
        public void SetConsentLevel(TelemetryLevel telemetryLevel)
        {
            var currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;

            _logger.LogInformation("Telemetry level set to {telemetryLevel} by {username}", telemetryLevel, currentUser?.Username);
            _keyValueService.SetValue(Key, telemetryLevel.ToString());
        }
Exemplo n.º 3
0
        private void RebuildDatabaseCacheIfSerializerChanged()
        {
            var serializer        = ConfigurationManager.AppSettings[Nucache_Serializer_Key];
            var currentSerializer = _keyValueService.GetValue(Nucache_Serializer_Key);

            if (currentSerializer == null)
            {
                currentSerializer = JSON_SERIALIZER_VALUE;
            }
            if (serializer == null)
            {
                serializer = JSON_SERIALIZER_VALUE;
            }

            if (serializer != currentSerializer)
            {
                _profilingLogger.Warn <NuCacheSerializerComponent>($"Database NuCache was serialized using {currentSerializer}. Currently configured NuCache serializer {serializer}. Rebuilding Nucache");

                using (_profilingLogger.TraceDuration <NuCacheSerializerComponent>($"Rebuilding NuCache database with {currentSerializer} serializer"))
                {
                    _service.Value.Rebuild();
                    _keyValueService.SetValue(Nucache_Serializer_Key, serializer);
                }
            }
        }
Exemplo n.º 4
0
        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());
                }
            }
        }
Exemplo n.º 5
0
    public void SetConsentLevel(TelemetryLevel telemetryLevel)
    {
        IUser?currentUser = _backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser;

        if (currentUser is null)
        {
            currentUser = _userService.GetUserById(Constants.Security.SuperUserId);
        }

        _logger.LogInformation("Telemetry level set to {telemetryLevel} by {username}", telemetryLevel, currentUser?.Username);
        _keyValueService.SetValue(Key, telemetryLevel.ToString());
    }
        public void Initialize()
        {
            // a quick version of only run once.
            // for a more complete set of methods see Migrations samples
            var installed = keyValueService.GetValue(setupKey);

            if (installed == null || installed != "installed")
            {
                AddSection("admin", CustomSection.SectionAlias);
                keyValueService.SetValue(setupKey, "installed");
            }
        }
        public HttpResponseMessage SaveConfig(RedirectSettings settings)
        {
            try
            {
                _keyValueService.SetValue("redirectSettings_" + _key, JsonConvert.SerializeObject(settings));

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }
Exemplo n.º 8
0
        public void GivenPackageMigrationsExist_WhenLatestStateIsRegistered_ThenLevelIsRun()
        {
            // Add the final state to the keyvalue storage
            IKeyValueService keyValueService = Services.GetRequiredService <IKeyValueService>();

            keyValueService.SetValue(
                Constants.Conventions.Migrations.KeyValuePrefix + TestMigrationPlan.TestMigrationPlanName,
                TestMigrationPlan.TestMigrationFinalState.ToString());

            RuntimeState.DetermineRuntimeLevel();

            Assert.AreEqual(RuntimeLevel.Run, RuntimeState.Level);
            Assert.AreEqual(RuntimeLevelReason.Run, RuntimeState.Reason);
        }
        public void Save(EnterspeedUmbracoConfiguration configuration)
        {
            if (configuration == null)
            {
                return;
            }

            configuration.MediaDomain = configuration.MediaDomain.TrimEnd('/');
            if (!configuration.MediaDomain.IsAbsoluteUrl())
            {
                throw new ConfigurationErrorsException(
                          "Configuration value for Enterspeed.MediaDomain must be absolute url");
            }

            configuration.IsConfigured = true;

            _keyValueService.SetValue(_configurationDatabaseKey, JsonConvert.SerializeObject(configuration));
            _configuration = configuration;
        }
Exemplo n.º 10
0
        public void CanExecute()
        {
            NullLoggerFactory loggerFactory = NullLoggerFactory.Instance;

            var            database = new TestDatabase();
            IDatabaseScope scope    = Mock.Of <IDatabaseScope>(x => x.Notifications == Mock.Of <IScopedNotificationPublisher>());

            Mock.Get(scope)
            .Setup(x => x.Database)
            .Returns(database);

            var sqlContext    = new SqlContext(new SqlServerSyntaxProvider(Options.Create(new GlobalSettings())), DatabaseType.SQLCe, Mock.Of <IPocoDataFactory>());
            var scopeProvider = new MigrationTests.TestScopeProvider(scope)
            {
                SqlContext = sqlContext
            };

            IMigrationBuilder migrationBuilder = Mock.Of <IMigrationBuilder>();

            Mock.Get(migrationBuilder)
            .Setup(x => x.Build(It.IsAny <Type>(), It.IsAny <IMigrationContext>()))
            .Returns <Type, IMigrationContext>((t, c) =>
            {
                switch (t.Name)
                {
                case "DeleteRedirectUrlTable":
                    return(new DeleteRedirectUrlTable(c));

                case "NoopMigration":
                    return(new NoopMigration(c));

                default:
                    throw new NotSupportedException();
                }
            });

            var executor = new MigrationPlanExecutor(scopeProvider, scopeProvider, loggerFactory, migrationBuilder);

            MigrationPlan plan = new MigrationPlan("default")
                                 .From(string.Empty)
                                 .To <DeleteRedirectUrlTable>("{4A9A1A8F-0DA1-4BCF-AD06-C19D79152E35}")
                                 .To <NoopMigration>("VERSION.33");

            IKeyValueService kvs = Mock.Of <IKeyValueService>();

            Mock.Get(kvs).Setup(x => x.GetValue(It.IsAny <string>()))
            .Returns <string>(k => k == "Umbraco.Tests.MigrationPlan" ? string.Empty : null);

            string state;

            using (IScope s = scopeProvider.CreateScope())
            {
                // read current state
                var sourceState = kvs.GetValue("Umbraco.Tests.MigrationPlan") ?? string.Empty;

                // execute plan
                state = executor.Execute(plan, sourceState);

                // save new state
                kvs.SetValue("Umbraco.Tests.MigrationPlan", sourceState, state);

                s.Complete();
            }

            Assert.AreEqual("VERSION.33", state);
            Assert.AreEqual(1, database.Operations.Count);
            Assert.AreEqual("DROP TABLE [umbracoRedirectUrl]", database.Operations[0].Sql);
        }