Skip to content

Commit

Permalink
fix: cmd/redka - enforce pragmas on each connection (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Jun 8, 2024
1 parent 05f54ab commit 673d1e5
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion cmd/redka/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package main

import (
"context"
"database/sql"
"flag"
"fmt"
"log/slog"
Expand All @@ -18,6 +19,7 @@ import (
"os/signal"
"syscall"

"github.com/mattn/go-sqlite3"
_ "github.com/mattn/go-sqlite3"
"github.com/nalgeon/redka"
"github.com/nalgeon/redka/internal/server"
Expand All @@ -30,7 +32,14 @@ var (
date = "unknown"
)

const driverName = "redka"
const memoryURI = "file:redka?mode=memory&cache=shared"
const pragma = `
pragma journal_mode = wal;
pragma synchronous = normal;
pragma temp_store = memory;
pragma mmap_size = 268435456;
pragma foreign_keys = on;`

// Config holds the server configuration.
type Config struct {
Expand All @@ -47,13 +56,24 @@ func (c *Config) Addr() string {
var config Config

func init() {
// Set up command line flags.
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: redka [options] <data-source>\n")
flag.PrintDefaults()
}
flag.StringVar(&config.Host, "h", "localhost", "server host")
flag.StringVar(&config.Port, "p", "6379", "server port")
flag.BoolVar(&config.Verbose, "v", false, "verbose logging")

// Register an SQLite driver with custom pragmas.
// Ensures that the PRAGMA settings apply to
// all connections opened by the driver.
sql.Register(driverName, &sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
_, err := conn.Exec(pragma, nil)
return err
},
})
}

func main() {
Expand Down Expand Up @@ -88,7 +108,12 @@ func main() {
slog.Info("starting redka", "version", version, "commit", commit, "built_at", date)

// Open the database.
db, err := redka.Open(config.Path, &redka.Options{Logger: logger})
opts := redka.Options{
DriverName: driverName,
Logger: logger,
Pragma: map[string]string{},
}
db, err := redka.Open(config.Path, &opts)
if err != nil {
slog.Error("data source", "error", err)
os.Exit(1)
Expand Down

0 comments on commit 673d1e5

Please sign in to comment.