Skip to content

Commit

Permalink
Merge pull request #3518 from sbwalker/dev
Browse files Browse the repository at this point in the history
add defensive logic to handle scenario where a tenant connection string does not exist in appsettings.json
  • Loading branch information
sbwalker authored Nov 29, 2023
2 parents 73aca22 + 85f9597 commit 5db61b7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Oqtane.Server/Infrastructure/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ private Installation MigrateTenants(InstallConfig install)
}
catch (Exception ex)
{
result.Message = "An Error Occurred Executing Upgrade Logic. " + ex.ToString();
result.Message = "An Error Occurred Executing Upgrade Logic On Tenant " + tenant.Name + ". " + ex.ToString();
_filelogger.LogError(Utilities.LogMessage(this, result.Message));
}
}
Expand Down Expand Up @@ -468,7 +468,7 @@ private Installation MigrateModules(InstallConfig install)
}
catch (Exception ex)
{
result.Message = "An Error Occurred Installing " + moduleDefinition.Name + " Version " + versions[i] + " - " + ex.ToString();
result.Message = "An Error Occurred Installing " + moduleDefinition.Name + " Version " + versions[i] + " On Tenant " + tenant.Name + " - " + ex.ToString();
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions Oqtane.Server/Repository/Context/DBContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,20 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
Tenant tenant = _tenantManager.GetTenant();
if (tenant != null)
{
_connectionString = _config.GetConnectionString(tenant.DBConnectionString)
.Replace($"|{Constants.DataDirectory}|", AppDomain.CurrentDomain.GetData(Constants.DataDirectory)?.ToString());
_databaseType = tenant.DBType;
_connectionString = _config.GetConnectionString(tenant.DBConnectionString);
if (_connectionString != null)
{
_connectionString = _connectionString.Replace($"|{Constants.DataDirectory}|", AppDomain.CurrentDomain.GetData(Constants.DataDirectory)?.ToString());
_databaseType = tenant.DBType;
}
else
{
// tenant connection string does not exist in appsettings.json
}
}
}

if (!String.IsNullOrEmpty(_databaseType))
if (!string.IsNullOrEmpty(_databaseType))
{
var type = Type.GetType(_databaseType);
ActiveDatabase = Activator.CreateInstance(type) as IDatabase;
Expand Down
10 changes: 9 additions & 1 deletion Oqtane.Server/Repository/SqlRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,15 @@ public IDataReader ExecuteReader(string DBType, string DBConnectionString, strin
public int ExecuteNonQuery(string connectionString, string databaseType, string query)
{
var db = GetActiveDatabase(databaseType);
return db.ExecuteNonQuery(GetConnectionString(connectionString), query);
var connectionstring = GetConnectionString(connectionString);
if (connectionstring != null)
{
return db.ExecuteNonQuery(GetConnectionString(connectionString), query);
}
else
{
return 0;
}
}

public string GetScriptFromAssembly(Assembly assembly, string fileName)
Expand Down

0 comments on commit 5db61b7

Please sign in to comment.