Skip to content
Open
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
26 changes: 5 additions & 21 deletions apps/worker/src/tasks/sync-post/processor.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import processor from "./processor.ts";
import type { TaskInputs } from "@playfulprogramming/common";
import type { Job } from "bullmq";
import {
posts,
postData,
postAuthors,
collectionChapters,
db,
} from "@playfulprogramming/db";
import { posts, postData, postAuthors, db } from "@playfulprogramming/db";
import { s3 } from "@playfulprogramming/s3";
import * as github from "@playfulprogramming/github-api";
import { eq } from "drizzle-orm";
Expand Down Expand Up @@ -173,9 +167,6 @@ test("Links post to collection when collection is provided", async () => {
onConflictDoUpdate: vi.fn(),
});
const insertPostAuthorsValues = vi.fn();
const insertCollectionChaptersValues = vi.fn().mockReturnValue({
onConflictDoUpdate: vi.fn(),
});

vi.mocked(db.insert).mockImplementation((table) => {
if (table === posts) {
Expand All @@ -187,9 +178,6 @@ test("Links post to collection when collection is provided", async () => {
if (table === postAuthors) {
return { values: insertPostAuthorsValues } as never;
}
if (table === collectionChapters) {
return { values: insertCollectionChaptersValues } as never;
}
throw new Error(`Unexpected table: ${table}`);
});

Expand Down Expand Up @@ -251,15 +239,11 @@ order: 1
},
} as unknown as Job<TaskInputs["sync-post"]>);

// Assert: Collection chapter was inserted
expect(insertCollectionChaptersValues).toBeCalledWith({
locale: "en",
// Assert: Collection chapter was referenced
expect(insertPostsValues).toBeCalledWith({
slug: "example-post",
collectionSlug: "example-collection",
postSlug: "example-post",
title: "Chapter One",
description: "The first chapter",
url: "/example-author/posts/example-post",
order: 1,
collectionOrder: 1,
});
});

Expand Down
43 changes: 9 additions & 34 deletions apps/worker/src/tasks/sync-post/processor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Tasks, env } from "@playfulprogramming/common";
import {
db,
posts,
postData,
postAuthors,
collectionChapters,
} from "@playfulprogramming/db";
import { db, posts, postData, postAuthors } from "@playfulprogramming/db";
import * as github from "@playfulprogramming/github-api";
import { s3 } from "@playfulprogramming/s3";
import { createProcessor } from "../../createProcessor.ts";
Expand Down Expand Up @@ -131,7 +125,14 @@ export default createProcessor(Tasks.SYNC_POST, async (job, { signal }) => {
// Phase 3: Perform all database operations in a single transaction
// =========================================================================
await db.transaction(async (tx) => {
await tx.insert(posts).values({ slug: post }).onConflictDoNothing();
await tx
.insert(posts)
.values({
slug: post,
collectionSlug: collection,
collectionOrder: localeData[0]?.parsed?.order,
})
.onConflictDoNothing();

for (const { locale, parsed } of localeData) {
const postDataRecord = {
Expand Down Expand Up @@ -162,32 +163,6 @@ export default createProcessor(Tasks.SYNC_POST, async (job, { signal }) => {
});

console.log(`Saved post metadata for ${post} (${locale})`);

if (collection) {
const postUrl = `/${author}/posts/${post}`;

const chapterRecord = {
locale,
collectionSlug: collection,
postSlug: post,
title: parsed.title,
description: parsed.description,
url: postUrl,
order: parsed.order ?? 0,
};

await tx
.insert(collectionChapters)
.values(chapterRecord)
.onConflictDoUpdate({
target: [collectionChapters.postSlug, collectionChapters.locale],
set: chapterRecord,
});

console.log(
`Linked post ${post} to collection ${collection} (${locale})`,
);
}
}

await tx.delete(postAuthors).where(eq(postAuthors.postSlug, post));
Expand Down
7 changes: 7 additions & 0 deletions packages/db/drizzle/0003_famous_silver_samurai.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE "collection_chapters" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
DROP TABLE "collection_chapters" CASCADE;--> statement-breakpoint
ALTER TABLE "collection_data" ADD COLUMN "cover_image" text;--> statement-breakpoint
ALTER TABLE "collection_data" ADD COLUMN "social_image" text;--> statement-breakpoint
ALTER TABLE "posts" ADD COLUMN "collection_slug" text;--> statement-breakpoint
ALTER TABLE "posts" ADD COLUMN "collection_order" integer DEFAULT 0 NOT NULL;--> statement-breakpoint
ALTER TABLE "posts" ADD CONSTRAINT "posts_collection_slug_collections_slug_fk" FOREIGN KEY ("collection_slug") REFERENCES "public"."collections"("slug") ON DELETE set null ON UPDATE no action;
Loading