-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm" | ||
|
||
export class GdocFrontMatterBooleans1701803220442 | ||
implements MigrationInterface | ||
{ | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
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<void> { | ||
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] | ||
) | ||
} | ||
} | ||
} |