fix(store): cascade delete related rows when removing a site
DeleteSite now removes maintenance_windows, check_history, and state_changes for the site within a transaction before deleting the site itself. Prevents orphaned rows. Closes #71
This commit was merged in pull request #92.
This commit is contained in:
@@ -152,10 +152,26 @@ func (s *SQLStore) UpdateSitePaused(id int, paused bool) error {
|
||||
}
|
||||
|
||||
func (s *SQLStore) DeleteSite(id int) error {
|
||||
_, err := s.db.Exec(s.q("DELETE FROM sites WHERE id=?"), id)
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
for _, q := range []string{
|
||||
"DELETE FROM maintenance_windows WHERE monitor_id = ?",
|
||||
"DELETE FROM check_history WHERE site_id = ?",
|
||||
"DELETE FROM state_changes WHERE site_id = ?",
|
||||
"DELETE FROM sites WHERE id = ?",
|
||||
} {
|
||||
if _, err := tx.Exec(s.q(q), id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
s.dialect.ResetSequenceOnEmpty(s.db, "sites")
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user