From 9ef33f4d4320f844851132e6317c642c40e6718f Mon Sep 17 00:00:00 2001 From: Julien Castiaux Date: Mon, 15 Apr 2019 09:57:09 +0000 Subject: [PATCH] [FIX] mail: File deletion on module uninstallation PR #30798 introduced thread, message, and attachments deletion on a module uninstallation. When removing attachments it was also removing files in the filestore even if those files were still in use by other attachments. opw-1968117 closes odoo/odoo#32682 Signed-off-by: Nicolas Martinelli (nim) --- addons/mail/models/ir_model.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/addons/mail/models/ir_model.py b/addons/mail/models/ir_model.py index 46319ea38d4..8bf002a6230 100644 --- a/addons/mail/models/ir_model.py +++ b/addons/mail/models/ir_model.py @@ -25,13 +25,23 @@ class IrModel(models.Model): query = "DELETE FROM mail_message WHERE model in %s" self.env.cr.execute(query, [models]) + # Get files attached solely by the models query = """ - DELETE FROM ir_attachment - WHERE res_model in %s - RETURNING store_fname + SELECT DISTINCT store_fname + FROM ir_attachment + WHERE res_model IN %s + EXCEPT + SELECT store_fname + FROM ir_attachment + WHERE res_model not IN %s; """ + self.env.cr.execute(query, [models, models]) + fnames = self.env.cr.fetchall() + + query = """DELETE FROM ir_attachment WHERE res_model in %s""" self.env.cr.execute(query, [models]) - for (fname,) in self.env.cr.fetchall(): + + for (fname,) in fnames: self.env['ir.attachment']._file_delete(fname) return super(IrModel, self).unlink()