| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package db
- import (
- "context"
- "database/sql"
- "fmt"
- "log/slog"
- "path/filepath"
- "github.com/pressly/goose/v3"
- )
- var pragmas = map[string]string{
- "foreign_keys": "ON",
- "journal_mode": "WAL",
- "page_size": "4096",
- "cache_size": "-8000",
- "synchronous": "NORMAL",
- "secure_delete": "ON",
- "busy_timeout": "30000",
- }
- // Connect opens a SQLite database connection and runs migrations.
- func Connect(ctx context.Context, dataDir string) (*sql.DB, error) {
- if dataDir == "" {
- return nil, fmt.Errorf("data.dir is not set")
- }
- dbPath := filepath.Join(dataDir, "crush.db")
- db, err := openDB(dbPath)
- if err != nil {
- return nil, err
- }
- if err = db.PingContext(ctx); err != nil {
- db.Close()
- return nil, fmt.Errorf("failed to connect to database: %w", err)
- }
- goose.SetBaseFS(FS)
- if err := goose.SetDialect("sqlite3"); err != nil {
- slog.Error("Failed to set dialect", "error", err)
- return nil, fmt.Errorf("failed to set dialect: %w", err)
- }
- if err := goose.Up(db, "migrations"); err != nil {
- slog.Error("Failed to apply migrations", "error", err)
- return nil, fmt.Errorf("failed to apply migrations: %w", err)
- }
- return db, nil
- }
|