2014-02-26 16:01:31 +01:00
|
|
|
|
using System;
|
2020-05-06 20:37:03 +02:00
|
|
|
|
using System.Collections.Generic;
|
2019-01-11 14:30:04 +11:00
|
|
|
|
using System.Threading.Tasks;
|
2014-02-26 16:01:31 +01:00
|
|
|
|
using Umbraco.Core;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
using Umbraco.Core.Logging;
|
2017-12-18 18:26:32 +01:00
|
|
|
|
using Umbraco.Core.Migrations.Install;
|
2014-02-26 16:01:31 +01:00
|
|
|
|
using Umbraco.Web.Install.Models;
|
2020-08-21 15:27:06 +01:00
|
|
|
|
using Umbraco.Core.Configuration.Models;
|
2020-08-21 14:52:47 +01:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2014-02-26 16:01:31 +01:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Install.InstallSteps
|
|
|
|
|
|
{
|
2014-03-04 19:20:36 +11:00
|
|
|
|
[InstallSetupStep(InstallationType.NewInstall,
|
2014-03-05 20:03:45 +01:00
|
|
|
|
"DatabaseConfigure", "database", 10, "Setting up a database, so Umbraco has a place to store your website",
|
2014-03-05 11:34:42 +11:00
|
|
|
|
PerformsAppRestart = true)]
|
2020-02-17 14:58:34 +01:00
|
|
|
|
public class DatabaseConfigureStep : InstallSetupStep<DatabaseModel>
|
2014-02-26 16:01:31 +01:00
|
|
|
|
{
|
2016-11-29 10:31:25 +01:00
|
|
|
|
private readonly DatabaseBuilder _databaseBuilder;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
private readonly ILogger _logger;
|
2020-08-21 14:52:47 +01:00
|
|
|
|
private readonly ConnectionStrings _connectionStrings;
|
2014-02-26 16:01:31 +01:00
|
|
|
|
|
2020-08-23 23:36:48 +02:00
|
|
|
|
public DatabaseConfigureStep(DatabaseBuilder databaseBuilder, IOptions<ConnectionStrings> connectionStrings)
|
2014-02-26 16:01:31 +01:00
|
|
|
|
{
|
2016-11-29 10:31:25 +01:00
|
|
|
|
_databaseBuilder = databaseBuilder;
|
2020-08-21 14:52:47 +01:00
|
|
|
|
_connectionStrings = connectionStrings.Value ?? throw new ArgumentNullException(nameof(connectionStrings));
|
2014-02-26 16:01:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-11 14:30:04 +11:00
|
|
|
|
public override Task<InstallSetupResult> ExecuteAsync(DatabaseModel database)
|
2014-02-26 16:01:31 +01:00
|
|
|
|
{
|
2014-03-05 12:08:35 +11:00
|
|
|
|
//if the database model is null then we will apply the defaults
|
|
|
|
|
|
if (database == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
database = new DatabaseModel();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-12-18 10:39:39 +01:00
|
|
|
|
if (_databaseBuilder.CanConnect(database.DatabaseType.ToString(), database.ConnectionString, database.Server, database.DatabaseName, database.Login, database.Password, database.IntegratedAuth) == false)
|
2014-02-26 16:01:31 +01:00
|
|
|
|
{
|
2014-03-20 17:35:51 +11:00
|
|
|
|
throw new InstallException("Could not connect to the database");
|
2014-02-26 16:01:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
ConfigureConnection(database);
|
2019-01-11 14:30:04 +11:00
|
|
|
|
return Task.FromResult<InstallSetupResult>(null);
|
2014-02-26 16:01:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ConfigureConnection(DatabaseModel database)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (database.ConnectionString.IsNullOrWhiteSpace() == false)
|
|
|
|
|
|
{
|
2016-11-29 10:31:25 +01:00
|
|
|
|
_databaseBuilder.ConfigureDatabaseConnection(database.ConnectionString);
|
2014-02-26 16:01:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if (database.DatabaseType == DatabaseType.SqlCe)
|
|
|
|
|
|
{
|
2016-11-29 10:31:25 +01:00
|
|
|
|
_databaseBuilder.ConfigureEmbeddedDatabaseConnection();
|
2014-02-26 16:01:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
else if (database.IntegratedAuth)
|
|
|
|
|
|
{
|
2016-11-29 10:31:25 +01:00
|
|
|
|
_databaseBuilder.ConfigureIntegratedSecurityDatabaseConnection(database.Server, database.DatabaseName);
|
2014-02-26 16:01:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2019-05-01 05:54:35 +10:00
|
|
|
|
var password = database.Password.Replace("'", "''");
|
2016-07-21 11:24:08 +02:00
|
|
|
|
password = string.Format("'{0}'", password);
|
2016-06-16 06:09:59 -06:00
|
|
|
|
|
2016-11-29 10:31:25 +01:00
|
|
|
|
_databaseBuilder.ConfigureDatabaseConnection(
|
2016-06-16 06:09:59 -06:00
|
|
|
|
database.Server, database.DatabaseName, database.Login, password,
|
2014-02-26 16:01:31 +01:00
|
|
|
|
database.DatabaseType.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-05-06 20:37:03 +02:00
|
|
|
|
public override object ViewModel
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
var databases = new List<object>()
|
|
|
|
|
|
{
|
|
|
|
|
|
new { name = "Microsoft SQL Server", id = 1 },
|
|
|
|
|
|
new { name = "Microsoft SQL Azure", id = 3 },
|
|
|
|
|
|
new { name = "Custom connection string", id = -1 },
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (IsSqlCeAvailable())
|
|
|
|
|
|
{
|
|
|
|
|
|
databases.Insert(0, new { name = "Microsoft SQL Server Compact (SQL CE)", id = 0 });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new
|
|
|
|
|
|
{
|
|
|
|
|
|
databases = databases
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool IsSqlCeAvailable()
|
|
|
|
|
|
{
|
2020-05-08 16:35:37 +10:00
|
|
|
|
// NOTE: Type.GetType will only return types that are currently loaded into the appdomain. In this case
|
|
|
|
|
|
// that is ok because we know if this is availalbe we will have manually loaded it into the appdomain.
|
|
|
|
|
|
// Else we'd have to use Assembly.LoadFrom and need to know the DLL location here which we don't need to do.
|
2020-05-06 20:37:03 +02:00
|
|
|
|
return !(Type.GetType("Umbraco.Persistance.SqlCe.SqlCeSyntaxProvider, Umbraco.Persistance.SqlCe") is null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-29 10:31:25 +01:00
|
|
|
|
public override string View => ShouldDisplayView() ? base.View : "";
|
2014-02-26 16:30:25 +01:00
|
|
|
|
|
2020-05-06 20:37:03 +02:00
|
|
|
|
|
2014-03-05 14:30:17 +11:00
|
|
|
|
public override bool RequiresExecution(DatabaseModel model)
|
2014-03-04 11:16:42 +11:00
|
|
|
|
{
|
2014-03-04 19:20:36 +11:00
|
|
|
|
return ShouldDisplayView();
|
2014-03-04 11:16:42 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-02-26 16:30:25 +01:00
|
|
|
|
private bool ShouldDisplayView()
|
|
|
|
|
|
{
|
|
|
|
|
|
//If the connection string is already present in web.config we don't need to show the settings page and we jump to installing/upgrading.
|
2020-01-21 17:03:46 -08:00
|
|
|
|
var databaseSettings = _connectionStrings[Constants.System.UmbracoConnectionName];
|
2014-02-26 16:30:25 +01:00
|
|
|
|
|
2019-11-18 14:11:50 +01:00
|
|
|
|
if (databaseSettings.IsConnectionStringConfigured())
|
2014-02-26 16:30:25 +01:00
|
|
|
|
{
|
2014-03-04 19:20:36 +11:00
|
|
|
|
try
|
2014-02-26 16:30:25 +01:00
|
|
|
|
{
|
2014-03-04 19:20:36 +11:00
|
|
|
|
//Since a connection string was present we verify the db can connect and query
|
2018-12-18 10:39:39 +01:00
|
|
|
|
_ = _databaseBuilder.ValidateSchema();
|
2014-02-26 16:30:25 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2017-09-08 19:39:13 +02:00
|
|
|
|
catch (Exception ex)
|
2014-02-26 16:30:25 +01:00
|
|
|
|
{
|
2018-08-17 15:41:58 +01:00
|
|
|
|
_logger.Error<DatabaseConfigureStep>(ex, "An error occurred, reconfiguring...");
|
2014-03-04 19:20:36 +11:00
|
|
|
|
//something went wrong, could not connect so probably need to reconfigure
|
|
|
|
|
|
return true;
|
2014-02-26 16:30:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-03-04 19:20:36 +11:00
|
|
|
|
|
|
|
|
|
|
return true;
|
2014-02-26 16:01:31 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|