Skip to content

Commit ff85389

Browse files
author
Brian Cummings
committed
Use separate admin connection string for database migrations
- Add PathfinderMigrationCS connection string for migrations - Update MigrationService to use admin credentials for schema changes - Update GitHub workflow to inject PROD_ADMIN_CONNECTIONSTRING secret - Normal app operations continue to use restricted user credentials - Fixes permission issues with ALTER TABLE operations
1 parent 1db7b8d commit ff85389

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

.github/workflows/main_pathfinderhonormanager.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ jobs:
105105
files: '${{env.DOTNET_ROOT}}/myapp/appsettings.json'
106106
env:
107107
ConnectionStrings.PathfinderCS: ${{ secrets.PRODCONNECTIONSTRING }}
108+
ConnectionStrings.PathfinderMigrationCS: ${{ secrets.PROD_ADMIN_CONNECTIONSTRING }}
108109
App.ServerRootAddress: ${{ env.SERVER_ROOT_ADDRESS }}
109110

110111
- name: Upload artifact for deployment job

PathfinderHonorManager/Service/MigrationService.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Configuration;
23
using Microsoft.Extensions.DependencyInjection;
34
using Microsoft.Extensions.Hosting;
45
using Microsoft.Extensions.Logging;
@@ -14,25 +15,39 @@ public class MigrationService : IHostedService
1415
{
1516
private readonly IServiceProvider _serviceProvider;
1617
private readonly ILogger<MigrationService> _logger;
18+
private readonly IConfiguration _configuration;
1719

18-
public MigrationService(IServiceProvider serviceProvider, ILogger<MigrationService> logger)
20+
public MigrationService(IServiceProvider serviceProvider, ILogger<MigrationService> logger, IConfiguration configuration)
1921
{
2022
_serviceProvider = serviceProvider;
2123
_logger = logger;
24+
_configuration = configuration;
2225
}
2326

2427
public async Task StartAsync(CancellationToken cancellationToken)
2528
{
2629
_logger.LogInformation("Starting database migration check...");
2730

2831
using var scope = _serviceProvider.CreateScope();
29-
var context = scope.ServiceProvider.GetRequiredService<PathfinderContext>();
32+
33+
var migrationConnectionString = _configuration.GetConnectionString("PathfinderMigrationCS");
34+
var optionsBuilder = new DbContextOptionsBuilder<PathfinderContext>();
35+
optionsBuilder.UseNpgsql(migrationConnectionString,
36+
npgsqlOptions =>
37+
{
38+
npgsqlOptions.CommandTimeout(60);
39+
npgsqlOptions.EnableRetryOnFailure(
40+
maxRetryCount: 3,
41+
maxRetryDelay: TimeSpan.FromSeconds(10),
42+
errorCodesToAdd: null);
43+
});
44+
45+
var context = new PathfinderContext(optionsBuilder.Options);
3046

3147
try
3248
{
33-
var connectionString = context.Database.GetConnectionString();
34-
var builder = new Npgsql.NpgsqlConnectionStringBuilder(connectionString);
35-
_logger.LogDebug("Connecting to database: Host={Host}, Database={Database}, Username={Username}",
49+
var builder = new Npgsql.NpgsqlConnectionStringBuilder(migrationConnectionString);
50+
_logger.LogDebug("Connecting to database for migrations: Host={Host}, Database={Database}, Username={Username}",
3651
builder.Host, builder.Database, builder.Username);
3752

3853
var canConnect = await context.Database.CanConnectAsync(cancellationToken);

PathfinderHonorManager/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"https_port": 443,
33
"ConnectionStrings": {
4-
"PathfinderCS": "defaultConnection"
4+
"PathfinderCS": "defaultConnection",
5+
"PathfinderMigrationCS": "defaultMigrationConnection"
56
},
67
"Logging": {
78
"LogLevel": {

0 commit comments

Comments
 (0)