Skip to content
Closed
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
5 changes: 5 additions & 0 deletions .changeset/fuzzy-wolves-filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"emdash": patch
---

Fixes default category and tag taxonomies ignoring a site's configured collections during initial seeding, which caused taxonomy filters to return no matching content for collections other than `posts`.
27 changes: 26 additions & 1 deletion packages/core/src/seed/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@ const SANITIZE_PATTERN = /[^a-zA-Z0-9_-]/g;
/** Pattern to collapse multiple hyphens */
const MULTIPLE_HYPHENS_PATTERN = /-+/g;

function isUntouchedBuiltInTaxonomy(definition: {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[suggestion] This function duplicates the factory defaults already defined in packages/core/src/database/migrations/006_taxonomy_defs.ts (ids, labels, hierarchical, and the `[

id: string;
name: string;
label: string;
label_singular: string | null;
hierarchical: number;
collections: string | null;
}): boolean {
const expected =
definition.name === "category"
? { id: "taxdef_category", label: "Categories", labelSingular: "Category", hierarchical: 1 }
: definition.name === "tag"
? { id: "taxdef_tag", label: "Tags", labelSingular: "Tag", hierarchical: 0 }
: null;

return (
expected !== null &&
definition.id === expected.id &&
definition.label === expected.label &&
definition.label_singular === expected.labelSingular &&
definition.hierarchical === expected.hierarchical &&
definition.collections === '["posts"]'
);
}

/**
* Apply a seed file to the database
*
Expand Down Expand Up @@ -317,7 +342,7 @@ export async function applySeed(
if (onConflict === "error") {
throw new Error(`Conflict: taxonomy "${taxonomy.name}" (${defLocale}) already exists`);
}
if (onConflict === "update") {
if (onConflict === "update" || isUntouchedBuiltInTaxonomy(existingDef)) {
await db
.updateTable("_emdash_taxonomy_defs")
.set({
Expand Down
36 changes: 36 additions & 0 deletions packages/core/tests/integration/seed-on-conflict.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,42 @@ describe("applySeed onConflict modes", () => {
});

describe("onConflict: skip (default)", () => {
it.each([
["category", "Categories", true],
["tag", "Tags", false],
] as const)(
"replaces the untouched built-in %s taxonomy definition",
async (name, label, builtInHierarchical) => {
const seed: SeedFile = {
version: "1",
taxonomies: [{ name, label, hierarchical: false, collections: ["projects"] }],
};

await applySeed(db, seed, { onConflict: "skip" });

const taxonomy = await db
.selectFrom("_emdash_taxonomy_defs")
.select(["collections", "hierarchical"])
.where("name", "=", name)
.executeTakeFirstOrThrow();
expect(taxonomy).toMatchObject({ collections: '["projects"]', hierarchical: 0 });

seed.taxonomies![0]!.collections = ["posts"];
seed.taxonomies![0]!.hierarchical = builtInHierarchical;
await applySeed(db, seed, { onConflict: "skip" });

const preservedTaxonomy = await db
.selectFrom("_emdash_taxonomy_defs")
.select(["collections", "hierarchical"])
.where("name", "=", name)
.executeTakeFirstOrThrow();
expect(preservedTaxonomy).toMatchObject({
collections: '["projects"]',
hierarchical: 0,
});
},
);

it("skips existing collections", async () => {
const seed = createTestSeed();
// First apply
Expand Down
Loading