Skip to content

Commit

Permalink
Merge pull request #4274 from owid/cloudflare-images-prep
Browse files Browse the repository at this point in the history
🎉 cloudflare images DB setup
  • Loading branch information
ikesau authored Dec 10, 2024
2 parents 4e3bf99 + cf7ad74 commit 0f5903e
Show file tree
Hide file tree
Showing 4 changed files with 525 additions and 0 deletions.
39 changes: 39 additions & 0 deletions db/migration/1731360326761-CloudflareImages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class CloudflareImages1731360326761 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`-- sql
ALTER TABLE images
ADD COLUMN cloudflareId CHAR(36) NULL,
ADD CONSTRAINT images_cloudflareId_unique UNIQUE (cloudflareId),
ADD COLUMN hash VARCHAR(255) NULL,
MODIFY COLUMN googleId VARCHAR(255) NULL,
MODIFY COLUMN defaultAlt VARCHAR(1600) NULL;`)

// One-way migration 👋
await queryRunner.query(`-- sql
UPDATE images
SET defaultAlt = NULL
WHERE defaultAlt = 'legacy-wordpress-upload';
`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`-- sql
ALTER TABLE images
DROP COLUMN cloudflareId,
DROP COLUMN hash
`)

await queryRunner.query(`-- sql
UPDATE images
SET googleId = 'cloudflare_image'
WHERE googleId IS NULL
`)

await queryRunner.query(`-- sql
ALTER TABLE images
MODIFY COLUMN googleId VARCHAR(255) NOT NULL
`)
}
}
23 changes: 23 additions & 0 deletions db/migration/1732994843041-CloudflareImagesAddUserId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class CloudflareImagesAddUserId1732994843041
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`-- sql
ALTER TABLE images
ADD COLUMN userId INTEGER,
ADD CONSTRAINT fk_user_images
FOREIGN KEY (userId) REFERENCES users(id)
ON DELETE SET NULL;
`)
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`-- sql
ALTER TABLE images DROP CONSTRAINT fk_user_images;
`)
await queryRunner.query(`-- sql
ALTER TABLE images DROP COLUMN userId;
`)
}
}
Loading

0 comments on commit 0f5903e

Please sign in to comment.