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) +}