Skip to content

feat: Add ability to set PostgreSql connection options via appsettings#2975

Open
alexeyshibanov wants to merge 12 commits intodevfrom
feat/postgre-options
Open

feat: Add ability to set PostgreSql connection options via appsettings#2975
alexeyshibanov wants to merge 12 commits intodevfrom
feat/postgre-options

Conversation

@alexeyshibanov
Copy link
Contributor

@alexeyshibanov alexeyshibanov commented Jan 30, 2026

Description

These changes allow set PostgreSQL connection options via appsettings when we can't do it via configure ConnectionString.


Note

Medium Risk
Medium risk because it changes how PostgreSQL connection strings and EF/Hangfire timeouts are derived at runtime, which can affect pooling, timeouts, and overall DB stability/performance under load.

Overview
Adds a new PostgreSqlOptions configuration model (with validation) and wires it into Startup so PostgreSQL tuning can be set via appsettings.json.

PostgreSQL connection strings for EF Core and Hangfire are now automatically enhanced with configurable defaults (command timeout, pool sizes, connection lifetime, keepalive/TCP keepalive) only when those parameters aren’t already present, using new EnhanceConnectionString helpers.

Adjusts EF repository initialization to avoid overwriting an explicitly configured EF command timeout unless it’s unset or lower than the connection timeout, and adds unit tests covering the connection-string enhancement behavior and casing/alias preservation.

Written by Cursor Bugbot for commit 789a8fb. This will update automatically on new commits. Configure here.

References

QA-test:

Jira-link:

Artifact URL:

Image tag:
ghcr.io/VirtoCommerce/platform:3.1002.0-pr-2975-789a-postgre-options-789a8fb3

# Conflicts:
#	tests/VirtoCommerce.Platform.Core.Tests/VirtoCommerce.Platform.Core.Tests.csproj
#	tests/VirtoCommerce.Platform.Tests/VirtoCommerce.Platform.Tests.csproj
#	tests/VirtoCommerce.Platform.Web.Tests/VirtoCommerce.Platform.Web.Tests.csproj
@vc-ci
Copy link
Contributor

vc-ci commented Jan 30, 2026

Review task created: https://virtocommerce.atlassian.net/browse/VCST-4575

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds PostgreSQL connection configuration options to allow fine-tuning database connection settings via appsettings.json, particularly optimized for Azure PostgreSQL deployments. The changes enable control over timeouts, connection pooling, and keepalive settings without modifying connection strings directly.

Changes:

  • Introduced PostgreSqlOptions configuration model with properties for command timeouts, pool sizing, connection lifetime, and TCP keepalive settings
  • Enhanced PostgreSQL connection strings at initialization time with configurable parameters (separate defaults for regular DB and Hangfire operations)
  • Improved data layer transaction handling and async disposal patterns in raw SQL helper methods
  • Refined command timeout logic in repository and migration setup to only override when necessary

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
src/VirtoCommerce.Platform.Core/PostgreSqlOptions.cs New configuration model defining PostgreSQL connection parameters with default values
src/VirtoCommerce.Platform.Web/appsettings.json Added PostgreSql configuration section with documented defaults for Azure PostgreSQL
src/VirtoCommerce.Platform.Web/Startup.cs Registered PostgreSqlOptions with DI container and removed unused provider parameter from DbContext setup
src/VirtoCommerce.Platform.Hangfire/Extensions/PostgreSqlOptionsExtensions.cs Extension method to enhance connection strings with Hangfire-specific settings (HangfireCommandTimeout, HangfireMaxPoolSize)
src/VirtoCommerce.Platform.Hangfire/Extensions/ServiceCollectionExtensions.cs Applied connection string enhancement during Hangfire storage initialization
src/VirtoCommerce.Platform.Hangfire/Extensions/ApplicationBuilderExtensions.cs Applied connection string enhancement during Hangfire middleware setup, removed unused import
src/VirtoCommerce.Platform.Data.PostgreSql/Extensions/PostgreSqlOptionsExtensions.cs Extension method to enhance connection strings with regular DB settings (CommandTimeout, MaxPoolSize)
src/VirtoCommerce.Platform.Data.PostgreSql/Extensions/DbContextOptionsBuilderExtensions.cs Applied connection string enhancement during EF Core PostgreSQL configuration
src/VirtoCommerce.Platform.Data/Infrastructure/DbContextRepositoryBase.cs Modified to only set command timeout when it's unset or lower than connection timeout
src/VirtoCommerce.Platform.Data/Extensions/DatabaseFacadeExtensions.cs Modified to only set command timeout when it's unset or lower than connection timeout
src/VirtoCommerce.Platform.Data/Extensions/DbContextCommandExtensions.cs Enhanced raw SQL methods with transaction participation and async disposal patterns
tests/VirtoCommerce.Platform.Tests/UnitTests/PostgreSqlOptionsExtensionsTests.cs Comprehensive unit tests for connection string enhancement logic

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@vc-ci vc-ci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 7.994
Timestamp: 30-01-2026T10:25:02

Copy link
Contributor

@vc-ci vc-ci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 7.552
Timestamp: 30-01-2026T10:36:23

Copy link
Contributor

@vc-ci vc-ci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 7.978
Timestamp: 30-01-2026T11:45:21

Copy link
Contributor

@vc-ci vc-ci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 7.831
Timestamp: 30-01-2026T11:58:33

Copy link
Contributor

@vc-ci vc-ci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 7.727
Timestamp: 02-02-2026T09:42:32

Copy link
Contributor

@vc-ci vc-ci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 8.02
Timestamp: 02-02-2026T15:15:29

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 3, 2026

Quality Gate Failed Quality Gate failed

Failed conditions
27.7% Duplication on New Code (required ≤ 10%)

See analysis details on SonarQube Cloud

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

if (commandTimeout is null || (commandTimeout >= 0 && commandTimeout < connectionTimeout))
{
dbContext.Database.SetCommandTimeout(connectionTimeout);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Infinite command timeout (0) incorrectly gets overwritten

Medium Severity

The condition commandTimeout >= 0 && commandTimeout < connectionTimeout incorrectly overwrites an infinite command timeout. In EF Core, a commandTimeout of 0 means "no timeout" (wait indefinitely). When a user sets CommandTimeout=0 to allow long-running queries, and connectionTimeout is positive (e.g., 30), the check 0 < 30 evaluates to true, causing the infinite timeout to be replaced with the connection timeout value. Using commandTimeout > 0 instead of >= 0 would preserve the special infinite timeout semantics.

Fix in Cursor Fix in Web

Copy link
Contributor

@vc-ci vc-ci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Suite: Test Suites/Modules/module_Assets
Tests: 13
Failures: 0
Errors: 0
Time: 7.745
Timestamp: 03-02-2026T07:18:07

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants