Skip to content

[16.0][IMP] database_cleanup: purge orphaned attachments#3566

Open
Aldeigja wants to merge 1 commit intoOCA:16.0from
cetmix:16.0-t5258-database_cleanup-orphaned-attachments-clean
Open

[16.0][IMP] database_cleanup: purge orphaned attachments#3566
Aldeigja wants to merge 1 commit intoOCA:16.0from
cetmix:16.0-t5258-database_cleanup-orphaned-attachments-clean

Conversation

@Aldeigja
Copy link
Copy Markdown

Add wizard to list ir.attachment records whose store_fname points to a missing file on disk when storage is "file", and allow purging those rows.

@Aldeigja Aldeigja force-pushed the 16.0-t5258-database_cleanup-orphaned-attachments-clean branch from 699d8f0 to ab70051 Compare March 20, 2026 10:03
Copy link
Copy Markdown
Member

@StefanRijnhart StefanRijnhart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Judging from the name of the PR and the patterns from this module, I would have expected signalling attachments where the model no longer exists or the res_id is not present as a record. This makes sense as well, but to make it explicit what the issue is with each attachment (and to prepare for extension with other possible error conditions), would you mind adding a reason field to the wizard line model as is done in cleanup.purge.line, with for now a fixed reason like 'File missing in filestore'?

wizard = env["cleanup.purge.wizard.attachment"].create(
{
"purge_line_ids": [
(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use the more modern fields.Command.create?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out. Fixed.

IrModel = env.registry["ir.attachment"]
original_unlink = IrModel.unlink

def patched_unlink(self):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does unlink need to be patched? Isn't the exists check enough for the validation of the test?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch is necessary because this test validates the error-resilience path in the purge(). Regardless of whether unlink() raises a UserError for one attachment, the remaining attachments should still be purged. Without the patch, both attachments would be successfully deleted, and we would miss the exception branch entirely.

@Aldeigja Aldeigja force-pushed the 16.0-t5258-database_cleanup-orphaned-attachments-clean branch from b372147 to 95fb498 Compare April 3, 2026 23:00
@Aldeigja Aldeigja requested a review from StefanRijnhart April 3, 2026 23:12
Copy link
Copy Markdown
Member

@StefanRijnhart StefanRijnhart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates! Please squash commits.

attachment_id = fields.Many2one("ir.attachment")
reason = fields.Selection(
[
(REASON_MISSING_FILE, ("File missing in filestore")),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(REASON_MISSING_FILE, ("File missing in filestore")),
(REASON_MISSING_FILE, "File missing in filestore"),

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing.

"Attachment #%s cannot be deleted: %s",
attach.id,
str(exc),
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that you built this resilience, you will want to visualize this as well. Can you store the error message in a new column on the line model, and add it to the list view with optional="hide".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I've added the field and updated the view as well.

res.append(
(
0,
0,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another one for Command

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing that.

Add wizard to list ir.attachment records whose store_fname points to a
missing file on disk when storage is "file", and allow purging those
rows.

task 5258
@Aldeigja Aldeigja force-pushed the 16.0-t5258-database_cleanup-orphaned-attachments-clean branch from 95fb498 to e8b0061 Compare April 4, 2026 17:08
@Aldeigja Aldeigja requested a review from StefanRijnhart April 4, 2026 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants