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

🐛 fix cloudflare image version management #4320

Merged
merged 4 commits into from
Dec 18, 2024
Merged

Conversation

ikesau
Copy link
Member

@ikesau ikesau commented Dec 16, 2024

Fixes #4313

Changes

  • Adds a replacedBy column to the images table
  • Adds a version column to the images table
  • Removes the unique filename constraint on the images column
  • Adds a unique constraint on filename, version
  • Updates all places that select "all images" to only select images where replacedBy is null
  • Updates images API functions to reflect these changes
    • the PUT route now sets the replacedBy column, and triggers a build
    • the DELETE route uses a CTE to find every image in the chain and deletes them all
      • it's possible that a user unpublishes an article and immediately deletes its images before the rebake has occurred, thus temporarily breaking the article before it's removed, but that seems unlikely

To test

CRUD

  1. Create three images
  2. Upload the first one, replace it with the second, and the third
  3. Confirm that all 3 images are still in the DB, and that replacedBy has been set correctly
  4. Delete the third image and confirm that all 3 images get deleted in Cloudflare and the DB

Baking

  1. Create 2 images
  2. Upload one of them, and publish it in an article
  3. Confirm the image works
  4. Update the image, and confirm that a bake is triggered
  5. Confirm that the article still works in the meantime
  6. Confirm that the updated image is used once the bake is finished

@ikesau ikesau requested a review from danyx23 December 16, 2024 23:48
@ikesau ikesau self-assigned this Dec 16, 2024
@owidbot
Copy link
Contributor

owidbot commented Dec 17, 2024

Quick links (staging server):

Site Dev Site Preview Admin Wizard Docs

Login: ssh owid@staging-site-fix-image-updating

SVG tester:

Number of differences (default views): 1389 (1c73fd) ❌
Number of differences (all views): 452 (0bf88d) ❌

Edited: 2024-12-18 17:04:40 UTC
Execution time: 1.29 seconds

@ikesau
Copy link
Member Author

ikesau commented Dec 18, 2024

Hey @danyx23 - I ended up adding a version column and applying UNIQUE(filename, version) to get around the MySQL 8 weirdness.

This way you can PUT multiple images with the same filename because the version will be incremented, but you can't POST a new image with the same filename because it will collide with the (replaced) version 0 original image.

Copy link
Contributor

@danyx23 danyx23 left a comment

Choose a reason for hiding this comment

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

Ah this is a nice idea - the version number carries a bit of additional information which is neat.

The only small change I would still aks is to expand the comment on the DB image type definition to mention that we considered using replacedBy alone and have a unique constraint but that this doesn't work with mysql. I would then also copy this comment to the migration file (since this is where you might end up when you try to find out about why the version was added).

Nice work!

@ikesau ikesau merged commit edd3a4d into master Dec 18, 2024
19 of 21 checks passed
@ikesau ikesau deleted the fix-image-updating branch December 18, 2024 19:14
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.

Cloudflare Images reuploads aren't working
3 participants