2014-02-26 16:30:25 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-03-04 19:20:36 +11:00
|
|
|
using System.Configuration;
|
2014-02-26 16:30:25 +01:00
|
|
|
using System.Linq;
|
|
|
|
|
using Umbraco.Core;
|
2014-03-04 19:20:36 +11:00
|
|
|
using Umbraco.Core.Configuration;
|
2014-02-26 16:30:25 +01:00
|
|
|
using Umbraco.Core.Logging;
|
|
|
|
|
using Umbraco.Web.Install.Models;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Install.InstallSteps
|
|
|
|
|
{
|
2014-03-04 19:20:36 +11:00
|
|
|
[InstallSetupStep(InstallationType.Upgrade,
|
2014-03-05 20:03:45 +01:00
|
|
|
"DatabaseUpgrade", 12, "")]
|
2014-02-26 16:30:25 +01:00
|
|
|
internal class DatabaseUpgradeStep : InstallSetupStep<object>
|
|
|
|
|
{
|
|
|
|
|
private readonly ApplicationContext _applicationContext;
|
2014-03-04 19:20:36 +11:00
|
|
|
|
|
|
|
|
public DatabaseUpgradeStep(ApplicationContext applicationContext)
|
2014-02-26 16:30:25 +01:00
|
|
|
{
|
|
|
|
|
_applicationContext = applicationContext;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-04 11:16:42 +11:00
|
|
|
public override InstallSetupResult Execute(object model)
|
2014-02-26 16:30:25 +01:00
|
|
|
{
|
2014-03-05 12:21:49 +11:00
|
|
|
var installSteps = InstallStatusTracker.GetStatus().ToArray();
|
|
|
|
|
var previousStep = installSteps.Single(x => x.Name == "DatabaseInstall");
|
2014-02-26 16:30:25 +01:00
|
|
|
var upgrade = previousStep.AdditionalData.ContainsKey("upgrade");
|
|
|
|
|
|
|
|
|
|
if (upgrade)
|
|
|
|
|
{
|
|
|
|
|
LogHelper.Info<DatabaseUpgradeStep>("Running 'Upgrade' service");
|
|
|
|
|
|
|
|
|
|
var result = _applicationContext.DatabaseContext.UpgradeSchemaAndData();
|
|
|
|
|
|
2014-03-13 21:47:08 +11:00
|
|
|
if (result.Success == false)
|
|
|
|
|
{
|
|
|
|
|
return new InstallSetupResult("error",
|
|
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
message = "The database failed to upgrade. ERROR: " + result.Message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-26 16:30:25 +01:00
|
|
|
DatabaseInstallStep.HandleConnectionStrings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
if (_applicationContext.IsConfigured)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-03-05 12:21:49 +11:00
|
|
|
|
|
|
|
|
var installSteps = InstallStatusTracker.GetStatus().ToArray();
|
|
|
|
|
//this step relies on the preious one completed - because it has stored some information we need
|
|
|
|
|
if (installSteps.Any(x => x.Name == "DatabaseInstall" && x.AdditionalData.ContainsKey("upgrade")) == false)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-03-04 19:20:36 +11:00
|
|
|
|
|
|
|
|
var databaseSettings = ConfigurationManager.ConnectionStrings[GlobalSettings.UmbracoConnectionName];
|
|
|
|
|
|
|
|
|
|
if (_applicationContext.DatabaseContext.IsConnectionStringConfigured(databaseSettings))
|
|
|
|
|
{
|
2014-03-05 12:21:49 +11:00
|
|
|
//Since a connection string was present we verify whether this is an upgrade or an empty db
|
|
|
|
|
var result = _applicationContext.DatabaseContext.ValidateDatabaseSchema();
|
2014-03-04 19:20:36 +11:00
|
|
|
|
2014-03-05 12:21:49 +11:00
|
|
|
var determinedVersion = result.DetermineInstalledVersion();
|
|
|
|
|
if (determinedVersion.Equals(new Version(0, 0, 0)))
|
2014-03-04 19:20:36 +11:00
|
|
|
{
|
2014-03-05 12:21:49 +11:00
|
|
|
//Fresh install
|
2014-03-04 19:20:36 +11:00
|
|
|
return false;
|
|
|
|
|
}
|
2014-03-05 12:21:49 +11:00
|
|
|
|
|
|
|
|
//Upgrade
|
|
|
|
|
return true;
|
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
|
|
|
}
|
|
|
|
|
}
|