-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActiveStorage foreign key enforcement and purge delete order #32584
Comments
+1 for both flipping the order of those operations and adding a foreign key. |
@georgeclaghorn Could I take a crack at this? unless @mauricew wants to |
Please do! Note that we want to avoid making service calls or enqueuing background jobs inside a database transaction. I think that’s the trickiest part of this. |
I'll probably open 2 PRs, one to handle the issue of ordering the |
@juliusdelta Sounds like a plan. |
I don’t think the changes proposed here would resolve either of those issues, but they’re nonetheless worthwhile. |
@claudiob I’ll see what the work/issues are like and respond on those issues accordingly tomorrow. |
Yea I'll see if I can wrap that stuff up in this patch @claudiob |
…b if attachments exist The issue rails#32584 was fixed in rails#33405 by adding foreign key constraint to the `active_storage_attachments` table for blobs. This commit implements fix on app-level in order to ensure that users can't delete a blob with attachments even if they don't have the foreign key constraint. See a related discussion in the Campfire: https://3.basecamp.com/3076981/buckets/24956/chats/12416418@1236718899 Note that, we should backport it to `5-2-stable` too. Related to rails#33405
This issue is twofold:
#purge
Problem information
So, to ensure maximum enforcement in the database, I would want to the attachments table to have an foreign key to the blobs table. I can easily do this by specifying it in the migration:
This works fine when initially adding an attachment.
But when I would delete it, the blob gets deleted first, then the attachment, creating a temporary case in which the FK wouldn't be used
Expected behavior
The purge process should complete successfully. However, I see that the process should go a bit differently than it currently does:
If it follows these steps, then the foreign key association is ok. Having the FK be a part of the default migration is another story, which I'm strongly suggesting with this.
Actual behavior
Blob gets deleted first, then attachment. Foreign key is not possible with this arrangement, and if something goes wrong this can lead to database orphans. Also it appears to do both deletes in separate transactions in a situation where this is successful.
System configuration
Rails version:
5.2.0
Ruby version:
2.4.4
Further information can be provided if necessary.
The text was updated successfully, but these errors were encountered: