Skip to content

Commit

Permalink
Database Migrator: Make database command timeout configurable (#626)
Browse files Browse the repository at this point in the history
* feat: make CommandTimeout configurable in modules

* feat: make CommandTimeout configurable in DatabaseMigrator

* chore: fix warning
  • Loading branch information
tnotheis authored Apr 26, 2024
1 parent d41de55 commit d7911f9
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static IServiceCollection AddDatabase(this IServiceCollection services, A
case SQLSERVER:
dbContextOptions.UseSqlServer(options.ConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(SQLSERVER_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "AdminUi");
Expand All @@ -45,7 +45,7 @@ public static IServiceCollection AddDatabase(this IServiceCollection services, A
case POSTGRES:
dbContextOptions.UseNpgsql(options.ConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(POSTGRES_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "AdminUi");
Expand All @@ -63,6 +63,7 @@ public class DbOptions
{
public string Provider { get; set; } = null!;
public string ConnectionString { get; set; } = null!;
public int CommandTimeout { get; set; } = 20;
public RetryOptions RetryOptions { get; set; } = new();
}

Expand Down
3 changes: 3 additions & 0 deletions DatabaseMigrator/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ public class SqlDatabaseConfiguration

[Required]
public string ConnectionString { get; set; } = null!;

[Range(1, int.MaxValue)]
public int CommandTimeout { get; set; } = 300;
}
9 changes: 9 additions & 0 deletions DatabaseMigrator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,54 +71,63 @@ static IHostBuilder CreateHostBuilder(string[] args)
{
options.Provider = parsedConfiguration.Infrastructure.SqlDatabase.Provider;
options.DbConnectionString = parsedConfiguration.Infrastructure.SqlDatabase.ConnectionString;
options.CommandTimeout = parsedConfiguration.Infrastructure.SqlDatabase.CommandTimeout;
});

Backbone.Modules.Devices.Infrastructure.Persistence.IServiceCollectionExtensions.AddDatabase(services, options =>
{
options.Provider = parsedConfiguration.Infrastructure.SqlDatabase.Provider;
options.ConnectionString = parsedConfiguration.Infrastructure.SqlDatabase.ConnectionString;
options.CommandTimeout = parsedConfiguration.Infrastructure.SqlDatabase.CommandTimeout;
});

Backbone.Modules.Files.Infrastructure.Persistence.Database.IServiceCollectionExtensions.AddDatabase(services, options =>
{
options.Provider = parsedConfiguration.Infrastructure.SqlDatabase.Provider;
options.DbConnectionString = parsedConfiguration.Infrastructure.SqlDatabase.ConnectionString;
options.CommandTimeout = parsedConfiguration.Infrastructure.SqlDatabase.CommandTimeout;
});

Backbone.Modules.Messages.Infrastructure.Persistence.Database.IServiceCollectionExtensions.AddDatabase(services, options =>
{
options.Provider = parsedConfiguration.Infrastructure.SqlDatabase.Provider;
options.DbConnectionString = parsedConfiguration.Infrastructure.SqlDatabase.ConnectionString;
options.CommandTimeout = parsedConfiguration.Infrastructure.SqlDatabase.CommandTimeout;
});

Backbone.Modules.Quotas.Infrastructure.Persistence.Database.IServiceCollectionExtensions.AddDatabase(services, options =>
{
options.Provider = parsedConfiguration.Infrastructure.SqlDatabase.Provider;
options.DbConnectionString = parsedConfiguration.Infrastructure.SqlDatabase.ConnectionString;
options.CommandTimeout = parsedConfiguration.Infrastructure.SqlDatabase.CommandTimeout;
});

Backbone.Modules.Relationships.Infrastructure.Persistence.Database.IServiceCollectionExtensions.AddDatabase(services, options =>
{
options.Provider = parsedConfiguration.Infrastructure.SqlDatabase.Provider;
options.DbConnectionString = parsedConfiguration.Infrastructure.SqlDatabase.ConnectionString;
options.CommandTimeout = parsedConfiguration.Infrastructure.SqlDatabase.CommandTimeout;
});

Backbone.Modules.Synchronization.Infrastructure.Persistence.Database.IServiceCollectionExtensions.AddDatabase(services, options =>
{
options.Provider = parsedConfiguration.Infrastructure.SqlDatabase.Provider;
options.DbConnectionString = parsedConfiguration.Infrastructure.SqlDatabase.ConnectionString;
options.CommandTimeout = parsedConfiguration.Infrastructure.SqlDatabase.CommandTimeout;
});

Backbone.Modules.Tokens.Infrastructure.Persistence.Database.IServiceCollectionExtensions.AddDatabase(services, options =>
{
options.Provider = parsedConfiguration.Infrastructure.SqlDatabase.Provider;
options.DbConnectionString = parsedConfiguration.Infrastructure.SqlDatabase.ConnectionString;
options.CommandTimeout = parsedConfiguration.Infrastructure.SqlDatabase.CommandTimeout;
});

Backbone.AdminApi.Infrastructure.Persistence.IServiceCollectionExtensions.AddDatabase(services, options =>
{
options.Provider = parsedConfiguration.Infrastructure.SqlDatabase.Provider;
options.ConnectionString = parsedConfiguration.Infrastructure.SqlDatabase.ConnectionString;
options.CommandTimeout = parsedConfiguration.Infrastructure.SqlDatabase.CommandTimeout;
});

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void AddDatabase(this IServiceCollection services, Action<DbOption
case SQLSERVER:
dbContextOptions.UseSqlServer(options.DbConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(SQLSERVER_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Challenges");
Expand All @@ -36,7 +36,7 @@ public static void AddDatabase(this IServiceCollection services, Action<DbOption
case POSTGRES:
dbContextOptions.UseNpgsql(options.DbConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(POSTGRES_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Challenges");
Expand All @@ -54,6 +54,7 @@ public class DbOptions
{
public string Provider { get; set; } = null!;
public string DbConnectionString { get; set; } = null!;
public int CommandTimeout { get; set; } = 20;
public RetryOptions RetryOptions { get; set; } = new();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void AddDatabase(this IServiceCollection services, Action<DbOption
case SQLSERVER:
dbContextOptions.UseSqlServer(options.ConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(SQLSERVER_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Devices");
Expand All @@ -38,7 +38,7 @@ public static void AddDatabase(this IServiceCollection services, Action<DbOption
case POSTGRES:
dbContextOptions.UseNpgsql(options.ConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(POSTGRES_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Devices");
Expand All @@ -47,6 +47,7 @@ public static void AddDatabase(this IServiceCollection services, Action<DbOption
default:
throw new Exception($"Unsupported database provider: {options.Provider}");
}

dbContextOptions.UseOpenIddict<
CustomOpenIddictEntityFrameworkCoreApplication,
CustomOpenIddictEntityFrameworkCoreAuthorization,
Expand All @@ -72,6 +73,7 @@ public class DbOptions
{
public string Provider { get; set; } = null!;
public string ConnectionString { get; set; } = null!;
public int CommandTimeout { get; set; } = 20;
public RetryOptions RetryOptions { get; set; } = new();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio
case SQLSERVER:
dbContextOptions.UseSqlServer(options.DbConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(SQLSERVER_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Files");
Expand All @@ -39,7 +39,7 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio
case POSTGRES:
dbContextOptions.UseNpgsql(options.DbConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(POSTGRES_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Files");
Expand All @@ -58,6 +58,7 @@ public class DbOptions
{
public string Provider { get; set; } = null!;
public string DbConnectionString { get; set; } = null!;
public int CommandTimeout { get; set; } = 20;
public RetryOptions RetryOptions { get; set; } = new();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio
case SQLSERVER:
dbContextOptions.UseSqlServer(options.DbConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(SQLSERVER_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Messages");
Expand All @@ -38,7 +38,7 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio
case POSTGRES:
dbContextOptions.UseNpgsql(options.DbConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(POSTGRES_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Messages");
Expand All @@ -55,6 +55,7 @@ public class DbOptions
{
public string Provider { get; set; } = null!;
public string DbConnectionString { get; set; } = null!;
public int CommandTimeout { get; set; } = 20;
public RetryOptions RetryOptions { get; set; } = new();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio
case SQLSERVER:
dbContextOptions.UseSqlServer(options.DbConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(SQLSERVER_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Quotas");
Expand All @@ -42,7 +42,7 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio
case POSTGRES:
dbContextOptions.UseNpgsql(options.DbConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(POSTGRES_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Quotas");
Expand All @@ -67,6 +67,7 @@ public class DbOptions
{
public string Provider { get; set; } = null!;
public string DbConnectionString { get; set; } = null!;
public int CommandTimeout { get; set; } = 20;
public RetryOptions RetryOptions { get; set; } = new();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio
case SQLSERVER:
dbContextOptions.UseSqlServer(options.DbConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(SQLSERVER_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Relationships");
Expand All @@ -38,7 +38,7 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio
case POSTGRES:
dbContextOptions.UseNpgsql(options.DbConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(POSTGRES_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Relationships");
Expand All @@ -54,6 +54,7 @@ public class DbOptions
{
public string Provider { get; set; } = null!;
public string DbConnectionString { get; set; } = null!;
public int CommandTimeout { get; set; } = 20;
public RetryOptions RetryOptions { get; set; } = new();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio
case SQLSERVER:
dbContextOptions.UseSqlServer(options.DbConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(SQLSERVER_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Synchronization");
Expand All @@ -39,7 +39,7 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio
case POSTGRES:
dbContextOptions.UseNpgsql(options.DbConnectionString, sqlOptions =>
{
sqlOptions.CommandTimeout(20);
sqlOptions.CommandTimeout(options.CommandTimeout);
sqlOptions.MigrationsAssembly(POSTGRES_MIGRATIONS_ASSEMBLY);
sqlOptions.EnableRetryOnFailure(options.RetryOptions.MaxRetryCount, TimeSpan.FromSeconds(options.RetryOptions.MaxRetryDelayInSeconds), null);
sqlOptions.MigrationsHistoryTable(HistoryRepository.DefaultTableName, "Synchronization");
Expand All @@ -57,6 +57,7 @@ public static void AddDatabase(this IServiceCollection services, DbOptions optio
public class DbOptions
{
public string Provider { get; set; } = null!;
public int CommandTimeout { get; set; } = 20;
public string DbConnectionString { get; set; } = null!;
public RetryOptions RetryOptions { get; set; } = new();
}
Expand Down
Loading

0 comments on commit d7911f9

Please sign in to comment.