chatbot-template/lib/db/migrations/0001_even_baron_strucker.sql
dmitry.galkin 22a172e92d Add Umami analytics, subscription tiers, tool-call metering
- Analytics: <Analytics /> in root layout loads umami when NEXT_PUBLIC_UMAMI_* env are set
- Subscription: free/pro tiers in Postgres, daily quotas (10msg/5tool free, 1000/500 pro)
- Chat route: checkAndConsume(chat_message) gate before streamText, 429 when exhausted
- Tool metering: getWeather wrapped with withLimit decorator that deducts tool_call quota
- Pricing page at /pricing with upgrade button
- /api/billing/checkout posts to EGBE payment gateway, /api/billing/webhook verifies HMAC and upgrades user on checkout.session.completed
- UsageBadge in chat header polls /api/usage every 15s
- Dropped now-unused entitlements.ts
2026-05-25 17:27:08 +04:00

37 lines
1.3 KiB
SQL

CREATE TABLE IF NOT EXISTS "Subscription" (
"userId" uuid PRIMARY KEY NOT NULL,
"tier" varchar DEFAULT 'free' NOT NULL,
"status" varchar DEFAULT 'active' NOT NULL,
"stripeCustomerId" text,
"stripeSubscriptionId" text,
"currentPeriodEnd" timestamp,
"createdAt" timestamp DEFAULT now() NOT NULL,
"updatedAt" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "Usage" (
"userId" uuid NOT NULL,
"periodKey" varchar(10) NOT NULL,
"messageCount" integer DEFAULT 0 NOT NULL,
"toolCallCount" integer DEFAULT 0 NOT NULL,
"updatedAt" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "Usage_userId_periodKey_pk" PRIMARY KEY("userId","periodKey")
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "WebhookEvent" (
"id" text PRIMARY KEY NOT NULL,
"type" text NOT NULL,
"receivedAt" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "Subscription" ADD CONSTRAINT "Subscription_userId_User_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."User"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "Usage" ADD CONSTRAINT "Usage_userId_User_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."User"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;