From f66954a30a56f3fcacc9e4a6db56ff2088419555 Mon Sep 17 00:00:00 2001 From: Flavio Castelli Date: Fri, 28 Jun 2024 14:09:49 +0200 Subject: [PATCH] fix: address issues of sqlite backend Using sqlite to save checkpoints failed at runtime because: * Internal Mutex was a nil pointer * The help messages explaining how to create the sqlite3 database had the wrong CREATE statement * The implementation of the `LockBackend` interface had the `Fetch` function return a wrong object (a `dynamoCheckpoint` instead of a `sqliteCheckpoint`) Signed-off-by: Flavio Castelli --- cmd/sunlight/main.go | 2 +- internal/ctlog/sqlite.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/sunlight/main.go b/cmd/sunlight/main.go index 63d7750..0e6a49d 100644 --- a/cmd/sunlight/main.go +++ b/cmd/sunlight/main.go @@ -80,7 +80,7 @@ type Config struct { // The database must already exist to protect against accidental // misconfiguration. Create the table with: // - // $ sqlite3 checkpoints.db "CREATE TABLE checkpoints (logID BLOB PRIMARY KEY, checkpoint TEXT)" + // $ sqlite3 checkpoints.db "CREATE TABLE checkpoints (logID BLOB PRIMARY KEY, body TEXT)" // Checkpoints string diff --git a/internal/ctlog/sqlite.go b/internal/ctlog/sqlite.go index 9c0abfb..2764dc2 100644 --- a/internal/ctlog/sqlite.go +++ b/internal/ctlog/sqlite.go @@ -34,7 +34,7 @@ func NewSQLiteBackend(ctx context.Context, path string, l *slog.Logger) (*SQLite conn, err := sqlite.OpenConn(path, sqlite.OpenFlagsDefault & ^sqlite.SQLITE_OPEN_CREATE) if err != nil { - return nil, fmt.Errorf(`failed to open SQLite lock database (hint: to avoid misconfiguration, the lock database must be created manually with "CREATE TABLE checkpoints (logID BLOB PRIMARY KEY, checkpoint TEXT)"): %w`, err) + return nil, fmt.Errorf(`failed to open SQLite lock database (hint: to avoid misconfiguration, the lock database must be created manually with "CREATE TABLE checkpoints (logID BLOB PRIMARY KEY, body TEXT)"): %w`, err) } if err := sqlitex.ExecTransient(conn, "PRAGMA synchronous = FULL", nil); err != nil { conn.Close() @@ -49,6 +49,7 @@ func NewSQLiteBackend(ctx context.Context, path string, l *slog.Logger) (*SQLite conn: conn, duration: duration, log: l, + mu: &sync.Mutex{}, }, nil } @@ -78,7 +79,7 @@ func (b *SQLiteBackend) Fetch(ctx context.Context, logID [sha256.Size]byte) (Loc if body == nil { return nil, errors.New("checkpoint not found") } - return &dynamoDBCheckpoint{logID: logID, body: body}, nil + return &sqliteCheckpoint{logID: logID, body: body}, nil } func (b *SQLiteBackend) Replace(ctx context.Context, old LockedCheckpoint, new []byte) (LockedCheckpoint, error) {