From 45782f31fbc84bfbef875d12061161c829e05cee Mon Sep 17 00:00:00 2001 From: Ike Saunders Date: Tue, 5 Dec 2023 19:20:05 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20migration=20for=20booleans?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1701803220442-GdocFrontMatterBooleans.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 db/migration/1701803220442-GdocFrontMatterBooleans.ts diff --git a/db/migration/1701803220442-GdocFrontMatterBooleans.ts b/db/migration/1701803220442-GdocFrontMatterBooleans.ts new file mode 100644 index 00000000000..c209d7b1d8b --- /dev/null +++ b/db/migration/1701803220442-GdocFrontMatterBooleans.ts @@ -0,0 +1,48 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class GdocFrontMatterBooleans1701803220442 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + const gdocs: { id: string; content: string }[] = + await queryRunner.query( + `SELECT id, content FROM posts_gdocs WHERE published = TRUE AND (content LIKE '%"true"%' OR content LIKE '%"false"%')` + ) + + for (const gdoc of gdocs) { + const content = JSON.parse(gdoc.content) + for (const [key, value] of Object.entries(content)) { + if (typeof value === "string") { + if (value === "true") { + content[key] = true + } else if (value === "false") { + content[key] = false + } + } + } + await queryRunner.query( + `UPDATE posts_gdocs SET content = ? WHERE id = ?`, + [JSON.stringify(content), gdoc.id] + ) + } + } + + public async down(queryRunner: QueryRunner): Promise { + const gdocs: { id: string; content: string }[] = + await queryRunner.query( + `SELECT id, content FROM posts_gdocs WHERE published = TRUE AND (content LIKE '%: true%' OR content LIKE '%: false%')` + ) + for (const gdoc of gdocs) { + const content = JSON.parse(gdoc.content) + for (const [key, value] of Object.entries(content)) { + if (typeof value === "boolean") { + content[key] = value ? "true" : "false" + } + } + await queryRunner.query( + `UPDATE posts_gdocs SET content = ? WHERE id = ?`, + [JSON.stringify(content), gdoc.id] + ) + } + } +}