Refactor MySQL connection string assignment logic#4484
Conversation
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.
WalkthroughThe MySQL connection initialization in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Not up to standards ⛔
|
There was a problem hiding this comment.
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
📒 Files selected for processing (1)
Ginger/GingerCoreNET/Database/DatabaseOperations.cs
| if (string.IsNullOrEmpty(ConnectionStringCalculated)) | ||
| { | ||
| Database.ConnectionString = my.ConnectionString; | ||
| } | ||
|
|
||
| oConn = new MySqlConnection | ||
| { | ||
| ConnectionString = my.ConnectionString | ||
| ConnectionString = GetConnectionString() |
There was a problem hiding this comment.
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() |
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
Checklist
[IsSerializedForLocalRepository]where neededReporter.ToLog()patternSummary by CodeRabbit