chore: clean .gitignore cache

This commit is contained in:
GitHub Actions
2026-01-26 19:21:33 +00:00
parent 1b1b3a70b1
commit e5f0fec5db
1483 changed files with 0 additions and 472793 deletions

View File

@@ -1,80 +0,0 @@
// Package database handles database connections and migrations.
package database
import (
"database/sql"
"fmt"
"strings"
"github.com/Wikid82/charon/backend/internal/logger"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// Connect opens a SQLite database connection with optimized settings.
// Uses WAL mode for better concurrent read/write performance.
func Connect(dbPath string) (*gorm.DB, error) {
// Add SQLite performance pragmas if not already present
dsn := dbPath
if !strings.Contains(dsn, "?") {
dsn += "?"
} else {
dsn += "&"
}
// WAL mode: better concurrent access, faster writes
// busy_timeout: wait up to 5s instead of failing immediately on lock
// cache: shared cache for better memory usage
// synchronous=NORMAL: good balance of safety and speed
dsn += "_journal_mode=WAL&_busy_timeout=5000&_synchronous=NORMAL&_cache_size=-64000"
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{
// Skip default transaction for single operations (faster)
SkipDefaultTransaction: true,
// Prepare statements for reuse
PrepareStmt: true,
})
if err != nil {
return nil, fmt.Errorf("open database: %w", err)
}
// Configure connection pool
sqlDB, err := db.DB()
if err != nil {
return nil, fmt.Errorf("get underlying db: %w", err)
}
configurePool(sqlDB)
// Verify WAL mode is enabled and log confirmation
var journalMode string
if err := db.Raw("PRAGMA journal_mode").Scan(&journalMode).Error; err != nil {
logger.Log().WithError(err).Warn("Failed to verify SQLite journal mode")
} else {
logger.Log().WithField("journal_mode", journalMode).Info("SQLite database connected with WAL mode enabled")
}
// Run quick integrity check on startup (non-blocking, warn-only)
var quickCheckResult string
if err := db.Raw("PRAGMA quick_check").Scan(&quickCheckResult).Error; err != nil {
logger.Log().WithError(err).Warn("Failed to run SQLite integrity check on startup")
} else if quickCheckResult == "ok" {
logger.Log().Info("SQLite database integrity check passed")
} else {
// Database has corruption - log error but don't fail startup
logger.Log().WithField("quick_check_result", quickCheckResult).
WithField("error_type", "database_corruption").
Error("SQLite database integrity check failed - database may be corrupted")
}
return db, nil
}
// configurePool sets connection pool settings for SQLite.
// SQLite handles concurrency differently than server databases,
// so we use conservative settings.
func configurePool(sqlDB *sql.DB) {
// SQLite is file-based, so we limit connections
// but keep some idle for reuse
sqlDB.SetMaxOpenConns(1) // SQLite only allows one writer at a time
sqlDB.SetMaxIdleConns(1) // Keep one connection ready
sqlDB.SetConnMaxLifetime(0) // Don't close idle connections
}

View File

