fix(db): Open 自动创建父目录(自定义 DATA_DIR 首启不报错);add docker-compose.dev.yml(PG+pgvector 必需 + Ollama embedding 可选)
This commit is contained in:
1 parent
cee2d26205
commit
1d9c14300d
2 files changed
+59
No files matched your search
@@ -22,6 +22,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
_ "modernc.org/sqlite" // pure-Go SQLite driver, registers driver name "sqlite"
|
||||
@@ -66,6 +68,16 @@ func Open(ctx context.Context, path string, log *slog.Logger) (*sql.DB, error) {
|
||||
}
|
||||
log = discardLogger(log)
|
||||
|
||||
// Ensure the parent directory exists for file-backed databases so a custom
|
||||
// DATA_DIR (e.g. ./data) works on first run without a pre-made directory.
|
||||
if !isMemory(path) {
|
||||
if dir := filepath.Dir(path); dir != "" && dir != "." {
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return nil, fmt.Errorf("db: create dir %q: %w", dir, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
db, err := sql.Open("sqlite", buildDSN(path))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db: open %q: %w", path, err)
|
||||
|
||||
Reference in new issue
Block a user