[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) <nim@odoo.com>
This commit is contained in:
Julien Castiaux
2019-04-15 12:40:44 +00:00
parent 8906807669
commit 9ef33f4d43
+14 -4
View File
@@ -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()