Some checks failed
Go Benchmark / Performance Regression Check (push) Has been cancelled
Cerberus Integration / Cerberus Security Stack Integration (push) Has been cancelled
Upload Coverage to Codecov / Backend Codecov Upload (push) Has been cancelled
Upload Coverage to Codecov / Frontend Codecov Upload (push) Has been cancelled
CodeQL - Analyze / CodeQL analysis (go) (push) Has been cancelled
CodeQL - Analyze / CodeQL analysis (javascript-typescript) (push) Has been cancelled
CrowdSec Integration / CrowdSec Bouncer Integration (push) Has been cancelled
Docker Build, Publish & Test / build-and-push (push) Has been cancelled
Quality Checks / Auth Route Protection Contract (push) Has been cancelled
Quality Checks / Codecov Trigger/Comment Parity Guard (push) Has been cancelled
Quality Checks / Backend (Go) (push) Has been cancelled
Quality Checks / Frontend (React) (push) Has been cancelled
Rate Limit integration / Rate Limiting Integration (push) Has been cancelled
Security Scan (PR) / Trivy Binary Scan (push) Has been cancelled
Supply Chain Verification (PR) / Verify Supply Chain (push) Has been cancelled
WAF integration / Coraza WAF Integration (push) Has been cancelled
Docker Build, Publish & Test / Security Scan PR Image (push) Has been cancelled
Repo Health Check / Repo health (push) Has been cancelled
History Rewrite Dry-Run / Dry-run preview for history rewrite (push) Has been cancelled
74 lines
2.1 KiB
Go
Executable File
74 lines
2.1 KiB
Go
Executable File
// 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
|
|
}
|