fix(database): add dbname parameter to postgres dsn#970
fix(database): add dbname parameter to postgres dsn#970sm-28601 wants to merge 5 commits intomeshery:masterfrom
Conversation
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
Signed-off-by: Sahil Maurya <sahilmaurya5611@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the database connection options and PostgreSQL DSN construction so the application can explicitly target a configured database name instead of relying on PostgreSQL defaults.
Changes:
- Added a
Databasefield todatabase.Options. - Updated the PostgreSQL DSN format string to include
dbname=%s.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
database/database.go
Outdated
| switch opts.Engine { | ||
| case POSTGRES: | ||
| dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s", opts.Host, opts.Username, opts.Password, opts.Port) | ||
| dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s dbname=%s", opts.Host, opts.Username, opts.Password, opts.Port, opts.Database) |
There was a problem hiding this comment.
opts.Database is interpolated unconditionally into the Postgres DSN. If Options.Database is left empty, the DSN will contain dbname= with an empty value, which is unlikely to be intended. Consider validating opts.Database (return a clear error) or conditionally omitting dbname/defaulting it (e.g., to opts.Username) to preserve previous behavior for existing callers.
| dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s dbname=%s", opts.Host, opts.Username, opts.Password, opts.Port, opts.Database) | |
| dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s", opts.Host, opts.Username, opts.Password, opts.Port) | |
| if opts.Database != "" { | |
| dsn = fmt.Sprintf("%s dbname=%s", dsn, opts.Database) | |
| } |
There was a problem hiding this comment.
Thanks for the thorough review! I have applied both suggestions: made the dbname interpolation conditional and properly wired the logger for the Postgres configuration.
database/database.go
Outdated
| case POSTGRES: | ||
| dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s", opts.Host, opts.Username, opts.Password, opts.Port) | ||
| dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s dbname=%s", opts.Host, opts.Username, opts.Password, opts.Port, opts.Database) | ||
| db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) |
There was a problem hiding this comment.
The SQLITE branch applies opts.Logger to gorm.Config, but the POSTGRES branch always uses an empty gorm.Config{}. If callers set Options.Logger, it will currently be ignored for Postgres connections. Consider wiring opts.Logger.DatabaseLogger() into the Postgres gorm.Config the same way as SQLITE for consistent behavior.
| db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) | |
| config := &gorm.Config{} | |
| if opts.Logger != nil { | |
| config.Logger = opts.Logger.DatabaseLogger() | |
| } | |
| db, err := gorm.Open(postgres.Open(dsn), config) |
There was a problem hiding this comment.
Thanks for the thorough review! I have applied both suggestions: made the dbname interpolation conditional and properly wired the logger for the Postgres configuration.
Signed-off-by: Sahil Maurya <sahilmaurya5611@gmail.com>
|
Can you clarify why dbname is needed here? What issue were we facing without it? |
Without the dbname parameter, PostgreSQL automatically tries to connect to a database matching the operating username. We needed this fix because users were unable to connect to a specific, custom database (like mesherydb) using the Options struct. It restricted the app to only using the default user-named database. |
| case POSTGRES: | ||
| dsn := fmt.Sprintf("host=%s user=%s password=%s port=%s", opts.Host, opts.Username, opts.Password, opts.Port) | ||
| db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) | ||
| if opts.Database != "" { |
There was a problem hiding this comment.
Please validate and improve error handling when Database is empty for Postgres.
Is meshery set as the default database name somewhere?
There was a problem hiding this comment.
Please validate and improve error handling when Database is empty for Postgres.
Is
mesheryset as the default database name somewhere?
No, "meshery" is not set as a default database name anywhere. The Postgres path in meshkit is currently unused by Meshery (which uses SQLite) .
Description
This PR addresses the issue where the PostgreSQL DSN connection string in
database/database.gowas constructed without thedbnameparameter. Without explicitly defining thedbname, PostgreSQL defaults to connecting to a database matching the operating user's name, which prevents the application from connecting to a dedicated database (e.g.,mesherydb).To fix this:
Databasefield to theOptionsstruct.fmt.Sprintffor the DSN under thePOSTGREScase to includedbname=%s.This PR fixes #969
Signed commits