Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace ServiceControl.Persistence.EFCore.PostgreSql;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ServiceControl.Persistence.EFCore.Abstractions;
using ServiceControl.Persistence.EFCore.DbContexts;

class PostgreSqlDatabaseMigrator(ServiceControlDbContext dbContext, ILogger<PostgreSqlDatabaseMigrator> logger) : IDatabaseMigrator
{
public async Task ApplyMigrations(CancellationToken cancellationToken = default)
{
logger.LogInformation("Starting PostgreSQL database migration");

var previousTimeout = dbContext.Database.GetCommandTimeout();
dbContext.Database.SetCommandTimeout(EFPersisterSettings.MigrationCommandTimeout);

await dbContext.Database.MigrateAsync(cancellationToken);

dbContext.Database.SetCommandTimeout(previousTimeout);

logger.LogInformation("PostgreSQL database migration completed");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace ServiceControl.Persistence.EFCore.PostgreSql;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using ServiceControl.Persistence.EFCore.Abstractions;
using ServiceControl.Persistence.EFCore.DbContexts;

class PostgreSqlPersistence(PostgreSqlPersisterSettings settings) : BasePersistence, IPersistence
{
Expand All @@ -17,6 +18,8 @@ public void AddInstaller(IServiceCollection services)
{
RegisterSettings(services);
ConfigureDbContext(services);

services.AddScoped<IDatabaseMigrator, PostgreSqlDatabaseMigrator>();
}

void RegisterSettings(IServiceCollection services)
Expand All @@ -26,7 +29,28 @@ void RegisterSettings(IServiceCollection services)
services.AddSingleton(settings);
}

void ConfigureDbContext(IServiceCollection services) =>
services.AddPooledDbContextFactory<PostgreSqlServiceControlDbContext>(options =>
options.UseNpgsql(settings.ConnectionString, npgsql => npgsql.CommandTimeout(settings.CommandTimeout)));
void ConfigureDbContext(IServiceCollection services)
{
services.AddDbContext<PostgreSqlServiceControlDbContext>((serviceProvider, options) =>
{
options.UseNpgsql(settings.ConnectionString, npgsqlOptions =>
{
npgsqlOptions.CommandTimeout(settings.CommandTimeout);
if (settings.EnableRetryOnFailure)
{
npgsqlOptions.EnableRetryOnFailure(
maxRetryCount: settings.MaxRetryCount,
maxRetryDelay: TimeSpan.FromSeconds(settings.MaxRetryDelayInSeconds),
errorCodesToAdd: null);
}
});

if (settings.EnableSensitiveDataLogging)
{
options.EnableSensitiveDataLogging();
}
}, ServiceLifetime.Scoped);

services.AddScoped<ServiceControlDbContext>(provider => provider.GetRequiredService<PostgreSqlServiceControlDbContext>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace ServiceControl.Persistence.EFCore.SqlServer;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ServiceControl.Persistence.EFCore.Abstractions;
using ServiceControl.Persistence.EFCore.DbContexts;

class SqlServerDatabaseMigrator(ServiceControlDbContext dbContext, ILogger<SqlServerDatabaseMigrator> logger) : IDatabaseMigrator
{
public async Task ApplyMigrations(CancellationToken cancellationToken = default)
{
logger.LogInformation("Starting SQL Server database migration");
Comment thread
johnsimons marked this conversation as resolved.

var previousTimeout = dbContext.Database.GetCommandTimeout();
dbContext.Database.SetCommandTimeout(EFPersisterSettings.MigrationCommandTimeout);

await dbContext.Database.MigrateAsync(cancellationToken);

dbContext.Database.SetCommandTimeout(previousTimeout);

logger.LogInformation("SQL Server database migration completed");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace ServiceControl.Persistence.EFCore.SqlServer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using ServiceControl.Persistence.EFCore.Abstractions;
using ServiceControl.Persistence.EFCore.DbContexts;

class SqlServerPersistence(SqlServerPersisterSettings settings) : BasePersistence, IPersistence
{
Expand All @@ -17,6 +18,8 @@ public void AddInstaller(IServiceCollection services)
{
RegisterSettings(services);
ConfigureDbContext(services);

services.AddScoped<IDatabaseMigrator, SqlServerDatabaseMigrator>();
}

void RegisterSettings(IServiceCollection services)
Expand All @@ -26,7 +29,28 @@ void RegisterSettings(IServiceCollection services)
services.AddSingleton(settings);
}

void ConfigureDbContext(IServiceCollection services) =>
services.AddPooledDbContextFactory<SqlServerServiceControlDbContext>(options =>
options.UseSqlServer(settings.ConnectionString, sqlServer => sqlServer.CommandTimeout(settings.CommandTimeout)));
void ConfigureDbContext(IServiceCollection services)
{
services.AddDbContext<SqlServerServiceControlDbContext>((serviceProvider, options) =>
{
options.UseSqlServer(settings.ConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(settings.CommandTimeout);
if (settings.EnableRetryOnFailure)
{
sqlOptions.EnableRetryOnFailure(
maxRetryCount: settings.MaxRetryCount,
maxRetryDelay: TimeSpan.FromSeconds(settings.MaxRetryDelayInSeconds),
errorNumbersToAdd: null);
}
});

if (settings.EnableSensitiveDataLogging)
{
options.EnableSensitiveDataLogging();
}
}, ServiceLifetime.Scoped);

services.AddScoped<ServiceControlDbContext>(provider => provider.GetRequiredService<SqlServerServiceControlDbContext>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ namespace ServiceControl.Persistence.EFCore.Abstractions;

public abstract class EFPersisterSettings : PersistenceSettings
{
public static readonly TimeSpan MigrationCommandTimeout = TimeSpan.FromMinutes(40);

public required string ConnectionString { get; set; }
public int CommandTimeout { get; set; } = 30;
public TimeSpan ErrorRetentionPeriod { get; set; }
public string? MessageBodyStoragePath { get; set; }
public int MinBodySizeForCompression { get; set; } = 4096;
public int MaxRetryCount { get; set; } = 5;
public int MaxRetryDelayInSeconds { get; set; } = 30;
public bool EnableSensitiveDataLogging { get; set; }
public bool EnableRetryOnFailure { get; set; } = true;
}
9 changes: 9 additions & 0 deletions src/ServiceControl.Persistence/IDatabaseMigrator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ServiceControl.Persistence;

using System.Threading;
using System.Threading.Tasks;

public interface IDatabaseMigrator
{
Task ApplyMigrations(CancellationToken cancellationToken = default);
}
10 changes: 10 additions & 0 deletions src/ServiceControl/Hosting/Commands/SetupCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
{
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Particular.ServiceControl;
using Particular.ServiceControl.Hosting;
using ServiceBus.Management.Infrastructure.Installers;
using ServiceBus.Management.Infrastructure.Settings;
using ServiceControl.Infrastructure;
using ServiceControl.Persistence;
using Transports;

class SetupCommand : AbstractCommand
Expand Down Expand Up @@ -47,6 +49,14 @@ public override async Task Execute(HostArguments args, Settings settings)
await transportCustomization.ProvisionQueues(transportSettings, componentSetupContext.Queues);
}

await using (var scope = host.Services.CreateAsyncScope())
{
if (scope.ServiceProvider.GetService<IDatabaseMigrator>() is { } databaseMigrator)
{
await databaseMigrator.ApplyMigrations();
}
}

await host.StopAsync();
}
}
Expand Down