package database import ( "context" "database/sql" "embed" "fmt" "sort" "time" _ "modernc.org/sqlite" ) //go:embed migrations/*.sql var migrationsFS embed.FS // Connect opens a SQLite database and verifies connectivity. func Connect(ctx context.Context, databaseURL string) (*sql.DB, error) { db, err := sql.Open("sqlite", databaseURL) if err != nil { return nil, fmt.Errorf("open DATABASE_URL: %w", err) } ctx, cancel := context.WithTimeout(ctx, 10*time.Second) defer cancel() if err := db.PingContext(ctx); err != nil { db.Close() return nil, fmt.Errorf("ping database: %w", err) } return db, nil } // Migrate applies pending SQL migrations in filename order. func Migrate(ctx context.Context, db *sql.DB) error { entries, err := migrationsFS.ReadDir("migrations") if err != nil { return fmt.Errorf("read migrations: %w", err) } names := make([]string, 0, len(entries)) for _, e := range entries { names = append(names, e.Name()) } sort.Strings(names) if _, err := db.ExecContext(ctx, ` CREATE TABLE IF NOT EXISTS schema_migrations ( version TEXT PRIMARY KEY, applied_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP )`); err != nil { return fmt.Errorf("ensure schema_migrations: %w", err) } for _, name := range names { var applied bool err := db.QueryRowContext(ctx, "SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version = ?)", name). Scan(&applied) if err != nil { return fmt.Errorf("check migration %s: %w", name, err) } if applied { continue } body, err := migrationsFS.ReadFile("migrations/" + name) if err != nil { return fmt.Errorf("read migration %s: %w", name, err) } tx, err := db.BeginTx(ctx, nil) if err != nil { return fmt.Errorf("begin migration %s: %w", name, err) } if _, err := tx.ExecContext(ctx, string(body)); err != nil { tx.Rollback() //nolint:errcheck return fmt.Errorf("apply migration %s: %w", name, err) } if _, err := tx.ExecContext(ctx, "INSERT INTO schema_migrations (version) VALUES (?)", name); err != nil { tx.Rollback() //nolint:errcheck return fmt.Errorf("record migration %s: %w", name, err) } if err := tx.Commit(); err != nil { return fmt.Errorf("commit migration %s: %w", name, err) } } return nil }