Skip to content
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
31 changes: 26 additions & 5 deletions prisma/seed/idempotent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,38 @@ export async function resetSeedScope(prisma: PrismaClient): Promise<void> {
});
}

// 1) 유저 종속 (주문, 리뷰, 찜, 최근본, 알림, 드래프트, 검색기록, 카트, 인증세션)
if (userIds.length > 0) {
// 1) 주문 — 사용자 조건과 분리한다. 재시드가 2) 상품 삭제에서 실패하면 시드 사용자는 이미 지워져
// 시드 매장과 막는 주문만 남으므로, 그 상태에서 다시 돌려도 복구돼야 한다
if (userIds.length > 0 || storeIds.length > 0) {
// 주문 종속들 → 주문 본체 (FK depth가 깊으므로 안에서 다시 처리)
const userOrders = await prisma.order.findMany({
where: { account_id: { in: userIds } },
// 시드 밖 계정이 시드 매장 상품으로 넣은 주문도 지운다 — 품목·리뷰가 시드 상품·매장 FK(RESTRICT)에 걸린다.
// 주문은 상품 하나로 만들어져 매장도 하나라, 시드 밖 매장 품목이 함께 지워지지 않는다
const seedOrders = await prisma.order.findMany({
where: {
OR: [
{ account_id: { in: userIds } },
{ items: { some: { store_id: { in: storeIds } } } },

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Run seed-store order cleanup without requiring seed users

When retrying after the original failure, this predicate is never reached because the entire order-cleanup block remains guarded by userIds.length > 0. The previous non-transactional reset deletes the seed users before reaching product deletion, where an outsider's order item causes the FK failure; consequently, the database state this fix must recover has seed stores and the blocking order but no seed users. A subsequent seed attempt still skips this query and fails at product deletion again, requiring manual cleanup instead of restoring idempotency. Move the seed-store order cleanup outside the user-only guard (while retaining user-specific cleanup separately).

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

반영: 주문 정리 블록을 userIds.length > 0 밖의 별도 블록(userIds.length > 0 || storeIds.length > 0)으로 이동. 시드 사용자 계정은 1) 끝에서 지워지고 실패는 2) 상품 삭제에서 나므로, 이미 한 번 실패한 DB(시드 사용자 없음 + 시드 매장·막는 주문 잔존)가 이번 수정으로도 복구되지 않던 경로가 맞음. 시드 매장만 있는 상태 재현 테스트 추가.

],
},
select: { id: true },
});
const orderIds = userOrders.map((o) => o.id);
const orderIds = seedOrders.map((o) => o.id);
if (orderIds.length > 0) {
const orderItems = await prisma.orderItem.findMany({
where: { order_id: { in: orderIds } },
select: { id: true },
});
const orderItemIds = orderItems.map((i) => i.id);
// 시드 밖 계정이 받은 이 주문·리뷰 알림 — 남기면 SET NULL로 대상 없는 알림이 된다
await prisma.notification.deleteMany({
where: {
OR: [
{ order_id: { in: orderIds } },
{ order_item_id: { in: orderItemIds } },
{ review: { order_item_id: { in: orderItemIds } } },
],
},
});
if (orderItemIds.length > 0) {
// OrderItemCustomFreeEdit -> attachments -> deletes
const freeEdits = await prisma.orderItemCustomFreeEdit.findMany({
Expand Down Expand Up @@ -125,7 +143,10 @@ export async function resetSeedScope(prisma: PrismaClient): Promise<void> {
});
await prisma.order.deleteMany({ where: { id: { in: orderIds } } });
}
}

// 1-2) 유저 종속 (리뷰, 찜, 최근본, 알림, 드래프트, 검색기록, 카트, 인증세션)
if (userIds.length > 0) {
// 리뷰: 주문 종속에서 이미 처리되었지만, 다른 리뷰가 있을 수 있음 → 사용자 기준으로도 한 번 더
await prisma.reviewLike.deleteMany({
where: { account_id: { in: userIds } },
Expand Down
87 changes: 86 additions & 1 deletion src/test/seed-reset-scope.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { seedCategories } from '../../prisma/seed/categories';
import { resetSeedScope } from '../../prisma/seed/idempotent';
import {
SEED_STORE_NAME_PREFIX,
resetSeedScope,
} from '../../prisma/seed/idempotent';
import { seedOrders } from '../../prisma/seed/orders';
import { seedRegions } from '../../prisma/seed/regions';
import { seedReviews } from '../../prisma/seed/reviews';
Expand All @@ -11,6 +14,8 @@ import { disconnectTestPrismaClient } from '@/test/db/prisma-test-client';
import { closeTruncateConnection, truncateAll } from '@/test/db/truncate';
import {
createAccount,
createNotification,
createOrder,
createOrderItem,
createReview,
createReviewReport,
Expand Down Expand Up @@ -51,6 +56,86 @@ describe('resetSeedScope (real DB)', () => {
});
}

/** 시드 밖 계정의 주문 1건(품목 1개)을 만든다. productId를 안 주면 시드 밖 매장 상품으로 */
async function outsiderOrder(productId?: bigint) {
const outsider = await createAccount(prisma, { account_type: 'USER' });
const order = await createOrder(prisma, { account_id: outsider.id });
const item = await createOrderItem(prisma, {
order_id: order.id,
...(productId ? { product_id: productId } : {}),
});
return { outsider, order, item };
}

it('시드 사용자 없이 시드 매장과 시드 밖 주문만 남은 상태(이전 재시드 실패)도 정리한다', async () => {
// 이전 재시드가 상품 삭제에서 실패하면 시드 사용자는 이미 지워져 이 상태가 남는다
await seedRegions(prisma);
const categories = await seedCategories(prisma);
const { products } = await seedStores(prisma, { categories });
const { order } = await outsiderOrder(products[0].id);

await resetSeedScope(prisma);

expect(
await prisma.order.findUnique({ where: { id: order.id } }),
).toBeNull();
expect(
await prisma.store.count({
where: { store_name: { startsWith: SEED_STORE_NAME_PREFIX } },
}),
).toBe(0);
});

it('시드 밖 계정이 시드 상품을 주문하고 리뷰를 남겨도 정리하고 다시 시드한다', async () => {
await seedThroughReviews();
const seedProduct = await prisma.product.findFirstOrThrow({
where: { store: { store_name: { startsWith: SEED_STORE_NAME_PREFIX } } },
});
const { outsider, order, item } = await outsiderOrder(seedProduct.id);
const review = await createReview(prisma, { order_item_id: item.id });
const notification = await createNotification(prisma, {
account_id: outsider.id,
order_id: order.id,
order_item_id: item.id,
review_id: review.id,
});

await resetSeedScope(prisma);
await seedThroughReviews();

expect(
await prisma.order.findUnique({ where: { id: order.id } }),
).toBeNull();
expect(
await prisma.review.findUnique({ where: { id: review.id } }),
).toBeNull();
expect(
await prisma.notification.findUnique({ where: { id: notification.id } }),
).toBeNull();
expect(
await prisma.account.findUnique({ where: { id: outsider.id } }),
).not.toBeNull();
});

it('시드 밖 계정이 시드 밖 매장에 넣은 주문은 남긴다', async () => {
await seedThroughReviews();
const { order, item } = await outsiderOrder();
const notification = await createNotification(prisma, {
account_id: order.account_id,
order_id: order.id,
order_item_id: item.id,
});

await resetSeedScope(prisma);

expect(
await prisma.order.findUnique({ where: { id: order.id } }),
).not.toBeNull();
expect(
await prisma.notification.findUnique({ where: { id: notification.id } }),
).not.toBeNull();
});

it('시드 리뷰에 다른 계정이 단 댓글이 있어도 정리하고 다시 시드한다', async () => {
const users = await seedThroughReviews();
const seedReview = await prisma.review.findFirstOrThrow({
Expand Down
Loading