fix traceid of events not reset after factory reset

This commit is contained in:
AndReicscs
2026-06-10 16:23:27 +00:00
parent 5610dfceb2
commit 1d76266b3d
2 changed files with 22 additions and 13 deletions
+8 -6
View File
@@ -117,17 +117,18 @@ func (s *SQLiteStore) FactoryReset() error {
if err != nil {
return err
}
// Defers are safe here; tx.Rollback() is a no-op if tx.Commit() succeeds.
defer tx.Rollback()
defer tx.Rollback() // Rollback on any error.
// Wipe all tables (Order matters if foreign keys aren't set to CASCADE,
// though ours are, it is best practice to be explicit).
// Wipe all tables. The order is important for manual deletion without cascades.
// It is best practice to be explicit, even though ON DELETE CASCADE is enabled.
queries := []string{
"DELETE FROM events",
"DELETE FROM sensor_heartbeats",
"DELETE FROM node_sensors",
"DELETE FROM nodes",
"DELETE FROM config",
// Reset the autoincrement counter for the events table.
"DELETE FROM sqlite_sequence WHERE name = 'events'",
}
for _, q := range queries {
@@ -136,9 +137,10 @@ func (s *SQLiteStore) FactoryReset() error {
}
}
if err := tx.Commit(); err != nil {
// Re-initialize default config within the same transaction.
if err := initializeDefaultConfigTx(tx); err != nil {
return err
}
return InitializeDefaultConfig(s.DB)
return tx.Commit()
}
+14 -7
View File
@@ -113,6 +113,19 @@ func NewStore(dbPath string) (*SQLiteStore, error) {
}
func InitializeDefaultConfig(db *sql.DB) error {
tx, err := db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
if err := initializeDefaultConfigTx(tx); err != nil {
return err
}
return tx.Commit()
}
func initializeDefaultConfigTx(tx *sql.Tx) error {
defaults := map[string]string{
"is_armed": "true",
"webhook_type": "ntfy",
@@ -124,11 +137,6 @@ func InitializeDefaultConfig(db *sql.DB) error {
"siem_protocol": "tcp",
}
tx, err := db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare("INSERT OR IGNORE INTO config (key, value) VALUES (?, ?)")
if err != nil {
return err
@@ -137,10 +145,9 @@ func InitializeDefaultConfig(db *sql.DB) error {
for k, v := range defaults {
if _, err := stmt.Exec(k, v); err != nil {
tx.Rollback()
return err
}
}
return tx.Commit()
return nil
}