@@ -1,199 +0,0 @@
package database
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConnect(t *testing.T) {
t.Parallel()
// Test with memory DB
db, err := Connect("file::memory:?cache=shared")
assert.NoError(t, err)
assert.NotNil(t, db)
// Test with file DB
tempDir := t.TempDir()
dbPath := filepath.Join(tempDir, "test.db")
db, err = Connect(dbPath)
assert.NoError(t, err)
assert.NotNil(t, db)
}
func TestConnect_Error(t *testing.T) {
t.Parallel()
// Test with invalid path (directory)
tempDir := t.TempDir()
_, err := Connect(tempDir)
assert.Error(t, err)
}
func TestConnect_WALMode(t *testing.T) {
t.Parallel()
// Create a file-based database to test WAL mode
tempDir := t.TempDir()
dbPath := filepath.Join(tempDir, "wal_test.db")
db, err := Connect(dbPath)
require.NoError(t, err)
require.NotNil(t, db)
// Verify WAL mode is enabled
var journalMode string
err = db.Raw("PRAGMA journal_mode").Scan(&journalMode).Error
require.NoError(t, err)
assert.Equal(t, "wal", journalMode, "SQLite should be in WAL mode")
// Verify other PRAGMA settings
var busyTimeout int
err = db.Raw("PRAGMA busy_timeout").Scan(&busyTimeout).Error
require.NoError(t, err)
assert.Equal(t, 5000, busyTimeout, "busy_timeout should be 5000ms")
var synchronous int
err = db.Raw("PRAGMA synchronous").Scan(&synchronous).Error
require.NoError(t, err)
assert.Equal(t, 1, synchronous, "synchronous should be NORMAL (1)")
}
// Phase 2: database.go coverage tests
func TestConnect_InvalidDSN(t *testing.T) {
t.Parallel()
// Test with a directory path instead of a file path
// SQLite cannot open a directory as a database file
tmpDir := t.TempDir()
_, err := Connect(tmpDir)
assert.Error(t, err)
}
func TestConnect_IntegrityCheckCorrupted(t *testing.T) {
t.Parallel()
// Create a valid SQLite database
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "corrupt.db")
// First create a valid database
db, err := Connect(dbPath)
require.NoError(t, err)
db.Exec("CREATE TABLE test (id INTEGER, data TEXT)")
db.Exec("INSERT INTO test VALUES (1, 'test')")
// Close the database
sqlDB, _ := db.DB()
_ = sqlDB.Close()
// Corrupt the database file by overwriting with invalid data
// We'll overwrite the middle of the file to corrupt it
corruptDB(t, dbPath)
// Try to connect to corrupted database
// Connection may succeed but integrity check should detect corruption
db2, err := Connect(dbPath)
// Connection might succeed or fail depending on corruption type
if err != nil {
// If connection fails, that's also a valid outcome for corrupted DB
assert.Contains(t, err.Error(), "database")
} else {
// If connection succeeds, integrity check should catch it
// The Connect function logs the error but doesn't fail the connection
assert.NotNil(t, db2)
}
}
func TestConnect_PRAGMAVerification(t *testing.T) {
t.Parallel()
// Verify all PRAGMA settings are correctly applied
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "pragma_test.db")
db, err := Connect(dbPath)
require.NoError(t, err)
require.NotNil(t, db)
// Verify journal_mode
var journalMode string
err = db.Raw("PRAGMA journal_mode").Scan(&journalMode).Error
require.NoError(t, err)
assert.Equal(t, "wal", journalMode)
// Verify busy_timeout
var busyTimeout int
err = db.Raw("PRAGMA busy_timeout").Scan(&busyTimeout).Error
require.NoError(t, err)
assert.Equal(t, 5000, busyTimeout)
// Verify synchronous
var synchronous int
err = db.Raw("PRAGMA synchronous").Scan(&synchronous).Error
require.NoError(t, err)
assert.Equal(t, 1, synchronous, "synchronous should be NORMAL (1)")
}
func TestConnect_CorruptedDatabase_FullIntegrationScenario(t *testing.T) {
t.Parallel()
// Create a valid database with data
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "integration.db")
db, err := Connect(dbPath)
require.NoError(t, err)
// Create table and insert data
err = db.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)").Error
require.NoError(t, err)
err = db.Exec("INSERT INTO users (name) VALUES ('Alice'), ('Bob')").Error
require.NoError(t, err)
// Close database
sqlDB, _ := db.DB()
_ = sqlDB.Close()
// Corrupt the database
corruptDB(t, dbPath)
// Attempt to reconnect
db2, err := Connect(dbPath)
// The function logs errors but may still return a database connection
// depending on when corruption is detected
if err != nil {
assert.Contains(t, err.Error(), "database")
} else {
assert.NotNil(t, db2)
// Try to query - should fail or return error
var count int
err = db2.Raw("SELECT COUNT(*) FROM users").Scan(&count).Error
// Query might fail due to corruption
if err != nil {
assert.Contains(t, err.Error(), "database")
}
}
}
// Helper function to corrupt SQLite database
func corruptDB(t *testing.T, dbPath string) {
t.Helper()
// Open and corrupt file
f, err := os.OpenFile(dbPath, os.O_RDWR, 0o644)
require.NoError(t, err)
defer func() { _ = f.Close() }()
// Get file size
stat, err := f.Stat()
require.NoError(t, err)
size := stat.Size()
if size > 100 {
// Overwrite middle section with random bytes to corrupt B-tree structure
_, err = f.WriteAt([]byte("CORRUPTED_DATA_BLOCK"), size/2)
require.NoError(t, err)
} else {
// For small files, corrupt the header
_, err = f.WriteAt([]byte("CORRUPT"), 0)
require.NoError(t, err)
}
}

