Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include featured image in wpPost -> ArchieML migration #2278

Merged
merged 5 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions baker/postUpdatedHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,36 @@ const syncPostToGrapher = async (
left join wp_term_taxonomy t on t.term_taxonomy_id = r.term_taxonomy_id
WHERE t.taxonomy = 'author' and p.ID = ?
group by p.ID
)
),
post_featured_image AS (
SELECT
p.ID,
(
SELECT
meta_value
FROM
wp_postmeta
WHERE
post_id = p.ID
AND meta_key = '_thumbnail_id') AS featured_image_id
FROM
wp_posts p
WHERE
p.ID = ?
)
-- finally here we select all the fields from posts_with_authors and
-- then we join in the first_revision to get the created_at field
select
p.*,
pwa.authors,
fr.created_at as created_at
fr.created_at as created_at,
regexp_replace((SELECT guid FROM wp_posts WHERE ID = fi.featured_image_id), '^https://owid.cloud/(app|wp-content)/', 'https://ourworldindata.org/wp-content/') AS featured_image
from wp_posts p
left join post_ids_with_authors pwa on pwa.id = p.id
left join first_revision fr on fr.post_id = pwa.id
left join first_revision fr on fr.post_id = p.id
left join post_featured_image fi on fi.ID = p.id
where p.id = ?`,
[postId, postId, postId]
[postId, postId, postId, postId]
)
const dereferenceReusableBlocksFn = await buildReusableBlocksResolver()

Expand All @@ -80,6 +98,7 @@ const syncPostToGrapher = async (
type: wpPost.post_type,
status: wpPost.post_status,
content: wpPost.post_content,
featured_image: wpPost.featured_image || "",
published_at:
wpPost.post_date_gmt === zeroDateString
? null
Expand Down
4 changes: 3 additions & 1 deletion db/migrateWpPostsToArchieMl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ const migrate = async (): Promise<void> => {
"authors",
"excerpt",
"created_at_in_wordpress",
"updated_at"
"updated_at",
"featured_image"
).from(db.knexTable(Post.postsTable)) //.where("id", "=", "29766"))

for (const post of posts) {
Expand Down Expand Up @@ -159,6 +160,7 @@ const migrate = async (): Promise<void> => {
subtitle: post.excerpt,
excerpt: post.excerpt,
authors: parsePostAuthors(post.authors),
"featured-image": post.featured_image,
dateline: dateline,
// TODO: this discards block level elements - those might be needed?
refs: undefined,
Expand Down
17 changes: 17 additions & 0 deletions db/migration/1685653967284-PostsFeaturedImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class PostsFeaturedImage1685653967284 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
queryRunner.query(`
ALTER TABLE posts
ADD COLUMN featured_image VARCHAR(1024) NOT NULL DEFAULT "";
`)
}

public async down(queryRunner: QueryRunner): Promise<void> {
queryRunner.query(`
ALTER TABLE posts
DROP COLUMN featured_image;
`)
}
}
22 changes: 20 additions & 2 deletions db/syncPostsToGrapher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,33 @@ const syncPostsToGrapher = async (): Promise<void> => {
from wp_posts p
left join posts_authors pa on p.ID = pa.id
group by p.ID
)
),
post_featured_image AS (
SELECT
p.ID,
(
SELECT
meta_value
FROM
wp_postmeta
WHERE
post_id = p.ID
AND meta_key = '_thumbnail_id') AS featured_image_id
FROM
wp_posts p
)
-- Finally collect all the fields we want to keep - this is everything from wp_posts, the authors from the
-- posts_with_authors CTE and the created_at from the first_revision CTE
select
p.*,
pwa.authors as authors,
fr.created_at as created_at
fr.created_at as created_at,
-- select the featured image url and normalize the to point to our full domain at the wp-content folder
regexp_replace((SELECT guid FROM wp_posts WHERE ID = fi.featured_image_id), '^https://owid.cloud/(app|wp-content)/', 'https://ourworldindata.org/wp-content/') AS featured_image
from wp_posts p
left join post_ids_with_authors pwa on p.ID = pwa.ID
left join first_revision fr on fr.post_id = pwa.ID
left join post_featured_image fi on fi.ID = p.id
where p.post_type in ('post', 'page') AND post_status != 'trash'
`
)
Expand Down Expand Up @@ -202,6 +219,7 @@ 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 || "",
}
}) as PostRow[]

Expand Down
1 change: 1 addition & 0 deletions packages/@ourworldindata/utils/src/owidTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export interface PostRow {
authors: string
excerpt: string
created_at_in_wordpress: Date | null
featured_image: string
}

export interface PostRowWithGdocPublishStatus extends PostRow {
Expand Down
Loading