REFACTORED - migrate the data layer from Prisma to Drizzle (unify the stack)
- Add lib/db (Drizzle schema: 7 tables + 6 enums + relations, postgres.js client), drizzle.config.ts, lib/db/enums.ts (Prisma-compatible enum objects) - Rewrite all 14 app consumers + 4 admin components to Drizzle - Move the test DB layer to Drizzle + in-process PGlite; convert all 8 integration test files + factories (371 tests green) - Remove Prisma: deps, prisma/ schema+migrations, lib/prisma.ts, Dockerfile prisma generate; wire db:generate/migrate/push/studio to drizzle-kit; update Makefile - Preserve the old seed as scripts/legacy-prisma-seed.cjs (needs a Drizzle rewrite)
This commit is contained in:
@@ -16,7 +16,15 @@ import {
|
||||
saveProjectAction,
|
||||
upsertCategoryAction,
|
||||
} from "@/app/_admin/portfolio/actions";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
|
||||
import { db } from "@/lib/db";
|
||||
import {
|
||||
category as categoryTable,
|
||||
mediaUsage as mediaUsageTable,
|
||||
portfolioAsset as portfolioAssetTable,
|
||||
portfolioProject as portfolioProjectTable,
|
||||
} from "@/lib/db/schema";
|
||||
import { createCategory, createProject } from "@/tests/helpers/factories";
|
||||
import { adminAuth, captureRedirect, formDataFrom, resetNextMocks } from "@/tests/helpers/next-mocks";
|
||||
|
||||
@@ -81,7 +89,7 @@ describe("upsertCategoryAction", () => {
|
||||
it("creates a category", async () => {
|
||||
const url = await captureRedirect(() => upsertCategoryAction(categoryForm()));
|
||||
expect(url).toContain("success=");
|
||||
const category = await prisma.category.findUnique({ where: { slug: "branding" } });
|
||||
const category = await db.query.category.findFirst({ where: eq(categoryTable.slug, "branding") });
|
||||
expect(category?.nameEn).toBe("Branding");
|
||||
expect(category?.isActive).toBe(true);
|
||||
});
|
||||
@@ -92,7 +100,7 @@ describe("upsertCategoryAction", () => {
|
||||
upsertCategoryAction(categoryForm({ id: existing.id, slug: "old", nameEn: "Renamed" })),
|
||||
);
|
||||
expect(url).toContain("success=");
|
||||
const category = await prisma.category.findUnique({ where: { id: existing.id } });
|
||||
const category = await db.query.category.findFirst({ where: eq(categoryTable.id, existing.id) });
|
||||
expect(category?.nameEn).toBe("Renamed");
|
||||
});
|
||||
|
||||
@@ -121,14 +129,14 @@ describe("deleteCategoryAction", () => {
|
||||
await createProject({ categoryId: category.id });
|
||||
const url = await captureRedirect(() => deleteCategoryAction(formDataFrom({ id: category.id })));
|
||||
expect(url).toContain("error=");
|
||||
expect(await prisma.category.findUnique({ where: { id: category.id } })).not.toBeNull();
|
||||
expect((await db.query.category.findFirst({ where: eq(categoryTable.id, category.id) })) ?? null).not.toBeNull();
|
||||
});
|
||||
|
||||
it("deletes an empty category", async () => {
|
||||
const category = await createCategory();
|
||||
const url = await captureRedirect(() => deleteCategoryAction(formDataFrom({ id: category.id })));
|
||||
expect(url).toContain("success=");
|
||||
expect(await prisma.category.findUnique({ where: { id: category.id } })).toBeNull();
|
||||
expect((await db.query.category.findFirst({ where: eq(categoryTable.id, category.id) })) ?? null).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -138,16 +146,14 @@ describe("saveProjectAction", () => {
|
||||
const url = await captureRedirect(() => saveProjectAction(projectForm(category.id)));
|
||||
expect(url).toContain("success=");
|
||||
|
||||
const project = await prisma.portfolioProject.findUnique({ where: { slug: "case-study" } });
|
||||
const project = await db.query.portfolioProject.findFirst({ where: eq(portfolioProjectTable.slug, "case-study") });
|
||||
expect(project).not.toBeNull();
|
||||
expect(project?.isPublished).toBe(true);
|
||||
expect(project?.publishedAt).not.toBeNull();
|
||||
expect(project?.coverImagePath).toBe("https://cdn/cover.png");
|
||||
|
||||
expect(await prisma.portfolioAsset.count({ where: { projectId: project!.id } })).toBe(1);
|
||||
const usages = await prisma.mediaUsage.findMany({
|
||||
where: { entityType: "portfolio-project", entityId: project!.id },
|
||||
});
|
||||
expect(await db.$count(portfolioAssetTable, eq(portfolioAssetTable.projectId, project!.id))).toBe(1);
|
||||
const usages = await db.select().from(mediaUsageTable).where(and(eq(mediaUsageTable.entityType, "portfolio-project"), eq(mediaUsageTable.entityId, project!.id)));
|
||||
const usageTypes = usages.map((u) => u.usageType).sort();
|
||||
expect(usageTypes).toEqual(["PORTFOLIO_ASSET", "PORTFOLIO_COVER"]);
|
||||
});
|
||||
@@ -156,26 +162,26 @@ describe("saveProjectAction", () => {
|
||||
const category = await createCategory();
|
||||
const created = await captureRedirect(() => saveProjectAction(projectForm(category.id)));
|
||||
void created;
|
||||
const project = await prisma.portfolioProject.findUnique({ where: { slug: "case-study" } });
|
||||
const project = await db.query.portfolioProject.findFirst({ where: eq(portfolioProjectTable.slug, "case-study") });
|
||||
|
||||
const url = await captureRedirect(() =>
|
||||
saveProjectAction(projectForm(category.id, { id: project!.id, titleEn: "Updated Title" })),
|
||||
);
|
||||
expect(url).toContain("success=");
|
||||
const updated = await prisma.portfolioProject.findUnique({ where: { id: project!.id } });
|
||||
const updated = await db.query.portfolioProject.findFirst({ where: eq(portfolioProjectTable.id, project!.id) });
|
||||
expect(updated?.titleEn).toBe("Updated Title");
|
||||
// assets are replaced, not duplicated
|
||||
expect(await prisma.portfolioAsset.count({ where: { projectId: project!.id } })).toBe(1);
|
||||
expect(await db.$count(portfolioAssetTable, eq(portfolioAssetTable.projectId, project!.id))).toBe(1);
|
||||
});
|
||||
|
||||
it("keeps the original publishedAt when re-saving an already published project", async () => {
|
||||
const category = await createCategory();
|
||||
await captureRedirect(() => saveProjectAction(projectForm(category.id)));
|
||||
const first = await prisma.portfolioProject.findUnique({ where: { slug: "case-study" } });
|
||||
const first = await db.query.portfolioProject.findFirst({ where: eq(portfolioProjectTable.slug, "case-study") });
|
||||
const originalPublishedAt = first!.publishedAt;
|
||||
|
||||
await captureRedirect(() => saveProjectAction(projectForm(category.id, { id: first!.id })));
|
||||
const second = await prisma.portfolioProject.findUnique({ where: { id: first!.id } });
|
||||
const second = await db.query.portfolioProject.findFirst({ where: eq(portfolioProjectTable.id, first!.id) });
|
||||
expect(second?.publishedAt?.toISOString()).toBe(originalPublishedAt?.toISOString());
|
||||
});
|
||||
|
||||
@@ -183,7 +189,7 @@ describe("saveProjectAction", () => {
|
||||
const category = await createCategory();
|
||||
const url = await captureRedirect(() => saveProjectAction(projectForm(category.id, { titleEn: "" })));
|
||||
expect(url).toContain("error=");
|
||||
expect(await prisma.portfolioProject.count()).toBe(0);
|
||||
expect(await db.$count(portfolioProjectTable)).toBe(0);
|
||||
});
|
||||
|
||||
it("reports a unique-constraint violation on duplicate slugs", async () => {
|
||||
@@ -206,7 +212,7 @@ describe("deleteProjectAction", () => {
|
||||
const project = await createProject();
|
||||
const url = await captureRedirect(() => deleteProjectAction(formDataFrom({ id: project.id })));
|
||||
expect(url).toContain("success=");
|
||||
expect(await prisma.portfolioProject.findUnique({ where: { id: project.id } })).toBeNull();
|
||||
expect((await db.query.portfolioProject.findFirst({ where: eq(portfolioProjectTable.id, project.id) })) ?? null).toBeNull();
|
||||
});
|
||||
|
||||
it("errors when the project does not exist", async () => {
|
||||
|
||||
Reference in New Issue
Block a user