Refactor db package logging
This commit is contained in:
@@ -2,6 +2,7 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"log/slog"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -12,7 +13,7 @@ func TestQueries(t *testing.T) {
|
|||||||
)
|
)
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
if err := db.ApplySchema(ctx, nil); err != nil {
|
if err := db.ApplySchema(ctx, slog.Default()); err != nil {
|
||||||
t.Fatalf("ApplySchema: %v", err)
|
t.Fatalf("ApplySchema: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ var migrations embed.FS
|
|||||||
// ApplySchema reads the current database version from PRAGMA user_version
|
// ApplySchema reads the current database version from PRAGMA user_version
|
||||||
// and applies any pending migrations in a single transaction. Each migration
|
// and applies any pending migrations in a single transaction. Each migration
|
||||||
// file runs in order; if any fail the entire transaction rolls back and the
|
// file runs in order; if any fail the entire transaction rolls back and the
|
||||||
// version is unchanged.
|
// version is unchanged. log must be non-nil; pass
|
||||||
|
// slog.New(slog.DiscardHandler) to run without logs.
|
||||||
//
|
//
|
||||||
// Migration files are named <version>.<description>. The version
|
// Migration files are named <version>.<description>. The version
|
||||||
// must be a non-negative integer. On success, PRAGMA user_version is set
|
// must be a non-negative integer. On success, PRAGMA user_version is set
|
||||||
@@ -27,7 +28,7 @@ var migrations embed.FS
|
|||||||
// the application code expects a schema that matches the latest migration.
|
// the application code expects a schema that matches the latest migration.
|
||||||
func (s *SQLite) ApplySchema(ctx context.Context, log *slog.Logger) error {
|
func (s *SQLite) ApplySchema(ctx context.Context, log *slog.Logger) error {
|
||||||
if log == nil {
|
if log == nil {
|
||||||
log = slog.Default()
|
return fmt.Errorf("logger is nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
var currentVersion int
|
var currentVersion int
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"log/slog"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ func TestSchema(t *testing.T) {
|
|||||||
testInitialVersion(t, ctx, db)
|
testInitialVersion(t, ctx, db)
|
||||||
})
|
})
|
||||||
|
|
||||||
if err := db.ApplySchema(ctx, nil); err != nil {
|
if err := db.ApplySchema(ctx, slog.Default()); err != nil {
|
||||||
t.Fatalf("ApplySchema: %v", err)
|
t.Fatalf("ApplySchema: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +93,7 @@ func testTablesExist(t *testing.T, ctx context.Context, db *SQLite) {
|
|||||||
|
|
||||||
func testSchemaIdempotent(t *testing.T, ctx context.Context, db *SQLite) {
|
func testSchemaIdempotent(t *testing.T, ctx context.Context, db *SQLite) {
|
||||||
// Apply schema again; should succeed without error.
|
// Apply schema again; should succeed without error.
|
||||||
if err := db.ApplySchema(ctx, nil); err != nil {
|
if err := db.ApplySchema(ctx, slog.Default()); err != nil {
|
||||||
t.Fatalf("second ApplySchema: %v", err)
|
t.Fatalf("second ApplySchema: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user