feat(pgdb): export config discovery functions for display purposes
Add FindConfig, ParseURIConfig, and GetConfigFiles as exported functions to allow callers to retrieve database configuration info without establishing a connection. Useful for version/startup info display.
This commit is contained in:
@@ -42,7 +42,7 @@ type PoolOptions struct {
|
|||||||
// DefaultPoolOptions returns sensible defaults for pgxpool
|
// DefaultPoolOptions returns sensible defaults for pgxpool
|
||||||
func DefaultPoolOptions() PoolOptions {
|
func DefaultPoolOptions() PoolOptions {
|
||||||
return PoolOptions{
|
return PoolOptions{
|
||||||
ConfigFiles: getConfigFiles(),
|
ConfigFiles: GetConfigFiles(),
|
||||||
MinConns: 0,
|
MinConns: 0,
|
||||||
MaxConns: 25,
|
MaxConns: 25,
|
||||||
MaxConnLifetime: time.Hour,
|
MaxConnLifetime: time.Hour,
|
||||||
@@ -96,7 +96,7 @@ func OpenPool(ctx context.Context, options PoolOptions) (*pgxpool.Pool, error) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Fall back to config file approach
|
// Fall back to config file approach
|
||||||
pgCfg, err := findAndParseConfig(options.ConfigFiles)
|
pgCfg, _, err := FindConfig(options.ConfigFiles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -137,8 +137,15 @@ func OpenPoolWithConfigFile(ctx context.Context, configFile string) (*pgxpool.Po
|
|||||||
return OpenPool(ctx, options)
|
return OpenPool(ctx, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
// findAndParseConfig searches for and parses the first existing config file
|
// FindConfig searches for and parses the first existing config file.
|
||||||
func findAndParseConfig(configFiles []string) (*database.PostgresConfig, error) {
|
// Returns the PostgresConfig, the path to the config file used, and any error.
|
||||||
|
// If DATABASE_URI env var is set, returns nil config with empty path (use ParseURIConfig instead).
|
||||||
|
func FindConfig(configFiles []string) (*database.PostgresConfig, string, error) {
|
||||||
|
// Check if DATABASE_URI takes precedence
|
||||||
|
if os.Getenv("DATABASE_URI") != "" {
|
||||||
|
return nil, "", nil
|
||||||
|
}
|
||||||
|
|
||||||
var errs []error
|
var errs []error
|
||||||
var triedFiles []string
|
var triedFiles []string
|
||||||
|
|
||||||
@@ -161,13 +168,23 @@ func findAndParseConfig(configFiles []string) (*database.PostgresConfig, error)
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
return pgCfg, nil
|
return pgCfg, configFile, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(errs) > 0 {
|
if len(errs) > 0 {
|
||||||
return nil, fmt.Errorf("no valid config file found (tried: %v): %w", triedFiles, errors.Join(errs...))
|
return nil, "", fmt.Errorf("no valid config file found (tried: %v): %w", triedFiles, errors.Join(errs...))
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("no valid config files provided")
|
return nil, "", fmt.Errorf("no valid config files provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseURIConfig extracts connection info from DATABASE_URI environment variable.
|
||||||
|
// Returns nil if DATABASE_URI is not set.
|
||||||
|
func ParseURIConfig() (*pgxpool.Config, error) {
|
||||||
|
uri := os.Getenv("DATABASE_URI")
|
||||||
|
if uri == "" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return pgxpool.ParseConfig(uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseConfigFile reads and parses a YAML config file
|
// parseConfigFile reads and parses a YAML config file
|
||||||
@@ -205,8 +222,9 @@ func parseConfigFile(configFile string) (*database.PostgresConfig, error) {
|
|||||||
return nil, fmt.Errorf("no PostgreSQL configuration found in %s", configFile)
|
return nil, fmt.Errorf("no PostgreSQL configuration found in %s", configFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
// getConfigFiles returns the list of config files to search
|
// GetConfigFiles returns the list of config files to search for database configuration.
|
||||||
func getConfigFiles() []string {
|
// Checks DATABASE_CONFIG_FILE env var first, otherwise returns default paths.
|
||||||
|
func GetConfigFiles() []string {
|
||||||
if configFile := os.Getenv("DATABASE_CONFIG_FILE"); configFile != "" {
|
if configFile := os.Getenv("DATABASE_CONFIG_FILE"); configFile != "" {
|
||||||
return []string{configFile}
|
return []string{configFile}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user