Skip to content

Commit

Permalink
🔨 align sync script and update hook
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Dec 14, 2023
1 parent 811ea0e commit 9d5fbf7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 14 additions & 4 deletions baker/postUpdatedHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import parseArgs from "minimist"
import { BAKE_ON_CHANGE } from "../settings/serverSettings.js"
import { DeployQueueServer } from "./DeployQueueServer.js"
import { exit } from "../db/cleanup.js"
import { PostRow } from "@ourworldindata/utils"
import { PostRow, extractFormattingOptions } from "@ourworldindata/utils"
import * as wpdb from "../db/wpdb.js"
import * as db from "../db/db.js"
import {
Expand Down Expand Up @@ -94,6 +94,9 @@ const syncPostToGrapher = async (
const existsInGrapher = !!matchingRows.length

const wpPost = rows[0]

const formattingOptions = extractFormattingOptions(wpPost.post_content)

const postRow = wpPost
? ({
id: wpPost.ID,
Expand All @@ -117,6 +120,7 @@ const syncPostToGrapher = async (
wpPost.created_at === zeroDateString
? "1970-01-01 00:00:00"
: wpPost.created_at,
formattingOptions: formattingOptions,
} as PostRow)
: undefined

Expand All @@ -130,13 +134,19 @@ const syncPostToGrapher = async (
)
postRow.content = contentWithBlocksInlined

const rowForDb = {
...postRow,
// TODO: it's not nice that we have to stringify this here
formattingOptions: JSON.stringify(postRow.formattingOptions),
}

if (!existsInGrapher)
await transaction.table(postsTable).insert(postRow)
await transaction.table(postsTable).insert(rowForDb)
else if (existsInGrapher)
await transaction
.table(postsTable)
.where("id", "=", postRow.id)
.update(postRow)
.where("id", "=", rowForDb.id)
.update(rowForDb)
}
})

Expand Down
4 changes: 2 additions & 2 deletions db/syncPostsToGrapher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ const syncPostsToGrapher = async (): Promise<void> => {
type: post.post_type,
status: post.post_status,
content: dereferenceReusableBlocksFn(content),
featured_image: post.featured_image || "",
published_at:
post.post_date_gmt === zeroDateString
? null
Expand All @@ -305,12 +306,11 @@ const syncPostsToGrapher = async (): Promise<void> => {
excerpt: post.post_excerpt,
created_at_in_wordpress:
post.created_at === zeroDateString ? null : post.created_at,
featured_image: post.featured_image || "",
formattingOptions: formattingOptions,
}
}) as PostRow[]
const postLinks = await PostLink.find()
const postLinksById = groupBy(postLinks, (link) => link.sourceId)
const postLinksById = groupBy(postLinks, (link: PostLink) => link.sourceId)

const linksToAdd: PostLink[] = []
const linksToDelete: PostLink[] = []
Expand Down

0 comments on commit 9d5fbf7

Please sign in to comment.