Fix #4969: add ability to rename the connection string key.
This commit is contained in:
parent
7f3d6ef6a5
commit
bbca9fca89
|
@ -17,6 +17,7 @@ using System.Net.Http.Headers;
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.Generic;
|
||||
using Oqtane.Extensions;
|
||||
|
||||
namespace Oqtane.Controllers
|
||||
{
|
||||
|
@ -56,7 +57,7 @@ namespace Oqtane.Controllers
|
|||
{
|
||||
var installation = new Installation { Success = false, Message = "" };
|
||||
|
||||
if (ModelState.IsValid && (User.IsInRole(RoleNames.Host) || string.IsNullOrEmpty(_configManager.GetSetting($"{SettingKeys.ConnectionStringsSection}:{SettingKeys.ConnectionStringKey}", ""))))
|
||||
if (ModelState.IsValid && (User.IsInRole(RoleNames.Host) || string.IsNullOrEmpty(_configManager.GetSetting($"{SettingKeys.ConnectionStringsSection}:{ConfigUtilities.GetConnectionStringKey()}", ""))))
|
||||
{
|
||||
installation = _databaseManager.Install(config);
|
||||
|
||||
|
|
28
Oqtane.Server/Extensions/ConfigUtilities.cs
Normal file
28
Oqtane.Server/Extensions/ConfigUtilities.cs
Normal file
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Oqtane.Shared;
|
||||
|
||||
namespace Oqtane.Extensions
|
||||
{
|
||||
public sealed class ConfigUtilities
|
||||
{
|
||||
private static IServiceProvider _serviceProvider;
|
||||
|
||||
public static void Configure(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
|
||||
public static string GetConnectionStringKey()
|
||||
{
|
||||
var configManager = _serviceProvider.GetService<Oqtane.Infrastructure.IConfigManager>();
|
||||
if (configManager != null)
|
||||
{
|
||||
return configManager.GetSetting("ConnectionStringKey", SettingKeys.DefaultConnectionStringKey);
|
||||
}
|
||||
|
||||
return SettingKeys.DefaultConnectionStringKey;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ using System.Linq;
|
|||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Oqtane.Extensions;
|
||||
using Oqtane.Shared;
|
||||
|
||||
namespace Oqtane.Infrastructure
|
||||
|
@ -154,7 +155,7 @@ namespace Oqtane.Infrastructure
|
|||
|
||||
public string GetConnectionString()
|
||||
{
|
||||
return _config.GetConnectionString(SettingKeys.ConnectionStringKey);
|
||||
return _config.GetConnectionString(ConfigUtilities.GetConnectionStringKey());
|
||||
}
|
||||
|
||||
public string GetConnectionString(string name)
|
||||
|
|
|
@ -43,7 +43,7 @@ namespace Oqtane.Infrastructure
|
|||
{
|
||||
var result = new Installation { Success = false, Message = string.Empty };
|
||||
|
||||
if (!string.IsNullOrEmpty(_config.GetConnectionString(SettingKeys.ConnectionStringKey)))
|
||||
if (!string.IsNullOrEmpty(_config.GetConnectionString(ConfigUtilities.GetConnectionStringKey())))
|
||||
{
|
||||
using (var db = GetInstallationContext())
|
||||
{
|
||||
|
@ -94,7 +94,7 @@ namespace Oqtane.Infrastructure
|
|||
// startup or silent installation
|
||||
install = new InstallConfig
|
||||
{
|
||||
ConnectionString = _config.GetConnectionString(SettingKeys.ConnectionStringKey),
|
||||
ConnectionString = _config.GetConnectionString(ConfigUtilities.GetConnectionStringKey()),
|
||||
TenantName = TenantNames.Master,
|
||||
DatabaseType = _config.GetSection(SettingKeys.DatabaseSection)[SettingKeys.DatabaseTypeKey],
|
||||
IsNewTenant = false
|
||||
|
@ -301,7 +301,7 @@ namespace Oqtane.Infrastructure
|
|||
tenant = new Tenant
|
||||
{
|
||||
Name = install.TenantName,
|
||||
DBConnectionString = (install.TenantName == TenantNames.Master) ? SettingKeys.ConnectionStringKey : install.TenantName,
|
||||
DBConnectionString = (install.TenantName == TenantNames.Master) ? ConfigUtilities.GetConnectionStringKey() : install.TenantName,
|
||||
DBType = install.DatabaseType,
|
||||
CreatedBy = "",
|
||||
CreatedOn = DateTime.UtcNow,
|
||||
|
@ -637,7 +637,7 @@ namespace Oqtane.Infrastructure
|
|||
|
||||
private InstallationContext GetInstallationContext()
|
||||
{
|
||||
var connectionString = NormalizeConnectionString(_config.GetConnectionString(SettingKeys.ConnectionStringKey));
|
||||
var connectionString = NormalizeConnectionString(_config.GetConnectionString(ConfigUtilities.GetConnectionStringKey()));
|
||||
var databaseType = _config.GetSection(SettingKeys.DatabaseSection)[SettingKeys.DatabaseTypeKey];
|
||||
|
||||
IDatabase database = null;
|
||||
|
@ -665,9 +665,9 @@ namespace Oqtane.Infrastructure
|
|||
public void UpdateConnectionString(string connectionString)
|
||||
{
|
||||
connectionString = DenormalizeConnectionString(connectionString);
|
||||
if (_config.GetConnectionString(SettingKeys.ConnectionStringKey) != connectionString)
|
||||
if (_config.GetConnectionString(ConfigUtilities.GetConnectionStringKey()) != connectionString)
|
||||
{
|
||||
_configManager.AddOrUpdateSetting($"{SettingKeys.ConnectionStringsSection}:{SettingKeys.ConnectionStringKey}", connectionString, true);
|
||||
_configManager.AddOrUpdateSetting($"{SettingKeys.ConnectionStringsSection}:{ConfigUtilities.GetConnectionStringKey()}", connectionString, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -695,10 +695,10 @@ namespace Oqtane.Infrastructure
|
|||
// migrate connection strings from the Tenant table to appsettings
|
||||
if (tenant.DBConnectionString.Contains("="))
|
||||
{
|
||||
var defaultConnection = _configManager.GetConnectionString(SettingKeys.ConnectionStringKey);
|
||||
var defaultConnection = _configManager.GetConnectionString(ConfigUtilities.GetConnectionStringKey());
|
||||
if (tenant.DBConnectionString == defaultConnection)
|
||||
{
|
||||
tenant.DBConnectionString = SettingKeys.ConnectionStringKey;
|
||||
tenant.DBConnectionString = ConfigUtilities.GetConnectionStringKey();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -6,6 +6,7 @@ using Microsoft.Extensions.DependencyInjection;
|
|||
using Oqtane.Infrastructure;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Oqtane.Documentation;
|
||||
using Oqtane.Extensions;
|
||||
|
||||
namespace Oqtane.Server
|
||||
{
|
||||
|
@ -15,6 +16,7 @@ namespace Oqtane.Server
|
|||
public static void Main(string[] args)
|
||||
{
|
||||
var host = BuildWebHost(args);
|
||||
ConfigUtilities.Configure(host.Services);
|
||||
var databaseManager = host.Services.GetService<IDatabaseManager>();
|
||||
var install = databaseManager.Install();
|
||||
if (!string.IsNullOrEmpty(install.Message))
|
||||
|
|
|
@ -41,7 +41,7 @@ namespace Oqtane.Repository
|
|||
{
|
||||
if (_config.IsInstalled())
|
||||
{
|
||||
_connectionString = _config.GetConnectionString(SettingKeys.ConnectionStringKey)
|
||||
_connectionString = _config.GetConnectionString(ConfigUtilities.GetConnectionStringKey())
|
||||
.Replace($"|{Constants.DataDirectory}|", AppDomain.CurrentDomain.GetData(Constants.DataDirectory)?.ToString());
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
using System;
|
||||
|
||||
namespace Oqtane.Shared
|
||||
{
|
||||
public static class SettingKeys
|
||||
|
@ -6,7 +8,7 @@ namespace Oqtane.Shared
|
|||
public const string DatabaseTypeKey = "DefaultDBType";
|
||||
|
||||
public const string ConnectionStringsSection = "ConnectionStrings";
|
||||
public const string ConnectionStringKey = "DefaultConnection";
|
||||
public const string DefaultConnectionStringKey = "DefaultConnection";
|
||||
|
||||
public const string InstallationSection = "Installation";
|
||||
public const string DefaultAliasKey = "DefaultAlias";
|
||||
|
@ -21,5 +23,8 @@ namespace Oqtane.Shared
|
|||
public const string AvailableDatabasesSection = "AvailableDatabases";
|
||||
|
||||
public const string TestModeKey = "TestMode"; // optional - used for testing run-time characteristics
|
||||
|
||||
[Obsolete("Please use Oqtane.Extensions.ConfigUtilities.GetConnectionStringKey instead.")]
|
||||
public const string ConnectionStringKey = "DefaultConnection";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user