-
Notifications
You must be signed in to change notification settings - Fork 105
feat: [#272] The Orm module can set DSN directly #763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughThe pull request introduces a new Changes
Assessment against linked issues
Possibly related PRs
Suggested reviewers
Tip CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚠️ Performance Alert ⚠️
Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.
| Benchmark suite | Current: 6af0768 | Previous: 44c5de1 | Ratio |
|---|---|---|---|
BenchmarkFile_ReadWrite |
275801 ns/op 2072 B/op 27 allocs/op |
181467 ns/op 2072 B/op 27 allocs/op |
1.52 |
BenchmarkFile_ReadWrite - ns/op |
275801 ns/op |
181467 ns/op |
1.52 |
This comment was automatically generated by workflow using github-action-benchmark.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #763 +/- ##
==========================================
+ Coverage 69.81% 69.83% +0.02%
==========================================
Files 210 210
Lines 17841 17853 +12
==========================================
+ Hits 12455 12467 +12
Misses 4706 4706
Partials 680 680 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
contracts/database/config.go (1)
18-18: Add documentation for the new Dsn fieldPlease add a comment explaining the purpose and format of the Dsn field, as this struct is used in configuration files.
database/db/dsn.go (1)
10-13: Consider adding DSN validationWhile the early return for a custom DSN is good, consider adding basic validation to ensure the provided DSN string is properly formatted for the selected driver.
database/db/dsn_test.go (1)
40-48: Consider adding more test cases for DSN handlingWhile the basic test case is good, consider adding tests for:
- Driver-specific DSN formats
- Invalid DSN strings
- Interaction between DSN and driver settings
Example test cases:
{ name: "mysql dsn format", config: database.FullConfig{ Config: database.Config{ Dsn: "user:pass@tcp(host:3306)/dbname", }, Driver: database.DriverMysql, }, expectDsn: "user:pass@tcp(host:3306)/dbname", }, { name: "postgres dsn format", config: database.FullConfig{ Config: database.Config{ Dsn: "postgres://user:pass@host:5432/dbname", }, Driver: database.DriverPostgres, }, expectDsn: "postgres://user:pass@host:5432/dbname", },
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (7)
contracts/database/config.go(1 hunks)database/db/config_builder.go(1 hunks)database/db/config_builder_test.go(5 hunks)database/db/dsn.go(1 hunks)database/db/dsn_test.go(1 hunks)database/gorm/test_utils.go(6 hunks)foundation/application_test.go(1 hunks)
🔇 Additional comments (7)
contracts/database/config.go (1)
Line range hint 18-24: Verify configuration file compatibility
The addition of the Dsn field to the Config struct may require updates to existing configuration files.
database/db/dsn.go (1)
10-12: LGTM! Clean implementation of direct DSN support
The early return pattern gives appropriate precedence to explicitly provided DSN strings while maintaining existing behavior.
database/db/config_builder.go (1)
58-60: LGTM! Verify DSN precedence over individual connection parameters.
The implementation correctly handles DSN configuration while maintaining backward compatibility. The placement before other connection parameters suggests DSN takes precedence, which is the expected behavior.
Let's verify the DSN precedence behavior:
✅ Verification successful
Let me gather more information about the DSN handling and connection parameters.
Let me check the test files and the DSN implementation to verify the precedence behavior.
Let me check one final aspect to understand how the DSN is used in relation to other connection parameters.
DSN precedence is correctly implemented and verified
The implementation ensures proper DSN precedence:
- The
Dsnfunction indatabase/db/dsn.goexplicitly checks for and returns the DSN first if it exists - Individual connection parameters (host, port, etc.) are only used to construct the DSN if no explicit DSN is provided
- The Config struct properly defines DSN as a first-class field
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify that DSN takes precedence over individual connection parameters
# by checking for any direct database connections that might bypass DSN.
# Search for database connection initialization patterns
rg -A 5 'sql\.Open|gorm\.Open'
# Search for DSN usage patterns
ast-grep --pattern 'if $dsn != "" {
$$$
}'
Length of output: 1797
Script:
#!/bin/bash
# Search for DSN and connection parameter handling in config_builder.go
rg -A 10 'fillDefault|buildConfig' database/db/config_builder.go
# Search for any other DSN-related logic
rg -l 'dsn.*=|=.*dsn' --type go
# Check for any tests that verify DSN behavior
fd test database --exec rg -l 'dsn|DSN' {}
Length of output: 1200
Script:
#!/bin/bash
# Check DSN-related test cases
rg -A 10 'TestDsn|test.*DSN' database/db/dsn_test.go database/db/config_builder_test.go
# Look at the actual DSN implementation
rg -A 15 'func.*fillDefault' database/db/config_builder.go
# Check dialector implementation for DSN handling
rg -A 10 'DSN|dsn' database/gorm/dialector.go
Length of output: 1937
Script:
#!/bin/bash
# Check the DSN generation logic
rg -A 15 'func.*Dsn' database/db/dsn.go
# Look for any DSN override tests
rg -B 5 -A 10 'dsn.*override|override.*dsn' database/db/dsn_test.go database/db/config_builder_test.go
# Check the Config and FullConfig structs
ast-grep --pattern 'type $_ struct {
$$$
Dsn $_
$$$
}'
Length of output: 1241
database/db/config_builder_test.go (1)
108-108: LGTM! Test coverage is comprehensive.
The test cases properly cover:
- DSN initialization
- Mock configuration expectations
- Both empty and populated DSN scenarios
Also applies to: 137-137, 155-155, 169-169, 193-193
foundation/application_test.go (1)
326-326: LGTM! Mock expectations are correctly configured.
The DSN mock expectation is properly placed and called twice to cover both read and write configurations.
database/gorm/test_utils.go (2)
279-279: LGTM! The DSN configuration is consistently implemented across database drivers.
The implementation of the DSN configuration is well-structured and consistent across MySQL, PostgreSQL, and SQLServer mock drivers. The changes appropriately:
- Add DSN configuration in both single and read/write scenarios
- Set empty string as the default value
- Maintain the existing mock configuration pattern
- Correctly exclude SQLite from DSN configuration as it doesn't use connection strings
Also applies to: 306-306, 352-352, 379-379, 482-482, 506-506
279-279: Verify DSN implementation consistency across the codebase.
Let's ensure the mock implementations align with the actual database configuration and usage patterns.
Also applies to: 306-306, 352-352, 379-379, 482-482, 506-506
✅ Verification successful
Let me gather more information about the DSN configuration pattern by checking the actual database connection implementation.
Based on the gathered information, I can now provide the final response:
DSN configuration pattern is consistent across the codebase
The DSN configuration follows a consistent pattern across all database drivers (MySQL, PostgreSQL, SQLite, SQLServer):
- DSN is correctly configured under
database.connections.<connection>.dsn - When DSN is empty, the connection is built using individual configuration parameters (host, port, database, username, password)
- The implementation in test_utils.go matches the actual database configuration structure in contracts/database/config.go
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Verify DSN configuration implementation across the codebase
# Check for DSN configuration in database config structs
echo "Checking DSN configuration in database config structs..."
ast-grep --pattern 'type Config struct {
$$$
Dsn string
$$$
}'
# Check for DSN usage in database connection logic
echo "Checking DSN usage in connection logic..."
rg -A 5 'GetString\(".*\.dsn"\)' --type go
Length of output: 1481
Script:
#!/bin/bash
# Check database connection implementation
echo "Checking database connection implementation..."
rg -A 10 'func.*Connect' --type go
# Check for any other DSN-related configuration patterns
echo "Checking DSN-related configuration patterns..."
rg -A 5 'database\.connections\.' --type go
Length of output: 69349
devhaozi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
📑 Description
Closes goravel/goravel#272
Migrated successfully from local:
Summary by CodeRabbit
New Features
Dsnfield in the database configuration, enhancing connection handling.Bug Fixes
Dsnfield is populated correctly when not set.Tests
fillDefaultmethod and theDsnfunction to validate new configurations.Chores
Dsnparameter.✅ Checks