Fix noinlineerr findings: internal/database (refs #61)

This commit is contained in:
2026-08-07 16:59:56 +00:00
parent dca3c50cd2
commit bf1d3c6bad
4 changed files with 49 additions and 22 deletions

View File

@@ -148,7 +148,9 @@ func (r *BlobChunkRepository) DeleteOrphaned(ctx context.Context) error {
WHERE blobs.id = blob_chunks.blob_id WHERE blobs.id = blob_chunks.blob_id
) )
` `
if _, err := r.db.ExecWithLog(ctx, query1); err != nil {
_, err := r.db.ExecWithLog(ctx, query1)
if err != nil {
return fmt.Errorf("deleting blob_chunks with missing blobs: %w", err) return fmt.Errorf("deleting blob_chunks with missing blobs: %w", err)
} }
@@ -160,7 +162,9 @@ func (r *BlobChunkRepository) DeleteOrphaned(ctx context.Context) error {
WHERE chunks.chunk_hash = blob_chunks.chunk_hash WHERE chunks.chunk_hash = blob_chunks.chunk_hash
) )
` `
if _, err := r.db.ExecWithLog(ctx, query2); err != nil {
_, err = r.db.ExecWithLog(ctx, query2)
if err != nil {
return fmt.Errorf("deleting blob_chunks with missing chunks: %w", err) return fmt.Errorf("deleting blob_chunks with missing chunks: %w", err)
} }

View File

@@ -150,15 +150,18 @@ func TestChunkFileRepositoryComplexDeduplication(t *testing.T) {
file2 := &File{Path: "/file2.txt", MTime: testTime, Size: 3072, Mode: 0644, UID: 1000, GID: 1000} file2 := &File{Path: "/file2.txt", MTime: testTime, Size: 3072, Mode: 0644, UID: 1000, GID: 1000}
file3 := &File{Path: "/file3.txt", MTime: testTime, Size: 2048, Mode: 0644, UID: 1000, GID: 1000} file3 := &File{Path: "/file3.txt", MTime: testTime, Size: 2048, Mode: 0644, UID: 1000, GID: 1000}
if err := fileRepo.Create(ctx, nil, file1); err != nil { err := fileRepo.Create(ctx, nil, file1)
if err != nil {
t.Fatalf("failed to create file1: %v", err) t.Fatalf("failed to create file1: %v", err)
} }
if err := fileRepo.Create(ctx, nil, file2); err != nil { err = fileRepo.Create(ctx, nil, file2)
if err != nil {
t.Fatalf("failed to create file2: %v", err) t.Fatalf("failed to create file2: %v", err)
} }
if err := fileRepo.Create(ctx, nil, file3); err != nil { err = fileRepo.Create(ctx, nil, file3)
if err != nil {
t.Fatalf("failed to create file3: %v", err) t.Fatalf("failed to create file3: %v", err)
} }

View File

@@ -117,7 +117,8 @@ func New(ctx context.Context, path string) (*DB, error) {
log.Debug("Database opened successfully with WAL mode", "path", path) log.Debug("Database opened successfully with WAL mode", "path", path)
// Enable foreign keys explicitly // Enable foreign keys explicitly
if _, err := conn.ExecContext(ctx, "PRAGMA foreign_keys = ON"); err != nil { _, err = conn.ExecContext(ctx, "PRAGMA foreign_keys = ON")
if err != nil {
log.Warn("Failed to enable foreign keys", "error", err) log.Warn("Failed to enable foreign keys", "error", err)
} }
@@ -159,7 +160,8 @@ func New(ctx context.Context, path string) (*DB, error) {
conn.SetMaxOpenConns(1) conn.SetMaxOpenConns(1)
conn.SetMaxIdleConns(1) conn.SetMaxIdleConns(1)
if err := conn.PingContext(ctx); err != nil { err = conn.PingContext(ctx)
if err != nil {
log.Debug("Failed to ping database in recovery mode, closing", "path", path, "error", err) log.Debug("Failed to ping database in recovery mode, closing", "path", path, "error", err)
_ = conn.Close() _ = conn.Close()
@@ -175,17 +177,21 @@ func New(ctx context.Context, path string) (*DB, error) {
// Switch back to WAL mode // Switch back to WAL mode
log.Debug("Switching database back to WAL mode", "path", path) log.Debug("Switching database back to WAL mode", "path", path)
if _, err := conn.ExecContext(ctx, "PRAGMA journal_mode=WAL"); err != nil { _, err = conn.ExecContext(ctx, "PRAGMA journal_mode=WAL")
if err != nil {
log.Warn("Failed to switch back to WAL mode", "path", path, "error", err) log.Warn("Failed to switch back to WAL mode", "path", path, "error", err)
} }
// Ensure foreign keys are enabled // Ensure foreign keys are enabled
if _, err := conn.ExecContext(ctx, "PRAGMA foreign_keys=ON"); err != nil { _, err = conn.ExecContext(ctx, "PRAGMA foreign_keys=ON")
if err != nil {
log.Warn("Failed to enable foreign keys", "path", path, "error", err) log.Warn("Failed to enable foreign keys", "path", path, "error", err)
} }
db := &DB{conn: conn, path: path} db := &DB{conn: conn, path: path}
if err := applyMigrations(ctx, conn); err != nil {
err = applyMigrations(ctx, conn)
if err != nil {
_ = conn.Close() _ = conn.Close()
return nil, fmt.Errorf("applying migrations: %w", err) return nil, fmt.Errorf("applying migrations: %w", err)
@@ -323,7 +329,8 @@ func bootstrapMigrationsTable(ctx context.Context, db *sql.DB) error {
// the schema_migrations table via 000.sql, then iterates through remaining // the schema_migrations table via 000.sql, then iterates through remaining
// migration files in order. // migration files in order.
func applyMigrations(ctx context.Context, db *sql.DB) error { func applyMigrations(ctx context.Context, db *sql.DB) error {
if err := bootstrapMigrationsTable(ctx, db); err != nil { err := bootstrapMigrationsTable(ctx, db)
if err != nil {
return err return err
} }

View File

@@ -168,24 +168,30 @@ func TestApplyMigrations_Idempotent(t *testing.T) {
conn.SetMaxIdleConns(1) conn.SetMaxIdleConns(1)
// First run: apply all migrations. // First run: apply all migrations.
if err := applyMigrations(ctx, conn); err != nil { err = applyMigrations(ctx, conn)
if err != nil {
t.Fatalf("first applyMigrations failed: %v", err) t.Fatalf("first applyMigrations failed: %v", err)
} }
// Count rows in schema_migrations after first run. // Count rows in schema_migrations after first run.
var countBefore int var countBefore int
if err := conn.QueryRowContext(ctx, "SELECT COUNT(*) FROM schema_migrations").Scan(&countBefore); err != nil {
err = conn.QueryRowContext(ctx, "SELECT COUNT(*) FROM schema_migrations").Scan(&countBefore)
if err != nil {
t.Fatalf("failed to count schema_migrations after first run: %v", err) t.Fatalf("failed to count schema_migrations after first run: %v", err)
} }
// Second run: must be a no-op. // Second run: must be a no-op.
if err := applyMigrations(ctx, conn); err != nil { err = applyMigrations(ctx, conn)
if err != nil {
t.Fatalf("second applyMigrations failed: %v", err) t.Fatalf("second applyMigrations failed: %v", err)
} }
// Count rows in schema_migrations after second run — must be unchanged. // Count rows in schema_migrations after second run — must be unchanged.
var countAfter int var countAfter int
if err := conn.QueryRowContext(ctx, "SELECT COUNT(*) FROM schema_migrations").Scan(&countAfter); err != nil {
err = conn.QueryRowContext(ctx, "SELECT COUNT(*) FROM schema_migrations").Scan(&countAfter)
if err != nil {
t.Fatalf("failed to count schema_migrations after second run: %v", err) t.Fatalf("failed to count schema_migrations after second run: %v", err)
} }
@@ -213,9 +219,11 @@ func TestBootstrapMigrationsTable_FreshDatabase(t *testing.T) {
// Verify schema_migrations does NOT exist yet. // Verify schema_migrations does NOT exist yet.
var tableBefore int var tableBefore int
if err := conn.QueryRowContext(ctx,
err = conn.QueryRowContext(ctx,
"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='schema_migrations'", "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='schema_migrations'",
).Scan(&tableBefore); err != nil { ).Scan(&tableBefore)
if err != nil {
t.Fatalf("failed to check for table before bootstrap: %v", err) t.Fatalf("failed to check for table before bootstrap: %v", err)
} }
@@ -224,15 +232,18 @@ func TestBootstrapMigrationsTable_FreshDatabase(t *testing.T) {
} }
// Run bootstrap. // Run bootstrap.
if err := bootstrapMigrationsTable(ctx, conn); err != nil { err = bootstrapMigrationsTable(ctx, conn)
if err != nil {
t.Fatalf("bootstrapMigrationsTable failed: %v", err) t.Fatalf("bootstrapMigrationsTable failed: %v", err)
} }
// Verify schema_migrations now exists. // Verify schema_migrations now exists.
var tableAfter int var tableAfter int
if err := conn.QueryRowContext(ctx,
err = conn.QueryRowContext(ctx,
"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='schema_migrations'", "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='schema_migrations'",
).Scan(&tableAfter); err != nil { ).Scan(&tableAfter)
if err != nil {
t.Fatalf("failed to check for table after bootstrap: %v", err) t.Fatalf("failed to check for table after bootstrap: %v", err)
} }
@@ -242,9 +253,11 @@ func TestBootstrapMigrationsTable_FreshDatabase(t *testing.T) {
// Verify version 0 row exists. // Verify version 0 row exists.
var version int var version int
if err := conn.QueryRowContext(ctx,
err = conn.QueryRowContext(ctx,
"SELECT version FROM schema_migrations WHERE version = 0", "SELECT version FROM schema_migrations WHERE version = 0",
).Scan(&version); err != nil { ).Scan(&version)
if err != nil {
t.Fatalf("version 0 row not found in schema_migrations: %v", err) t.Fatalf("version 0 row not found in schema_migrations: %v", err)
} }