Files
wiki-server/drizzle/0000_nosy_forgotten_one.sql
dahyeon dff10ff604 Establishes Drizzle ORM schema; enhances auth
Establishes the initial database schema using Drizzle ORM, replacing the prior placeholder. This migration introduces core entities like users, spaces, pages, access tokens, and their permissions, complete with relationships and constraints.

Updates the authentication flow by securing the sign-in endpoint with JWT validation and extracting user details directly from the token via a new custom parameter decorator, improving user context handling.

Integrates a new `Page` module, laying the groundwork for future page management capabilities.
2025-11-10 14:50:29 +00:00

71 lines
4.1 KiB
SQL

-- Current sql file was generated after introspecting the database
-- If you want to run this migration please uncomment this code before executing migrations
/*
CREATE TABLE "users" (
"user_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"entra_object_id" varchar(36) NOT NULL,
"entra_tenant_id" varchar(36) NOT NULL,
"email" varchar(255) NOT NULL,
"display_name" varchar(255) NOT NULL,
"is_system_admin" boolean DEFAULT false,
"is_active" boolean DEFAULT true,
"created_at" timestamp with time zone DEFAULT now(),
"updated_at" timestamp with time zone DEFAULT now(),
"last_login_at" timestamp with time zone,
CONSTRAINT "users_entra_object_id_key" UNIQUE("entra_object_id")
);
--> statement-breakpoint
CREATE TABLE "spaces" (
"space_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"space_key" varchar(50) NOT NULL,
"name" varchar(255) NOT NULL,
"description" text,
"created_by_user_id" uuid NOT NULL,
"created_at" timestamp with time zone DEFAULT now(),
CONSTRAINT "spaces_space_key_key" UNIQUE("space_key")
);
--> statement-breakpoint
CREATE TABLE "pages" (
"page_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"space_id" uuid NOT NULL,
"parent_page_id" uuid,
"sort_order" integer DEFAULT 0,
"title" varchar(255) NOT NULL,
"content" text,
"created_by_user_id" uuid NOT NULL,
"created_at" timestamp with time zone DEFAULT now(),
"updated_by_user_id" uuid NOT NULL,
"updated_at" timestamp with time zone DEFAULT now(),
"is_deleted" boolean DEFAULT false
);
--> statement-breakpoint
CREATE TABLE "accesstokens" (
"token_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"token_value" text NOT NULL,
"description" varchar(255),
"expires_at" timestamp with time zone,
"created_by_user_id" uuid NOT NULL,
"created_at" timestamp with time zone DEFAULT now(),
CONSTRAINT "accesstokens_token_value_key" UNIQUE("token_value")
);
--> statement-breakpoint
CREATE TABLE "tokenpermissions" (
"permission_id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
"token_id" uuid NOT NULL,
"space_id" uuid,
"page_id" uuid,
CONSTRAINT "tokenpermissions_token_id_space_id_key" UNIQUE("token_id","space_id"),
CONSTRAINT "tokenpermissions_token_id_page_id_key" UNIQUE("token_id","page_id"),
CONSTRAINT "tokenpermissions_check" CHECK (((space_id IS NOT NULL) AND (page_id IS NULL)) OR ((space_id IS NULL) AND (page_id IS NOT NULL)))
);
--> statement-breakpoint
ALTER TABLE "spaces" ADD CONSTRAINT "spaces_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."users"("user_id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "pages" ADD CONSTRAINT "pages_space_id_fkey" FOREIGN KEY ("space_id") REFERENCES "public"."spaces"("space_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "pages" ADD CONSTRAINT "pages_parent_page_id_fkey" FOREIGN KEY ("parent_page_id") REFERENCES "public"."pages"("page_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "pages" ADD CONSTRAINT "pages_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."users"("user_id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "pages" ADD CONSTRAINT "pages_updated_by_user_id_fkey" FOREIGN KEY ("updated_by_user_id") REFERENCES "public"."users"("user_id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "accesstokens" ADD CONSTRAINT "accesstokens_created_by_user_id_fkey" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."users"("user_id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tokenpermissions" ADD CONSTRAINT "tokenpermissions_token_id_fkey" FOREIGN KEY ("token_id") REFERENCES "public"."accesstokens"("token_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tokenpermissions" ADD CONSTRAINT "tokenpermissions_space_id_fkey" FOREIGN KEY ("space_id") REFERENCES "public"."spaces"("space_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tokenpermissions" ADD CONSTRAINT "tokenpermissions_page_id_fkey" FOREIGN KEY ("page_id") REFERENCES "public"."pages"("page_id") ON DELETE cascade ON UPDATE no action;
*/