repo_description: Glamourous agentic coding for all 💘
repo_topics: agentic-ai ai llms ravishing
code: package db
  
  import (
  	"context"
  	"database/sql"
  	"embed"
  	"fmt"
  	"log/slog"
  	"path/filepath"
  	"testing"
  
  	"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",
  }
  
  //go:embed migrations/*.sql
  var FS embed.FS
  
  func init() {
  	goose.SetBaseFS(FS)
  
  	if testing.Testing() {
  		goose.SetLogger(goose.NopLogger())
  	}
  }
  
  // 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)
  	}
  
  	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
  }
  