Skip to content

Refactor MySQL connection string assignment logic#4484

Closed
tanushahande2003 wants to merge 1 commit into
Releases/Official-Releasefrom
bugfix/61207_SSL-mode-parameter-handling
Closed

Refactor MySQL connection string assignment logic#4484
tanushahande2003 wants to merge 1 commit into
Releases/Official-Releasefrom
bugfix/61207_SSL-mode-parameter-handling

Conversation

@tanushahande2003
Copy link
Copy Markdown
Contributor

@tanushahande2003 tanushahande2003 commented Apr 9, 2026

Check if ConnectionStringCalculated is empty before assigning my.ConnectionString to Database.ConnectionString. Use GetConnectionString() when initializing MySqlConnection to ensure the correct connection string is used.

Description

Type of Change

  • Bug fix - [ ] New feature - [ ] Breaking change - [ ] Plugin update

Checklist

  • PR description clearly describes the changes
  • Target branch is correct (master for features, Releases/* for fixes)
  • Latest code from target branch merged
  • No commented/junk code included
  • No new build warnings or errors
  • All existing unit tests pass
  • New unit tests added for new functionality
  • Cross-platform compatibility verified (Windows/Linux/macOS)
  • CI/CD pipeline passes
  • Code follows project conventions (Act{Platform}{Type}, {Platform}Driver)
  • Repository objects use [IsSerializedForLocalRepository] where needed
  • Error handling uses Reporter.ToLog() pattern
  • Documentation updated for user-facing changes
  • Self-review completed and code review comments addressed

Summary by CodeRabbit

  • Bug Fixes
    • Improved MySQL database connection handling to prevent unintended connection string overwrites and ensure proper connection initialization.

Check if ConnectionStringCalculated is empty before assigning my.ConnectionString to Database.ConnectionString. Use GetConnectionString() when initializing MySqlConnection to ensure the correct connection string is used.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 9, 2026

Walkthrough

The MySQL connection initialization in DatabaseOperations.Connect() now conditionally assigns the connection string only when not already calculated, and sources it via GetConnectionString() method instead of direct property access.

Changes

Cohort / File(s) Summary
MySQL Connection String Handling
Ginger/GingerCoreNET/Database/DatabaseOperations.cs
Modified eDBTypes.MySQL connection logic to conditionally set Database.ConnectionString (only when null/empty) and use GetConnectionString() for the MySqlConnection instance instead of direct property assignment.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • MSSQL Fix Test #4382: Also modifies connection string construction in DatabaseOperations.cs, specifically for MSSQL builder logic in the same Connect flow.
  • Not using GetConnectionString method #4383: Modifies connection string handling in DatabaseOperations.cs for Oracle connections, replacing GetConnectionString() usage with direct ConnectionStringCalculated and decrypted password approach.

Poem

🐰 A MySQL string that's now quite wise,
Conditional checks before it flies,
GetConnectionString's gentle call,
Prevents the overwrite—no more to fall,
Ginger hops on with grace and care! 🥕

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The PR description provides a clear summary of changes but leaves most checklist items unchecked, indicating incomplete verification of required steps. Complete the checklist by checking off verified items and confirming testing, cross-platform compatibility, CI/CD pipeline status, and documentation updates.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: refactoring MySQL connection string assignment logic to make it conditional rather than unconditional.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bugfix/61207_SSL-mode-parameter-handling

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codacy-production
Copy link
Copy Markdown

Not up to standards ⛔

TIP This summary will be updated as you push new changes. Give us feedback

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@Ginger/GingerCoreNET/Database/DatabaseOperations.cs`:
- Around line 492-499: The code runs ValidateHostPort(...) and requires a valid
Database.TNS even when a complete connection string exists; update the logic to
skip host/TNS parsing when a connection string is already provided or
ConnectionStringCalculated is true: i.e., before calling ValidateHostPort (or
wrap that call), add a guard checking if
!string.IsNullOrEmpty(Database.ConnectionString) || ConnectionStringCalculated
and only run ValidateHostPort when that guard is false so GetConnectionString()
and the MySqlConnection initialization (oConn / GetConnectionString) can succeed
without TNS validation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0b818769-e753-4a72-aaf9-862ee4eb402a

📥 Commits

Reviewing files that changed from the base of the PR and between 5c74812 and 5b11fff.

📒 Files selected for processing (1)
  • Ginger/GingerCoreNET/Database/DatabaseOperations.cs

Comment on lines +492 to +499
if (string.IsNullOrEmpty(ConnectionStringCalculated))
{
Database.ConnectionString = my.ConnectionString;
}

oConn = new MySqlConnection
{
ConnectionString = my.ConnectionString
ConnectionString = GetConnectionString()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Guard currently preserves connection string assignment but still requires valid TNS unnecessarily

ConnectionStringCalculated is checked only for assignment (Line 492), but MySQL host parsing/ValidateHostPort(...) runs earlier regardless.
If Database.ConnectionString is already provided and Database.TNS is empty/invalid, connect still fails before Line 499 uses GetConnectionString().

Proposed fix
                     case eDBTypes.MySQL:
-                        string mySQLHost = TNSCalculated;
-                        uint? port1 = null;
-                        if (TNSCalculated.Contains(':', StringComparison.Ordinal))
-                        {
-                            var parts = TNSCalculated.Split(':', 2);
-                            mySQLHost = parts[0];
-                            if (uint.TryParse(parts[1], out uint p)) port1 = p;
-                        }
-
-                        ValidateHostPort(mySQLHost, port1.HasValue ? (int?)port1.Value : null);
-
-                        var my = new MySqlConnectionStringBuilder
-                        {
-                            Server = mySQLHost,
-                            Database = Database.Name ?? string.Empty,
-                            UserID = UserCalculated,
-                            Password = EncryptionHandler.DecryptwithKey(PassCalculated)
-                        };
-                        if (port1.HasValue) my.Port = port1.Value;
-                        if (string.IsNullOrEmpty(ConnectionStringCalculated))
-                        {
-                            Database.ConnectionString = my.ConnectionString;
-                        }
+                        if (string.IsNullOrEmpty(ConnectionStringCalculated))
+                        {
+                            string mySQLHost = TNSCalculated;
+                            uint? port1 = null;
+                            if (TNSCalculated.Contains(':', StringComparison.Ordinal))
+                            {
+                                var parts = TNSCalculated.Split(':', 2);
+                                mySQLHost = parts[0];
+                                if (uint.TryParse(parts[1], out uint p)) port1 = p;
+                            }
+
+                            ValidateHostPort(mySQLHost, port1.HasValue ? (int?)port1.Value : null);
+
+                            var my = new MySqlConnectionStringBuilder
+                            {
+                                Server = mySQLHost,
+                                Database = Database.Name ?? string.Empty,
+                                UserID = UserCalculated,
+                                Password = EncryptionHandler.DecryptwithKey(PassCalculated)
+                            };
+                            if (port1.HasValue) my.Port = port1.Value;
+                            Database.ConnectionString = my.ConnectionString;
+                        }
 
                         oConn = new MySqlConnection
                         {
                             ConnectionString = GetConnectionString()
                         };
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Ginger/GingerCoreNET/Database/DatabaseOperations.cs` around lines 492 - 499,
The code runs ValidateHostPort(...) and requires a valid Database.TNS even when
a complete connection string exists; update the logic to skip host/TNS parsing
when a connection string is already provided or ConnectionStringCalculated is
true: i.e., before calling ValidateHostPort (or wrap that call), add a guard
checking if !string.IsNullOrEmpty(Database.ConnectionString) ||
ConnectionStringCalculated and only run ValidateHostPort when that guard is
false so GetConnectionString() and the MySqlConnection initialization (oConn /
GetConnectionString) can succeed without TNS validation.

oConn = new MySqlConnection
{
ConnectionString = my.ConnectionString
ConnectionString = GetConnectionString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants