Skip to content

Commit

Permalink
✨ improve version explanation
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Dec 18, 2024
1 parent b65842b commit 7c227c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 7 additions & 0 deletions db/migration/1734369183234-CloudflareImagesReplacedBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export class CloudflareImagesReplacedBy1734369183234
await queryRunner.query(`-- sql
ALTER TABLE images
ADD COLUMN replacedBy INT NULL,
-- Necessary to create a unique constraint with filename, so that we can have multiple versions of the same image,
-- but not multiple images with the same filename and version.
-- i.e.
-- You can upload test.png and *replace* it with test.png, but you can't upload a *new* image named test.png,
-- because that would have a version of 0 and conflict with the first test.png that was uploaded
-- This was done because MySQL 8 doesn't support partial unique indexes (e.g. "UNIQUE(filename) WHERE replacedBy IS NULL")
-- Nor a unique constraint when the column is nullable (e.g. "UNIQUE(filename, replacedBy)" would allow multiple rows with ["test.png", null])
ADD COLUMN version INT NOT NULL DEFAULT 0,
ADD CONSTRAINT fk_images_replaced_by
FOREIGN KEY (replacedBy)
Expand Down
5 changes: 4 additions & 1 deletion packages/@ourworldindata/types/src/dbTypes/Images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ export interface DbInsertImage {
/**
* Necessary to create a unique constraint with filename, so that we can have multiple versions of the same image,
* but not multiple images with the same filename and version.
* i.e. you can upload test.png and *replace* it with test.png, but you can't upload a *new* image named test.png,
* i.e.
* You can upload test.png and *replace* it with test.png, but you can't upload a *new* image named test.png,
* because that would have a version of 0 and conflict with the first test.png that was uploaded
* This was done because MySQL 8 doesn't support partial unique indexes (e.g. "UNIQUE(filename) WHERE replacedBy IS NULL")
* Nor a unique constraint when the column is nullable (e.g. "UNIQUE(filename, replacedBy)" would allow multiple rows with ["test.png", null])
*/
version?: number
}
Expand Down

0 comments on commit 7c227c3

Please sign in to comment.