2020-01-21 17:03:46 -08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
2019-01-11 14:30:04 +11:00
|
|
|
|
using System.Threading.Tasks;
|
2014-02-26 16:30:25 +01:00
|
|
|
|
using Umbraco.Core;
|
2019-11-20 10:50:15 +01:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2020-02-10 11:23:23 +01:00
|
|
|
|
using Umbraco.Core.IO;
|
2014-02-26 16:30:25 +01:00
|
|
|
|
using Umbraco.Core.Logging;
|
2017-12-18 18:26:32 +01:00
|
|
|
|
using Umbraco.Core.Migrations.Install;
|
2019-02-13 09:53:17 +01:00
|
|
|
|
using Umbraco.Core.Migrations.Upgrade;
|
2019-11-15 11:07:37 +01:00
|
|
|
|
using Umbraco.Web.Composing;
|
2014-02-26 16:30:25 +01:00
|
|
|
|
using Umbraco.Web.Install.Models;
|
2019-02-13 09:53:17 +01:00
|
|
|
|
using Umbraco.Web.Migrations.PostMigrations;
|
2014-02-26 16:30:25 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Install.InstallSteps
|
|
|
|
|
|
{
|
2014-10-01 14:58:30 +10:00
|
|
|
|
[InstallSetupStep(InstallationType.Upgrade | InstallationType.NewInstall,
|
2014-03-05 20:03:45 +01:00
|
|
|
|
"DatabaseUpgrade", 12, "")]
|
2014-02-26 16:30:25 +01:00
|
|
|
|
internal class DatabaseUpgradeStep : InstallSetupStep<object>
|
|
|
|
|
|
{
|
2016-11-29 10:31:25 +01:00
|
|
|
|
private readonly DatabaseBuilder _databaseBuilder;
|
2016-09-01 19:06:08 +02:00
|
|
|
|
private readonly IRuntimeState _runtime;
|
2016-09-11 19:57:33 +02:00
|
|
|
|
private readonly ILogger _logger;
|
2019-11-20 10:50:15 +01:00
|
|
|
|
private readonly IUmbracoVersion _umbracoVersion;
|
2019-12-18 10:32:22 +01:00
|
|
|
|
private readonly IGlobalSettings _globalSettings;
|
2020-01-21 17:03:46 -08:00
|
|
|
|
private readonly IConnectionStrings _connectionStrings;
|
2020-02-10 11:23:23 +01:00
|
|
|
|
private readonly IIOHelper _ioHelper;
|
2016-09-01 19:06:08 +02:00
|
|
|
|
|
2020-02-10 11:23:23 +01:00
|
|
|
|
public DatabaseUpgradeStep(DatabaseBuilder databaseBuilder, IRuntimeState runtime, ILogger logger, IUmbracoVersion umbracoVersion, IGlobalSettings globalSettings, IConnectionStrings connectionStrings, IIOHelper ioHelper)
|
2014-02-26 16:30:25 +01:00
|
|
|
|
{
|
2016-11-29 10:31:25 +01:00
|
|
|
|
_databaseBuilder = databaseBuilder;
|
2016-09-01 19:06:08 +02:00
|
|
|
|
_runtime = runtime;
|
2016-09-11 19:57:33 +02:00
|
|
|
|
_logger = logger;
|
2019-11-20 10:50:15 +01:00
|
|
|
|
_umbracoVersion = umbracoVersion;
|
2019-12-18 10:32:22 +01:00
|
|
|
|
_globalSettings = globalSettings;
|
2020-01-21 17:03:46 -08:00
|
|
|
|
_connectionStrings = connectionStrings ?? throw new ArgumentNullException(nameof(connectionStrings));
|
2020-02-10 11:23:23 +01:00
|
|
|
|
_ioHelper = ioHelper;
|
2014-02-26 16:30:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-11 14:30:04 +11:00
|
|
|
|
public override Task<InstallSetupResult> ExecuteAsync(object model)
|
2014-02-26 16:30:25 +01:00
|
|
|
|
{
|
2017-07-20 11:21:28 +02:00
|
|
|
|
var installSteps = InstallStatusTracker.GetStatus().ToArray();
|
2014-03-05 12:21:49 +11:00
|
|
|
|
var previousStep = installSteps.Single(x => x.Name == "DatabaseInstall");
|
2014-02-26 16:30:25 +01:00
|
|
|
|
var upgrade = previousStep.AdditionalData.ContainsKey("upgrade");
|
|
|
|
|
|
|
|
|
|
|
|
if (upgrade)
|
|
|
|
|
|
{
|
2016-09-11 19:57:33 +02:00
|
|
|
|
_logger.Info<DatabaseUpgradeStep>("Running 'Upgrade' service");
|
2014-02-26 16:30:25 +01:00
|
|
|
|
|
2019-12-18 10:32:22 +01:00
|
|
|
|
var plan = new UmbracoPlan(_umbracoVersion, _globalSettings);
|
2019-02-13 09:53:17 +01:00
|
|
|
|
plan.AddPostMigration<ClearCsrfCookies>(); // needed when running installer (back-office)
|
|
|
|
|
|
|
|
|
|
|
|
var result = _databaseBuilder.UpgradeSchemaAndData(plan);
|
2014-02-26 16:30:25 +01:00
|
|
|
|
|
2014-03-13 21:47:08 +11:00
|
|
|
|
if (result.Success == false)
|
|
|
|
|
|
{
|
2014-03-20 17:35:51 +11:00
|
|
|
|
throw new InstallException("The database failed to upgrade. ERROR: " + result.Message);
|
2014-03-13 21:47:08 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-10 11:23:23 +01:00
|
|
|
|
DatabaseInstallStep.HandleConnectionStrings(_logger, _ioHelper);
|
2014-02-26 16:30:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-11 14:30:04 +11:00
|
|
|
|
return Task.FromResult<InstallSetupResult>(null);
|
2014-02-26 16:30:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-03-05 14:30:17 +11:00
|
|
|
|
public override bool RequiresExecution(object model)
|
2014-03-04 11:16:42 +11:00
|
|
|
|
{
|
2014-03-04 19:20:36 +11:00
|
|
|
|
//if it's properly configured (i.e. the versions match) then no upgrade necessary
|
2016-09-01 19:06:08 +02:00
|
|
|
|
if (_runtime.Level == RuntimeLevel.Run)
|
2014-03-04 19:20:36 +11:00
|
|
|
|
return false;
|
2014-03-05 12:21:49 +11:00
|
|
|
|
|
|
|
|
|
|
var installSteps = InstallStatusTracker.GetStatus().ToArray();
|
2014-10-01 14:58:30 +10:00
|
|
|
|
//this step relies on the previous one completed - because it has stored some information we need
|
2014-03-05 12:21:49 +11:00
|
|
|
|
if (installSteps.Any(x => x.Name == "DatabaseInstall" && x.AdditionalData.ContainsKey("upgrade")) == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2020-01-21 17:03:46 -08:00
|
|
|
|
var databaseSettings = _connectionStrings[Constants.System.UmbracoConnectionName];
|
2014-03-04 19:20:36 +11:00
|
|
|
|
|
2019-11-18 14:11:50 +01:00
|
|
|
|
if (databaseSettings.IsConnectionStringConfigured())
|
2014-03-04 19:20:36 +11:00
|
|
|
|
{
|
2018-12-18 10:39:39 +01:00
|
|
|
|
// a connection string was present, determine whether this is an install/upgrade
|
|
|
|
|
|
// return true (upgrade) if there is an installed version, else false (install)
|
|
|
|
|
|
var result = _databaseBuilder.ValidateSchema();
|
|
|
|
|
|
return result.DetermineHasInstalledVersion();
|
2014-03-04 19:20:36 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//no connection string configured, probably a fresh install
|
|
|
|
|
|
return false;
|
2014-03-04 11:16:42 +11:00
|
|
|
|
}
|
2014-02-26 16:30:25 +01:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|