Skip to content

Commit

Permalink
change to autovacuum
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybackman committed Aug 8, 2024
1 parent 4e36e51 commit 34e286e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 36 deletions.
1 change: 0 additions & 1 deletion mail_tracking/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"installable": True,
"depends": ["mail"],
"data": [
"data/ir_cron_data.xml",
"data/tracking_data.xml",
"security/mail_tracking_email_security.xml",
"security/ir.model.access.csv",
Expand Down
19 changes: 0 additions & 19 deletions mail_tracking/data/ir_cron_data.xml

This file was deleted.

20 changes: 11 additions & 9 deletions mail_tracking/models/mail_tracking_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,24 +474,26 @@ def event_process(self, request, post, metadata, event_type=None):
# - return 'ERROR' if any error
return "NONE" # pragma: no cover

@api.model

def _get_old_mail_tracking_email_domain(self, max_age_days):
target_write_date = fields.Datetime.subtract(
fields.Datetime.now(), days=max_age_days
)
return [("write_date", "<", target_write_date)]

@api.autovacuum
def _gc_mail_tracking_email(self, max_age_days=180, limit=5000):
enable_deletion = (
self.env["ir.config_parameter"]
.sudo()
.get_param("mail_tracking.enable_mail_tracking_email_deletion_job")
.get_param("mail_tracking.enable_old_mail_tracking_email_deletion")
)

if not enable_deletion:
_logger.info("Mail tracking email deletion job is disabled.")
_logger.info("Mail tracking email deletion is disabled.")
return False

target_write_date = fields.Datetime.subtract(
fields.Datetime.now(), days=max_age_days
)
domain = [
("write_date", "<", target_write_date),
]
domain = self._get_old_mail_tracking_email_domain(max_age_days)
records_to_delete = self.search(domain, limit=limit)
_logger.debug("Deleting %s mail.tracking.email records", len(records_to_delete))
return records_to_delete.unlink()
8 changes: 4 additions & 4 deletions mail_tracking/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class ResConfigSettings(models.TransientModel):
related="company_id.mail_tracking_show_aliases",
readonly=False,
)
enable_mail_tracking_email_deletion_job = fields.Boolean(
enable_old_mail_tracking_email_deletion = fields.Boolean(
"Enable deletion of old mail tracking records",
config_parameter="mail_tracking.enable_mail_tracking_email_deletion_job",
help="Enables the job to delete old mail tracking records to reduce "
config_parameter="mail_tracking.enable_old_mail_tracking_email_deletion",
help="Enables the autovacuum to delete old mail tracking records to reduce "
"the database size. This sets an ir.config.parameter "
"mail_tracking.enable_mail_tracking_email_deletion_job",
"mail_tracking.enable_old_mail_tracking_email_deletion",
)
6 changes: 3 additions & 3 deletions mail_tracking/tests/test_gc_mail_tracking_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.settings = cls.env["res.config.settings"].create(
{"enable_mail_tracking_email_deletion_job": True}
{"enable_old_mail_tracking_email_deletion": True}
)
cls.settings.execute()
cls.partner = cls.env.ref("base.res_partner_address_28")
Expand Down Expand Up @@ -65,14 +65,14 @@ def test_deletion_follows_configuration_variable(self):
len(self.env["mail.tracking.email"].search(self.domain)), self.total_count
)
# when disabled, no deletions should happen
self.settings.enable_mail_tracking_email_deletion_job = False
self.settings.enable_old_mail_tracking_email_deletion = False
self.settings.execute()
self.env["mail.tracking.email"]._gc_mail_tracking_email()
self.assertEqual(
len(self.env["mail.tracking.email"].search(self.domain)), self.total_count
)
# when enabled, deletions should happen
self.settings.enable_mail_tracking_email_deletion_job = True
self.settings.enable_old_mail_tracking_email_deletion = True
self.settings.execute()
self.env["mail.tracking.email"]._gc_mail_tracking_email()
self.assertEqual(
Expand Down

0 comments on commit 34e286e

Please sign in to comment.