Browse Source

[FIX] unlink cronjobs for the model before purging it

12.0-mig-module_prototyper_last
Holger Brunn 5 years ago
parent
commit
7795ef2742
  1. 7
      database_cleanup/models/purge_models.py
  2. 6
      database_cleanup/tests/test_database_cleanup.py

7
database_cleanup/models/purge_models.py

@ -73,6 +73,13 @@ class CleanupPurgeLineModel(models.TransientModel):
self.env['ir.model.constraint'].search([ self.env['ir.model.constraint'].search([
('model', '=', line.name), ('model', '=', line.name),
]).unlink() ]).unlink()
cronjobs = self.env['ir.cron'].with_context(
active_test=False
).search([
('model_id.model', '=', line.name),
])
if cronjobs:
cronjobs.unlink()
relations = self.env['ir.model.fields'].search([ relations = self.env['ir.model.fields'].search([
('relation', '=', row[1]), ('relation', '=', row[1]),
]).with_context(**context_flags) ]).with_context(**context_flags)

6
database_cleanup/tests/test_database_cleanup.py

@ -73,6 +73,11 @@ class TestDatabaseCleanup(TransactionCase):
'name': 'Database cleanup test model', 'name': 'Database cleanup test model',
'model': 'x_database.cleanup.test.model', 'model': 'x_database.cleanup.test.model',
}) })
# and a cronjob for it
cronjob = self.env['ir.cron'].create({
'name': 'testcronjob',
'model_id': self.models.id,
})
self.env.cr.execute( self.env.cr.execute(
'insert into ir_attachment (name, res_model, res_id, type) values ' 'insert into ir_attachment (name, res_model, res_id, type) values '
"('test attachment', 'database.cleanup.test.model', 42, 'binary')") "('test attachment', 'database.cleanup.test.model', 42, 'binary')")
@ -83,6 +88,7 @@ class TestDatabaseCleanup(TransactionCase):
self.assertFalse(self.env['ir.model'].search([ self.assertFalse(self.env['ir.model'].search([
('model', '=', 'x_database.cleanup.test.model'), ('model', '=', 'x_database.cleanup.test.model'),
])) ]))
self.assertFalse(cronjob.exists())
# create an orphaned table # create an orphaned table
self.env.cr.execute('create table database_cleanup_test (test int)') self.env.cr.execute('create table database_cleanup_test (test int)')

Loading…
Cancel
Save