fix: 完善权威数据约束与测试隔离

This commit is contained in:
root
2026-09-14 11:34:07 +08:00
parent a2a5e4bd0f
commit c547e78a22
9 changed files with 600 additions and 160 deletions
+50 -22
View File
@@ -1,20 +1,46 @@
import { createHash } from "node:crypto";
import { PrismaClient } from "@prisma/client";
import {
assertSeedDatabaseAllowed,
DEFAULT_TEST_DATABASE_URL,
} from "./database-safety";
const prisma = new PrismaClient();
const databaseUrl = process.env.DATABASE_URL ?? DEFAULT_TEST_DATABASE_URL;
assertSeedDatabaseAllowed(databaseUrl);
const prisma = new PrismaClient({ datasources: { db: { url: databaseUrl } } });
const id = (value: string) => createHash("sha256").update(value).digest("hex");
async function seed(): Promise<void> {
const accounts = await Promise.all(
[
{ key: "demo-a", nickname: "海风" },
{ key: "demo-b", nickname: "星河" },
].map(({ key, nickname }) =>
{
key: "demo-a",
nickname: "海风",
publicId: "00000000-0000-4000-8000-000000000001",
},
{
key: "demo-b",
nickname: "星河",
publicId: "00000000-0000-4000-8000-000000000002",
},
].map(({ key, nickname, publicId }) =>
prisma.account.upsert({
where: { phoneHmac: id(`drift-bottle:${key}:phone-hmac`) },
update: {
anonymousProfile: {
upsert: { create: { nickname }, update: { nickname } },
upsert: {
create: {
publicId,
nickname,
avatarColor: "#66CCFF",
reviewStatus: "APPROVED",
},
update: {
nickname,
avatarColor: "#66CCFF",
reviewStatus: "APPROVED",
},
},
},
},
create: {
@@ -23,7 +49,14 @@ async function seed(): Promise<void> {
"hex",
),
phoneHmac: id(`drift-bottle:${key}:phone-hmac`),
anonymousProfile: { create: { nickname } },
anonymousProfile: {
create: {
publicId,
nickname,
avatarColor: "#66CCFF",
reviewStatus: "APPROVED",
},
},
},
}),
),
@@ -31,20 +64,17 @@ async function seed(): Promise<void> {
const author = accounts[0];
if (!author) throw new Error("Seed author was not created");
const bottleData = {
authorId: author.id,
contentText: "愿你今天遇见温柔。",
reviewStatus: "APPROVED" as const,
poolStatus: "IN_POOL" as const,
approvedAt: new Date(0),
};
await prisma.bottle.upsert({
where: { id: "00000000-0000-4000-8000-000000000101" },
update: {
authorId: author.id,
contentText: "愿你今天遇见温柔。",
status: "IN_POOL",
},
create: {
id: "00000000-0000-4000-8000-000000000101",
authorId: author.id,
contentText: "愿你今天遇见温柔。",
status: "IN_POOL",
},
update: bottleData,
create: { id: "00000000-0000-4000-8000-000000000101", ...bottleData },
});
console.info(
@@ -54,10 +84,8 @@ async function seed(): Promise<void> {
seed()
.catch((error: unknown) => {
console.error(
"Seed failed",
error instanceof Error ? error.message : "unknown error",
);
void error;
console.error("Seed failed.");
process.exitCode = 1;
})
.finally(async () => prisma.$disconnect());