Skip to content

Commit 8e6ae98

Browse files
Merge pull request #519 from PHOENIXCONTACT/fix/path-creation-database-controller
Using Path.Combine for creating backup-path in DatabaseController
2 parents b59b095 + f02ba92 commit 8e6ae98

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/Moryx.Runtime.Endpoints/Databases/Endpoint/DatabaseController.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG
1+
// Copyright (c) 2025, Phoenix Contact GmbH & Co. KG
22
// Licensed under the Apache License, Version 2.0
33

44
using System;
@@ -30,7 +30,13 @@ public class DatabaseController : ControllerBase
3030
{
3131
private readonly IDbContextManager _dbContextManager;
3232
private readonly IDatabaseConfigUpdateService _databaseUpdateService;
33-
private readonly string _dataDirectory = @".\Backups\";
33+
private static readonly string DataDirectory;
34+
35+
static DatabaseController()
36+
{
37+
var executingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
38+
DataDirectory = Path.Combine(executingDirectory!, "Backups");
39+
}
3440

3541
public DatabaseController(IDbContextManager dbContextManager)
3642
{
@@ -183,7 +189,7 @@ public ActionResult<InvocationResponse> DumpDatabase(string targetModel, Databas
183189
return BadConfigValues();
184190

185191

186-
var targetPath = Path.Combine(_dataDirectory, targetModel);
192+
var targetPath = Path.Combine(DataDirectory, targetModel);
187193
if (!Directory.Exists(targetPath))
188194
Directory.CreateDirectory(targetPath);
189195

@@ -204,7 +210,7 @@ public ActionResult<InvocationResponse> RestoreDatabase(string targetModel, Rest
204210
if (!IsConfigValid(updatedConfig))
205211
return BadConfigValues();
206212

207-
var filePath = Path.Combine(_dataDirectory, targetModel, request.BackupFileName);
213+
var filePath = Path.Combine(DataDirectory, targetModel, request.BackupFileName);
208214
targetConfigurator.RestoreDatabase(updatedConfig, filePath);
209215

210216
return new InvocationResponse();
@@ -355,7 +361,7 @@ private IEnumerable<SetupModel> GetAllSetups(Type contextType)
355361
var setups = allSetups.Where(setup => string.IsNullOrEmpty(setup.SupportedFileRegex))
356362
.Select(ConvertSetup).OrderBy(setup => setup.SortOrder).ToList();
357363
string[] files;
358-
if (!Directory.Exists(_dataDirectory) || !(files = Directory.GetFiles(_dataDirectory)).Any())
364+
if (!Directory.Exists(DataDirectory) || !(files = Directory.GetFiles(DataDirectory)).Any())
359365
return setups.ToArray();
360366

361367
var fileSetups = allSetups.Where(setup => !string.IsNullOrEmpty(setup.SupportedFileRegex))
@@ -372,7 +378,7 @@ private IEnumerable<SetupModel> GetAllSetups(Type contextType)
372378
private IEnumerable<BackupModel> GetAllBackups(Type contextType)
373379
{
374380
var targetModel = TargetModelName(contextType);
375-
var backupFolder = Path.Combine(_dataDirectory, targetModel);
381+
var backupFolder = Path.Combine(DataDirectory, targetModel);
376382

377383
if (!Directory.Exists(backupFolder))
378384
return Array.Empty<BackupModel>();

0 commit comments

Comments
 (0)