Skip to content
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

Prevent unexpected data loss from concurrent editing #263

Open
bruvellu opened this issue Dec 2, 2023 · 1 comment
Open

Prevent unexpected data loss from concurrent editing #263

bruvellu opened this issue Dec 2, 2023 · 1 comment
Labels
feature Issues with ideas for new features

Comments

@bruvellu
Copy link
Owner

bruvellu commented Dec 2, 2023

Two users editing the metadata of the same image at the same time can lead to data loss. For example:

  1. Image with title field as “Old title”
  2. User A opens the editing form
  3. User B opens the editing form
  4. User B changes the title field to “New title from B”
  5. User B presses save
  6. User A changes another field without changing the title
  7. User A presses save
  8. Image is saved with the title “Old title” (edit from User B is lost)

Django has the select_for_update method, which blocks DB transactions for specific fields. But this is more of a low-level function to prevent concurrent transactions, when the block is released the waiting transaction is applied. Which in our case would also cause data loss.

@bruvellu bruvellu added the feature Issues with ideas for new features label Dec 2, 2023
@bruvellu
Copy link
Owner Author

bruvellu commented Dec 2, 2023

One solution is to have an is_locked = True/False boolean field in Media. Every time a user opens the editor, is_locked is set to True. If another user tries to edit the same image, it'll get an error saying the image is locked. Once the image is saved by the first user, is_locked is set to False. Now another user can edit the image using the up-to-date metadata.

To update the is_locked field, we should use the query set update() method instead of the model save() to avoid triggering unnecessarily auto-fields like the date_modified timestamp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Issues with ideas for new features
Projects
None yet
Development

No branches or pull requests

1 participant