View File

@@ -1,73 +0,0 @@
// Package database handles database connections, migrations, and error detection.
package database
import (
"strings"
"github.com/Wikid82/charon/backend/internal/logger"
"gorm.io/gorm"
)
// SQLite corruption error indicators
var corruptionPatterns = []string{
"malformed",
"corrupt",
"disk I/O error",
"database disk image is malformed",
"file is not a database",
"file is encrypted or is not a database",
"database or disk is full",
}
// IsCorruptionError checks if the given error indicates SQLite database corruption.
// It detects errors like "database disk image is malformed", "corrupt", and related I/O errors.
func IsCorruptionError(err error) bool {
if err == nil {
return false
}
errStr := strings.ToLower(err.Error())
for _, pattern := range corruptionPatterns {
if strings.Contains(errStr, strings.ToLower(pattern)) {
return true
}
}
return false
}
// LogCorruptionError logs a database corruption error with structured context.
// The context map can include fields like "operation", "table", "query", "monitor_id", etc.
func LogCorruptionError(err error, context map[string]any) {
if err == nil {
return
}
entry := logger.Log().WithError(err)
// Add all context fields (range over nil map is safe)
for key, value := range context {
entry = entry.WithField(key, value)
}
// Mark as corruption error for alerting/monitoring
entry = entry.WithField("error_type", "database_corruption")
entry.Error("SQLite database corruption detected")
}
// CheckIntegrity runs PRAGMA quick_check and returns whether the database is healthy.
// Returns (healthy, message): healthy is true if database passes integrity check,
// message contains "ok" on success or the error/corruption message on failure.
func CheckIntegrity(db *gorm.DB) (healthy bool, message string) {
var result string
if err := db.Raw("PRAGMA quick_check").Scan(&result).Error; err != nil {
return false, "failed to run integrity check: " + err.Error()
}
// SQLite returns "ok" if the database passes integrity check
if strings.EqualFold(result, "ok") {
return true, "ok"
}
return false, result
}

View File

