Skip to content

Commit

Permalink
✨ migration for booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Dec 8, 2023
1 parent c634943 commit 45782f3
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions db/migration/1701803220442-GdocFrontMatterBooleans.ts
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]
)
}
}
}

0 comments on commit 45782f3

Please sign in to comment.