ai-chatbot/lib/db/migrations/0096_add_user_access_keys.sql

24 lines
933 B
SQL

CREATE TABLE IF NOT EXISTS "UserAccessKey" (
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"userId" uuid NOT NULL,
"name" varchar(100) NOT NULL,
"tokenPrefix" varchar(32) NOT NULL,
"tokenHash" varchar(64) NOT NULL,
"scopes" jsonb DEFAULT '[]'::jsonb NOT NULL,
"lastUsedAt" timestamp,
"expiresAt" timestamp,
"revokedAt" timestamp,
"createdAt" timestamp DEFAULT now() NOT NULL,
"updatedAt" timestamp DEFAULT now() NOT NULL,
CONSTRAINT "UserAccessKey_tokenHash_unique" UNIQUE("tokenHash"),
CONSTRAINT "UserAccessKey_userId_User_id_fk"
FOREIGN KEY ("userId") REFERENCES "public"."User"("id")
ON DELETE cascade ON UPDATE no action
);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "UserAccessKey_userId_idx"
ON "UserAccessKey" USING btree ("userId");
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "UserAccessKey_tokenPrefix_idx"
ON "UserAccessKey" USING btree ("tokenPrefix");