Skip to content

Commit 1736a5b

Browse files
mpywclaude
andcommitted
fix: improve DSN parsing order for SQLite fallback
Parse URL first, then fallback to SQLite special cases (:memory:, .db, .sqlite) if no scheme found or parse error. Preserves original parse error for better diagnostics. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 05ea920 commit 1736a5b

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

internal/config/config.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,27 @@ func (cfg *Config) Driver() (string, error) {
102102
if dsn == "" {
103103
return "", errors.New("no database configured")
104104
}
105-
// Handle SQLite special cases: :memory: or file paths without scheme
105+
// Try to parse as URL and use scheme if present
106+
u, parseErr := url.Parse(dsn)
107+
if parseErr == nil && u.Scheme != "" {
108+
switch u.Scheme {
109+
case "postgres", "postgresql":
110+
return "pgx", nil
111+
case "file", "sqlite":
112+
return "sqlite", nil
113+
default:
114+
return u.Scheme, nil
115+
}
116+
}
117+
// No scheme or parse error - fallback to SQLite special cases
106118
if dsn == ":memory:" || strings.HasSuffix(dsn, ".db") || strings.HasSuffix(dsn, ".sqlite") {
107119
return "sqlite", nil
108120
}
109-
u, err := url.Parse(dsn)
110-
if err != nil {
111-
return "", fmt.Errorf("failed to parse DSN: %w", err)
112-
}
113-
switch u.Scheme {
114-
case "postgres", "postgresql":
115-
return "pgx", nil
116-
case "file", "sqlite":
117-
return "sqlite", nil
118-
default:
119-
return u.Scheme, nil
121+
// Return the original parse error if available
122+
if parseErr != nil {
123+
return "", fmt.Errorf("failed to parse DSN: %w", parseErr)
120124
}
125+
return "", errors.New("failed to parse DSN: missing protocol scheme")
121126
}
122127

123128
// CORSEnabled returns true if CORS is configured.

0 commit comments

Comments
 (0)