@@ -1,236 +0,0 @@
package database
import (
"errors"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIsCorruptionError(t *testing.T) {
t.Parallel()
tests := []struct {
name string
err error
expected bool
}{
{
name: "nil error",
err: nil,
expected: false,
},
{
name: "generic error",
err: errors.New("some random error"),
expected: false,
},
{
name: "database disk image is malformed",
err: errors.New("database disk image is malformed"),
expected: true,
},
{
name: "malformed in message",
err: errors.New("query failed: table is malformed"),
expected: true,
},
{
name: "corrupt database",
err: errors.New("database is corrupt"),
expected: true,
},
{
name: "disk I/O error",
err: errors.New("disk I/O error during read"),
expected: true,
},
{
name: "file is not a database",
err: errors.New("file is not a database"),
expected: true,
},
{
name: "file is encrypted or is not a database",
err: errors.New("file is encrypted or is not a database"),
expected: true,
},
{
name: "database or disk is full",
err: errors.New("database or disk is full"),
expected: true,
},
{
name: "case insensitive - MALFORMED uppercase",
err: errors.New("DATABASE DISK IMAGE IS MALFORMED"),
expected: true,
},
{
name: "wrapped error with corruption",
err: errors.New("failed to query: database disk image is malformed"),
expected: true,
},
{
name: "network error - not corruption",
err: errors.New("connection refused"),
expected: false,
},
{
name: "record not found - not corruption",
err: errors.New("record not found"),
expected: false,
},
{
name: "constraint violation - not corruption",
err: errors.New("UNIQUE constraint failed"),
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsCorruptionError(tt.err)
assert.Equal(t, tt.expected, result)
})
}
}
func TestLogCorruptionError(t *testing.T) {
t.Parallel()
t.Run("nil error does not panic", func(t *testing.T) {
// Should not panic
LogCorruptionError(nil, nil)
})
t.Run("logs with context", func(t *testing.T) {
// This just verifies it doesn't panic - actual log output is not captured
err := errors.New("database disk image is malformed")
ctx := map[string]any{
"operation": "GetMonitorHistory",
"table": "uptime_heartbeats",
"monitor_id": "test-uuid",
}
LogCorruptionError(err, ctx)
})
t.Run("logs without context", func(t *testing.T) {
err := errors.New("database corrupt")
LogCorruptionError(err, nil)
})
}
func TestCheckIntegrity(t *testing.T) {
t.Parallel()
t.Run("healthy database returns ok", func(t *testing.T) {
db, err := Connect("file::memory:?cache=shared")
require.NoError(t, err)
require.NotNil(t, db)
ok, result := CheckIntegrity(db)
assert.True(t, ok, "In-memory database should pass integrity check")
assert.Equal(t, "ok", result)
})
t.Run("file-based database passes check", func(t *testing.T) {
tmpDir := t.TempDir()
db, err := Connect(tmpDir + "/test.db")
require.NoError(t, err)
require.NotNil(t, db)
// Create a table and insert some data
err = db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)").Error
require.NoError(t, err)
err = db.Exec("INSERT INTO test (name) VALUES ('test')").Error
require.NoError(t, err)
ok, result := CheckIntegrity(db)
assert.True(t, ok)
assert.Equal(t, "ok", result)
})
}
// Phase 4 & 5: Deep coverage tests
func TestLogCorruptionError_EmptyContext(t *testing.T) {
t.Parallel()
// Test with empty context map
err := errors.New("database disk image is malformed")
emptyCtx := map[string]any{}
// Should not panic with empty context
LogCorruptionError(err, emptyCtx)
}
func TestCheckIntegrity_ActualCorruption(t *testing.T) {
t.Parallel()
// Create a SQLite database and corrupt it
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "corrupt_test.db")
// Create valid database
db, err := Connect(dbPath)
require.NoError(t, err)
// Insert some data
err = db.Exec("CREATE TABLE test (id INTEGER PRIMARY KEY, data TEXT)").Error
require.NoError(t, err)
err = db.Exec("INSERT INTO test (data) VALUES ('test1'), ('test2')").Error
require.NoError(t, err)
// Close connection
sqlDB, _ := db.DB()
_ = sqlDB.Close()
// Corrupt the database file
f, err := os.OpenFile(dbPath, os.O_RDWR, 0o644)
require.NoError(t, err)
stat, err := f.Stat()
require.NoError(t, err)
if stat.Size() > 100 {
// Overwrite middle section
_, err = f.WriteAt([]byte("CORRUPTED_DATA"), stat.Size()/2)
require.NoError(t, err)
}
_ = f.Close()
// Reconnect
db2, err := Connect(dbPath)
if err != nil {
// Connection failed due to corruption - that's a valid outcome
t.Skip("Database connection failed immediately")
}
// Run integrity check
ok, message := CheckIntegrity(db2)
// Should detect corruption
if !ok {
assert.False(t, ok)
assert.NotEqual(t, "ok", message)
assert.Contains(t, message, "database")
} else {
// Corruption might not be in checked pages
t.Log("Corruption not detected by quick_check - might be in unused pages")
}
}
func TestCheckIntegrity_PRAGMAError(t *testing.T) {
t.Parallel()
// Create database and close connection to cause PRAGMA to fail
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, "test.db")
db, err := Connect(dbPath)
require.NoError(t, err)
// Close the underlying SQL connection
sqlDB, err := db.DB()
require.NoError(t, err)
_ = sqlDB.Close()
// Now CheckIntegrity should fail because connection is closed
ok, message := CheckIntegrity(db)
assert.False(t, ok, "CheckIntegrity should fail on closed database")
assert.Contains(t, message, "failed to run integrity check")
}