Files
edgeKey/prisma/migrations/0004_add_discount_codes.sql
2026-05-21 16:14:28 +08:00

37 lines
1.3 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- CreateTable
CREATE TABLE IF NOT EXISTS "DiscountCode" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"code" TEXT NOT NULL,
"type" TEXT NOT NULL CHECK("type" IN ('FIXED', 'PERCENT')),
"value" INTEGER NOT NULL,
"minAmount" INTEGER,
"maxUses" INTEGER,
"usedCount" INTEGER NOT NULL DEFAULT 0,
"productIds" TEXT,
"expiresAt" DATETIME,
"isActive" BOOLEAN NOT NULL DEFAULT true,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);
-- CreateIndex
CREATE UNIQUE INDEX IF NOT EXISTS "DiscountCode_code_key" ON "DiscountCode"("code");
-- CreateIndex
CREATE INDEX IF NOT EXISTS "DiscountCode_code_idx" ON "DiscountCode"("code");
-- CreateIndex
CREATE INDEX IF NOT EXISTS "DiscountCode_isActive_idx" ON "DiscountCode"("isActive");
-- AlterTable: Add discount fields to Order
ALTER TABLE "Order" ADD COLUMN "discountCodeId" INTEGER;
ALTER TABLE "Order" ADD COLUMN "discountCodeStr" TEXT;
ALTER TABLE "Order" ADD COLUMN "originalAmount" INTEGER;
ALTER TABLE "Order" ADD COLUMN "discountAmount" INTEGER;
-- CreateIndex
CREATE INDEX IF NOT EXISTS "Order_discountCodeId_idx" ON "Order"("discountCodeId");
-- AddForeignKey (SQLite 不支持 ALTER TABLE ADD CONSTRAINT需要重建表)
-- 由于 D1/SQLite 限制,外键关系通过 Prisma schema 定义,应用层维护一致性