From c14be36e05112bf236db80c66bfa9669441ec73a Mon Sep 17 00:00:00 2001 From: Jordan Wright Date: Sat, 15 Dec 2018 21:47:40 -0600 Subject: [PATCH] Delete maillogs if a campaign is deleted. Fixes #1318 --- models/campaign.go | 5 +++++ models/campaign_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/models/campaign.go b/models/campaign.go index c1e1af9b..fd9af73d 100644 --- a/models/campaign.go +++ b/models/campaign.go @@ -568,6 +568,11 @@ func DeleteCampaign(id int64) error { log.Error(err) return err } + err = db.Where("campaign_id=?", id).Delete(&MailLog{}).Error + if err != nil { + log.Error(err) + return err + } // Delete the campaign err = db.Delete(&Campaign{Id: id}).Error if err != nil { diff --git a/models/campaign_test.go b/models/campaign_test.go index b0f36d2b..9c399701 100644 --- a/models/campaign_test.go +++ b/models/campaign_test.go @@ -105,3 +105,17 @@ func (s *ModelsSuite) TestLaunchCampaignMaillogStatus(c *check.C) { c.Assert(m.Processing, check.Equals, false) } } + +func (s *ModelsSuite) TestDeleteCampaignAlsoDeletesMailLogs(c *check.C) { + campaign := s.createCampaign(c) + ms, err := GetMailLogsByCampaign(campaign.Id) + c.Assert(err, check.Equals, nil) + c.Assert(len(ms), check.Equals, len(campaign.Results)) + + err = DeleteCampaign(campaign.Id) + c.Assert(err, check.Equals, nil) + + ms, err = GetMailLogsByCampaign(campaign.Id) + c.Assert(err, check.Equals, nil) + c.Assert(len(ms), check.Equals, 0) +}