fix(db): handle path names that include URL special chars (fixes #10245) (#10247)

😬
This commit is contained in:
Jakob Borg
2025-08-13 13:01:16 +02:00
committed by GitHub
parent 9ea6c9c3c3
commit 370bbb8f26
2 changed files with 61 additions and 1 deletions
+17 -1
View File
@@ -10,6 +10,7 @@ import (
"database/sql"
"embed"
"io/fs"
"net/url"
"path/filepath"
"strconv"
"strings"
@@ -44,7 +45,12 @@ type baseDB struct {
func openBase(path string, maxConns int, pragmas, schemaScripts, migrationScripts []string) (*baseDB, error) {
// Open the database with options to enable foreign keys and recursive
// triggers (needed for the delete+insert triggers on row replace).
sqlDB, err := sqlx.Open(dbDriver, "file:"+path+"?"+commonOptions)
pathURL := url.URL{
Scheme: "file",
Path: fileToUriPath(path),
RawQuery: commonOptions,
}
sqlDB, err := sqlx.Open(dbDriver, pathURL.String())
if err != nil {
return nil, wrap(err)
}
@@ -110,6 +116,16 @@ func openBase(path string, maxConns int, pragmas, schemaScripts, migrationScript
return db, nil
}
func fileToUriPath(path string) string {
path = filepath.ToSlash(path)
if (build.IsWindows && len(path) >= 2 && path[1] == ':') ||
(strings.HasPrefix(path, "//") && !strings.HasPrefix(path, "///")) {
// Add an extra leading slash for Windows drive letter or UNC path
path = "/" + path
}
return path
}
func (s *baseDB) Close() error {
s.updateLock.Lock()
s.statementsMut.Lock()