From 8e1e7a8676584a42cc6c19d73378902e972ed94f Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 22 Jun 2015 16:22:47 +0200 Subject: [PATCH] Gets the IsConfigured check checking the migration data, updates the install controller to ensure there's no infinite redirects and to use the IsUpgrading flag. Now to add entries to the table when the migrations have executed. --- src/Umbraco.Core/ApplicationContext.cs | 84 +++++++++++-------- .../Configuration/GlobalSettings.cs | 1 + .../Interfaces/IMigrationEntryRepository.cs | 5 +- .../Repositories/MigrationEntryRepository.cs | 13 +++ .../Services/MigrationEntryService.cs | 4 +- .../Sync/DatabaseServerRegistrar.cs | 4 +- .../Umbraco/Views/AuthorizeUpgrade.cshtml | 10 ++- .../Install/Controllers/InstallController.cs | 25 ++---- 8 files changed, 85 insertions(+), 61 deletions(-) diff --git a/src/Umbraco.Core/ApplicationContext.cs b/src/Umbraco.Core/ApplicationContext.cs index 8b78a6e676..fc72ea5674 100644 --- a/src/Umbraco.Core/ApplicationContext.cs +++ b/src/Umbraco.Core/ApplicationContext.cs @@ -38,6 +38,8 @@ namespace Umbraco.Core _services = serviceContext; ApplicationCache = cache; ProfilingLogger = logger; + + Init(); } /// @@ -60,6 +62,8 @@ namespace Umbraco.Core public ApplicationContext(CacheHelper cache) { ApplicationCache = cache; + + Init(); } /// @@ -190,16 +194,10 @@ namespace Umbraco.Core // GlobalSettings.CurrentVersion returns the hard-coded "current version" // the system is configured if they match // if they don't, install runs, updates web.config (presumably) and updates GlobalSettings.ConfiguredStatus - // - // then there is Application["umbracoNeedConfiguration"] which makes no sense... getting rid of it... SD: I have actually remove that now! - // + public bool IsConfigured { - // todo - we should not do this - ok for now - get - { - return Configured; - } + get { return _configured.Value; } } /// @@ -233,37 +231,51 @@ namespace Umbraco.Core /// internal string OriginalRequestUrl { get; set; } - private bool _versionsDifferenceReported; - /// - /// Checks if the version configured matches the assembly version - /// - private bool Configured - { - get - { - try - { - var configStatus = ConfigurationStatus; - var currentVersion = UmbracoVersion.Current.ToString(3); - var ok = configStatus == currentVersion; + private Lazy _configured; - if (ok == false && _versionsDifferenceReported == false) - { - // remember it's been reported so we don't flood the log - // no thread-safety so there may be a few log entries, doesn't matter - _versionsDifferenceReported = true; + private void Init() + { + + //Create the lazy value to resolve whether or not the application is 'configured' + _configured = new Lazy(() => + { + try + { + var configStatus = ConfigurationStatus; + var currentVersion = UmbracoVersion.Current.ToString(3); + var ok = configStatus == currentVersion; + + if (ok) + { + //The versions are the same in config, but are they the same in the database. We can only check this + // if we have a db context available, if we don't then we are not installed anyways + if (DatabaseContext.IsDatabaseConfigured && DatabaseContext.CanConnect) + { + var found = Services.MigrationEntryService.FindEntry(GlobalSettings.UmbracoMigrationName, UmbracoVersion.Current); + if (found == null) + { + //we haven't executed this migration in this environment, so even though the config versions match, + // this db has not been updated. + ProfilingLogger.Logger.Debug("The migration for version: '" + currentVersion + " has not been executed, there is no record in the database"); + ok = false; + } + } + } + else + { ProfilingLogger.Logger.Debug("CurrentVersion different from configStatus: '" + currentVersion + "','" + configStatus + "'"); - } - - return ok; - } - catch - { - return false; - } - } - } + } + + return ok; + } + catch + { + return false; + } + + }); + } private string ConfigurationStatus { diff --git a/src/Umbraco.Core/Configuration/GlobalSettings.cs b/src/Umbraco.Core/Configuration/GlobalSettings.cs index a32b38e9da..0df43b67c7 100644 --- a/src/Umbraco.Core/Configuration/GlobalSettings.cs +++ b/src/Umbraco.Core/Configuration/GlobalSettings.cs @@ -242,6 +242,7 @@ namespace Umbraco.Core.Configuration } } + //TODO: Move these to constants! public const string UmbracoConnectionName = "umbracoDbDSN"; public const string UmbracoMigrationName = "Umbraco"; diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IMigrationEntryRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IMigrationEntryRepository.cs index ff0a339a61..3128a0f9e4 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IMigrationEntryRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IMigrationEntryRepository.cs @@ -1,9 +1,10 @@ -using Umbraco.Core.Models; +using System; +using Umbraco.Core.Models; namespace Umbraco.Core.Persistence.Repositories { public interface IMigrationEntryRepository : IRepositoryQueryable { - + IMigrationEntry FindEntry(string migrationName, Version version); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/MigrationEntryRepository.cs b/src/Umbraco.Core/Persistence/Repositories/MigrationEntryRepository.cs index 657a1698eb..5179ea2506 100644 --- a/src/Umbraco.Core/Persistence/Repositories/MigrationEntryRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/MigrationEntryRepository.cs @@ -112,5 +112,18 @@ namespace Umbraco.Core.Persistence.Repositories entity.ResetDirtyProperties(); } + + public IMigrationEntry FindEntry(string migrationName, Version version) + { + var sql = new Sql().Select("*") + .From(SqlSyntax) + .Where(x => x.Name.InvariantEquals(migrationName) && x.Version == version.ToString()); + + var result = Database.FirstOrDefault(sql); + + var factory = new MigrationEntryFactory(); + + return result == null ? null : factory.BuildEntity(result); + } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/MigrationEntryService.cs b/src/Umbraco.Core/Services/MigrationEntryService.cs index 34a57e08e8..32e5e408cb 100644 --- a/src/Umbraco.Core/Services/MigrationEntryService.cs +++ b/src/Umbraco.Core/Services/MigrationEntryService.cs @@ -54,9 +54,7 @@ namespace Umbraco.Core.Services var uow = UowProvider.GetUnitOfWork(); using (var repo = RepositoryFactory.CreateMigrationEntryRepository(uow)) { - var query = Query.Builder - .Where(x => x.MigrationName.ToUpper() == migrationName.ToUpper() && x.Version == version); - return repo.GetByQuery(query).FirstOrDefault(); + return repo.FindEntry(migrationName, version); } } diff --git a/src/Umbraco.Core/Sync/DatabaseServerRegistrar.cs b/src/Umbraco.Core/Sync/DatabaseServerRegistrar.cs index 8bf02b873d..a200b8e2ab 100644 --- a/src/Umbraco.Core/Sync/DatabaseServerRegistrar.cs +++ b/src/Umbraco.Core/Sync/DatabaseServerRegistrar.cs @@ -9,7 +9,7 @@ namespace Umbraco.Core.Sync /// public sealed class DatabaseServerRegistrar : IServerRegistrar { - private readonly Lazy _registrationService; + private readonly Lazy _registrationService; /// /// Gets or sets the registrar options. @@ -21,7 +21,7 @@ namespace Umbraco.Core.Sync /// /// The registration service. /// Some options. - public DatabaseServerRegistrar(Lazy registrationService, DatabaseServerRegistrarOptions options) + public DatabaseServerRegistrar(Lazy registrationService, DatabaseServerRegistrarOptions options) { if (registrationService == null) throw new ArgumentNullException("registrationService"); if (options == null) throw new ArgumentNullException("options"); diff --git a/src/Umbraco.Web.UI/Umbraco/Views/AuthorizeUpgrade.cshtml b/src/Umbraco.Web.UI/Umbraco/Views/AuthorizeUpgrade.cshtml index dcad153fce..3d4f6baa7e 100644 --- a/src/Umbraco.Web.UI/Umbraco/Views/AuthorizeUpgrade.cshtml +++ b/src/Umbraco.Web.UI/Umbraco/Views/AuthorizeUpgrade.cshtml @@ -59,9 +59,15 @@ redirectUrl = Url.Action("AuthorizeUpgrade", "BackOffice") }); } - @Html.BareMinimumServerVariables(Url, externalLoginUrl) + @Html.BareMinimumServerVariablesScript(Url, externalLoginUrl) - @Html.AngularExternalLoginInfoValues((IEnumerable)ViewBag.ExternalSignInError) + @*And finally we can load in our angular app*@ diff --git a/src/Umbraco.Web/Install/Controllers/InstallController.cs b/src/Umbraco.Web/Install/Controllers/InstallController.cs index 3d6f397f9f..572bd0a192 100644 --- a/src/Umbraco.Web/Install/Controllers/InstallController.cs +++ b/src/Umbraco.Web/Install/Controllers/InstallController.cs @@ -37,30 +37,23 @@ namespace Umbraco.Web.Install.Controllers [HttpGet] public ActionResult Index() { - //if this is not an upgrade we will log in with the default user. - // It's not considered an upgrade if the ConfigurationStatus is missing or empty or if the db is not configured. - if (string.IsNullOrWhiteSpace(GlobalSettings.ConfigurationStatus) == false - && ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured) + if (ApplicationContext.Current.IsConfigured) { - Version current; - if (Version.TryParse(GlobalSettings.ConfigurationStatus, out current)) - { - //check if we are on the current version, and not let the installer execute - if (current == UmbracoVersion.Current) - { - return Redirect(SystemDirectories.Umbraco.EnsureEndsWith('/')); - } - } + return Redirect(SystemDirectories.Umbraco.EnsureEndsWith('/')); + } + if (ApplicationContext.Current.IsUpgrading) + { var result = _umbracoContext.Security.ValidateCurrentUser(false); switch (result) { case ValidateRequestAttempt.FailedNoPrivileges: - case ValidateRequestAttempt.FailedNoContextId: - return Redirect(SystemDirectories.Umbraco + "/AuthorizeUpgrade?redir=" + Server.UrlEncode(Request.RawUrl)); + case ValidateRequestAttempt.FailedNoContextId: + return Redirect(SystemDirectories.Umbraco + "/AuthorizeUpgrade?redir=" + Server.UrlEncode(Request.RawUrl)); } - } + } + //gen the install base url ViewBag.InstallApiBaseUrl = Url.GetUmbracoApiService("GetSetup", "InstallApi", "UmbracoInstall").TrimEnd("GetSetup");