From a83b524130f8188ac17556862b10bd74a3e398f0 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Tue, 26 Jul 2016 16:18:00 +0200 Subject: [PATCH 1/3] DetermineInstalledVersionByMigrations needs to check if the table exists, not the code asking for a version --- src/Umbraco.Core/DatabaseContext.cs | 9 ++------- .../Migrations/Initial/DatabaseSchemaResult.cs | 9 +++++++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Umbraco.Core/DatabaseContext.cs b/src/Umbraco.Core/DatabaseContext.cs index 7f8cbbf0f7..36da6007f6 100644 --- a/src/Umbraco.Core/DatabaseContext.cs +++ b/src/Umbraco.Core/DatabaseContext.cs @@ -625,13 +625,8 @@ namespace Umbraco.Core var installedSchemaVersion = new SemVersion(schemaResult.DetermineInstalledVersion()); - var installedMigrationVersion = new SemVersion(0); - //we cannot check the migrations table if it doesn't exist, this will occur when upgrading to 7.3 - if (schemaResult.ValidTables.Any(x => x.InvariantEquals("umbracoMigration"))) - { - installedMigrationVersion = schemaResult.DetermineInstalledVersionByMigrations(migrationEntryService); - } - + var installedMigrationVersion = schemaResult.DetermineInstalledVersionByMigrations(migrationEntryService); + var targetVersion = UmbracoVersion.Current; //In some cases - like upgrading from 7.2.6 -> 7.3, there will be no migration information in the database and therefore it will diff --git a/src/Umbraco.Core/Persistence/Migrations/Initial/DatabaseSchemaResult.cs b/src/Umbraco.Core/Persistence/Migrations/Initial/DatabaseSchemaResult.cs index 9af6d46fbb..e459f96c6d 100644 --- a/src/Umbraco.Core/Persistence/Migrations/Initial/DatabaseSchemaResult.cs +++ b/src/Umbraco.Core/Persistence/Migrations/Initial/DatabaseSchemaResult.cs @@ -43,8 +43,13 @@ namespace Umbraco.Core.Persistence.Migrations.Initial /// public SemVersion DetermineInstalledVersionByMigrations(IMigrationEntryService migrationEntryService) { - var allMigrations = migrationEntryService.GetAll(GlobalSettings.UmbracoMigrationName); - var mostrecent = allMigrations.OrderByDescending(x => x.Version).Select(x => x.Version).FirstOrDefault(); + SemVersion mostrecent = null; + + if (ValidTables.Any(x => x.InvariantEquals("umbracoMigrations"))) + { + var allMigrations = migrationEntryService.GetAll(GlobalSettings.UmbracoMigrationName); + mostrecent = allMigrations.OrderByDescending(x => x.Version).Select(x => x.Version).FirstOrDefault(); + } return mostrecent ?? new SemVersion(new Version(0, 0, 0)); } From bb376367d6e3c2ca43e08e2b6125f6955b949f11 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Tue, 26 Jul 2016 16:19:17 +0200 Subject: [PATCH 2/3] If the umbracoMigration table does not yet exist, then get the configured version from the web.config and parse it --- .../Install/InstallSteps/UpgradeStep.cs | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Umbraco.Web/Install/InstallSteps/UpgradeStep.cs b/src/Umbraco.Web/Install/InstallSteps/UpgradeStep.cs index 42bca03498..5ef2577179 100644 --- a/src/Umbraco.Web/Install/InstallSteps/UpgradeStep.cs +++ b/src/Umbraco.Web/Install/InstallSteps/UpgradeStep.cs @@ -1,3 +1,5 @@ +using System; +using System.Linq; using Semver; using Umbraco.Core; using Umbraco.Core.Configuration; @@ -52,12 +54,35 @@ namespace Umbraco.Web.Install.InstallSteps //If we have a db context available, if we don't then we are not installed anyways if (ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured && ApplicationContext.Current.DatabaseContext.CanConnect) - { version = ApplicationContext.Current.DatabaseContext.ValidateDatabaseSchema().DetermineInstalledVersionByMigrations(ApplicationContext.Current.Services.MigrationEntryService); + + if (version != new SemVersion(0)) + return version; + + // If we aren't able to get a result from the umbracoMigrations table then use the version in web.config, if it's available + if (string.IsNullOrWhiteSpace(GlobalSettings.ConfigurationStatus)) + return version; + + var configuredVersion = GlobalSettings.ConfigurationStatus; + + string currentComment = null; + + var current = configuredVersion.Split('-'); + if (current.Length > 1) + currentComment = current[1]; + + Version currentVersion; + if (Version.TryParse(current[0], out currentVersion)) + { + version = new SemVersion( + currentVersion.Major, + currentVersion.Minor, + currentVersion.Build, + string.IsNullOrWhiteSpace(currentComment) ? null : currentComment, + currentVersion.Revision > 0 ? currentVersion.Revision.ToString() : null); } return version; } - } } \ No newline at end of file From c955cce28deb88b2993b3be1df982c9a6de1a85b Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Tue, 26 Jul 2016 16:20:22 +0200 Subject: [PATCH 3/3] Because the cmsStylesheetProperty table was already deleted by an earlier migration, it can't be deleted again However because all of this runs in one transaction, it will try anyway, we need to do this in a "local" context instead --- .../RemoveStylesheetDataAndTablesAgain.cs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/RemoveStylesheetDataAndTablesAgain.cs b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/RemoveStylesheetDataAndTablesAgain.cs index 48135f35d7..96523e25e8 100644 --- a/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/RemoveStylesheetDataAndTablesAgain.cs +++ b/src/Umbraco.Core/Persistence/Migrations/Upgrades/TargetVersionSevenFiveZero/RemoveStylesheetDataAndTablesAgain.cs @@ -19,22 +19,32 @@ namespace Umbraco.Core.Persistence.Migrations.Upgrades.TargetVersionSevenFiveZer public override void Up() { + // defer, because we are making decisions based upon what's in the database + Execute.Code(MigrationCode); + } + + private string MigrationCode(Database database) + { + var localContext = new LocalMigrationContext(Context.CurrentDatabaseProvider, database, SqlSyntax, Logger); + //Clear all stylesheet data if the tables exist var tables = SqlSyntax.GetTablesInSchema(Context.Database).ToArray(); if (tables.InvariantContains("cmsStylesheetProperty")) { - Delete.FromTable("cmsStylesheetProperty").AllRows(); - Delete.FromTable("umbracoNode").Row(new { nodeObjectType = new Guid(Constants.ObjectTypes.StylesheetProperty) }); + localContext.Delete.FromTable("cmsStylesheetProperty").AllRows(); + localContext.Delete.FromTable("umbracoNode").Row(new { nodeObjectType = new Guid(Constants.ObjectTypes.StylesheetProperty) }); - Delete.Table("cmsStylesheetProperty"); + localContext.Delete.Table("cmsStylesheetProperty"); } if (tables.InvariantContains("cmsStylesheet")) { - Delete.FromTable("cmsStylesheet").AllRows(); - Delete.FromTable("umbracoNode").Row(new { nodeObjectType = new Guid(Constants.ObjectTypes.Stylesheet) }); + localContext.Delete.FromTable("cmsStylesheet").AllRows(); + localContext.Delete.FromTable("umbracoNode").Row(new { nodeObjectType = new Guid(Constants.ObjectTypes.Stylesheet) }); - Delete.Table("cmsStylesheet"); + localContext.Delete.Table("cmsStylesheet"); } + + return localContext.GetSql(); } public override void Down()