CREATE TABLE "ai_comparisons" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "prompt" text NOT NULL, "models" jsonb NOT NULL, "results" jsonb DEFAULT '[]'::jsonb NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "ai_conversations" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "title" text DEFAULT 'New conversation' NOT NULL, "page_id" uuid, "model" text, "routing" text DEFAULT 'auto', "pinned" boolean DEFAULT false NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, "deleted_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "ai_executions" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "agent" text NOT NULL, "task" text NOT NULL, "model" text, "status" text DEFAULT 'pending' NOT NULL, "scopes" jsonb DEFAULT '[]'::jsonb NOT NULL, "tools_invoked" jsonb DEFAULT '[]'::jsonb, "documents_accessed" jsonb DEFAULT '[]'::jsonb, "proposed_changes" jsonb DEFAULT '[]'::jsonb, "approvals" jsonb DEFAULT '[]'::jsonb, "outcome" text, "result" text, "error" text, "cost_usd" double precision DEFAULT 0, "tokens_in" integer DEFAULT 0, "tokens_out" integer DEFAULT 0, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "started_at" timestamp with time zone, "finished_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "ai_messages" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "conversation_id" uuid NOT NULL, "role" text NOT NULL, "content" text NOT NULL, "model" text, "provider" text, "sources" jsonb DEFAULT '[]'::jsonb, "usage" jsonb DEFAULT '{}'::jsonb, "cost_usd" double precision, "latency_ms" integer, "ttft_ms" integer, "meta" jsonb DEFAULT '{}'::jsonb, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "ai_models" ( "id" text PRIMARY KEY NOT NULL, "provider" text NOT NULL, "display_name" text, "category" text DEFAULT 'text' NOT NULL, "available" boolean DEFAULT true NOT NULL, "enabled" boolean DEFAULT true NOT NULL, "context_window" integer, "max_output" integer, "input_price" double precision, "cached_price" double precision, "output_price" double precision, "pricing_source" text, "probes" jsonb DEFAULT '{}'::jsonb, "benchmark" jsonb DEFAULT '{}'::jsonb, "capabilities" jsonb DEFAULT '{}'::jsonb, "notes" jsonb DEFAULT '[]'::jsonb, "discovered_at" timestamp with time zone, "probed_at" timestamp with time zone, "benchmarked_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "ai_providers" ( "id" text PRIMARY KEY NOT NULL, "name" text NOT NULL, "enabled" boolean DEFAULT true NOT NULL, "last_discovery_at" timestamp with time zone, "status" text DEFAULT 'unknown', "status_detail" text ); --> statement-breakpoint CREATE TABLE "ai_usage" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid, "provider" text NOT NULL, "model" text NOT NULL, "task" text NOT NULL, "input_tokens" integer DEFAULT 0 NOT NULL, "output_tokens" integer DEFAULT 0 NOT NULL, "cached_tokens" integer DEFAULT 0, "reasoning_tokens" integer DEFAULT 0, "cost_usd" double precision, "latency_ms" integer, "ttft_ms" integer, "ok" boolean DEFAULT true NOT NULL, "error" text, "ref_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "attachments" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "page_id" uuid, "filename" text NOT NULL, "mime" text NOT NULL, "size" bigint NOT NULL, "sha256" text NOT NULL, "storage_path" text NOT NULL, "extracted_text" text, "page_count" integer, "meta" jsonb DEFAULT '{}'::jsonb, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "deleted_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "audit_logs" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid, "action" text NOT NULL, "target_type" text, "target_id" text, "detail" jsonb DEFAULT '{}'::jsonb, "ip" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "backup_jobs" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "kind" text DEFAULT 'full' NOT NULL, "status" text DEFAULT 'running' NOT NULL, "path" text, "size_bytes" bigint, "sha256" text, "offsite" boolean DEFAULT false, "error" text, "started_at" timestamp with time zone DEFAULT now() NOT NULL, "finished_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "chunks" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "source_type" text NOT NULL, "source_id" uuid NOT NULL, "page_id" uuid, "ordinal" integer NOT NULL, "block_id" text, "heading" text, "content" text NOT NULL, "token_count" integer DEFAULT 0 NOT NULL, "content_hash" text NOT NULL, "tsv" "tsvector" GENERATED ALWAYS AS (to_tsvector('simple', coalesce(heading,'') || ' ' || content)) STORED, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "dashboard_layouts" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid NOT NULL, "name" text DEFAULT 'Home' NOT NULL, "widgets" jsonb DEFAULT '[]'::jsonb NOT NULL, "is_default" boolean DEFAULT true NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "database_properties" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "database_id" uuid NOT NULL, "name" text NOT NULL, "type" text NOT NULL, "config" jsonb DEFAULT '{}'::jsonb NOT NULL, "position" integer DEFAULT 0 NOT NULL, "width" integer DEFAULT 180, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "database_rows" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "database_id" uuid NOT NULL, "page_id" uuid NOT NULL, "values" jsonb DEFAULT '{}'::jsonb NOT NULL, "position" double precision DEFAULT 0 NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, "deleted_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "database_views" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "database_id" uuid NOT NULL, "name" text NOT NULL, "type" text DEFAULT 'table' NOT NULL, "config" jsonb DEFAULT '{}'::jsonb NOT NULL, "position" integer DEFAULT 0 NOT NULL, "is_default" boolean DEFAULT false NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "databases" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "page_id" uuid NOT NULL, "name" text NOT NULL, "description" text, "icon" text, "is_template" boolean DEFAULT false NOT NULL, "template_of" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, "deleted_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "embeddings" ( "chunk_id" uuid PRIMARY KEY NOT NULL, "model" text NOT NULL, "version" integer DEFAULT 1 NOT NULL, "embedding" vector(1536) NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "experiments" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "page_id" uuid, "name" text NOT NULL, "status" text DEFAULT 'planned' NOT NULL, "params" jsonb DEFAULT '{}'::jsonb, "metrics" jsonb DEFAULT '{}'::jsonb, "dataset_ref" text, "repo_url" text, "notes" text, "started_at" timestamp with time zone, "finished_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "index_state" ( "source_type" text NOT NULL, "source_id" uuid NOT NULL, "content_hash" text NOT NULL, "model" text NOT NULL, "version" integer DEFAULT 1 NOT NULL, "indexed_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "index_state_source_type_source_id_pk" PRIMARY KEY("source_type","source_id") ); --> statement-breakpoint CREATE TABLE "knowledge_edges" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "from_id" uuid NOT NULL, "to_id" uuid NOT NULL, "from_type" text DEFAULT 'page' NOT NULL, "to_type" text DEFAULT 'page' NOT NULL, "kind" text NOT NULL, "status" text DEFAULT 'accepted' NOT NULL, "confidence" double precision, "rationale" text, "model" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "decided_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "links" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "from_page_id" uuid NOT NULL, "to_page_id" uuid NOT NULL, "kind" text DEFAULT 'link' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "login_attempts" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "ip" text NOT NULL, "username" text, "success" boolean NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "page_tags" ( "page_id" uuid NOT NULL, "tag_id" uuid NOT NULL, CONSTRAINT "page_tags_page_id_tag_id_pk" PRIMARY KEY("page_id","tag_id") ); --> statement-breakpoint CREATE TABLE "page_versions" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "page_id" uuid NOT NULL, "version" integer NOT NULL, "title" text NOT NULL, "content" jsonb NOT NULL, "properties" jsonb DEFAULT '{}'::jsonb, "reason" text DEFAULT 'autosave' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "word_count" integer DEFAULT 0 ); --> statement-breakpoint CREATE TABLE "pages" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "parent_id" uuid, "database_id" uuid, "type" text DEFAULT 'page' NOT NULL, "section" text DEFAULT 'notes' NOT NULL, "title" text DEFAULT 'Untitled' NOT NULL, "icon" text, "cover" text, "content" jsonb DEFAULT '{"type":"doc","content":[]}'::jsonb NOT NULL, "plain_text" text DEFAULT '' NOT NULL, "tsv" "tsvector" GENERATED ALWAYS AS (setweight(to_tsvector('simple', coalesce(title,'')), 'A') || setweight(to_tsvector('simple', coalesce(plain_text,'')), 'B')) STORED, "properties" jsonb DEFAULT '{}'::jsonb NOT NULL, "position" double precision DEFAULT 0 NOT NULL, "pinned" boolean DEFAULT false NOT NULL, "archived" boolean DEFAULT false NOT NULL, "version" integer DEFAULT 1 NOT NULL, "word_count" integer DEFAULT 0 NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, "last_opened_at" timestamp with time zone, "deleted_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "relations" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "property_id" uuid NOT NULL, "from_row_id" uuid NOT NULL, "to_row_id" uuid NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "references" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "page_id" uuid, "cite_key" text NOT NULL, "entry_type" text DEFAULT 'article' NOT NULL, "title" text NOT NULL, "authors" jsonb DEFAULT '[]'::jsonb, "year" integer, "journal" text, "doi" text, "url" text, "bibtex" text, "fields" jsonb DEFAULT '{}'::jsonb, "verified" text DEFAULT 'user' NOT NULL, "attachment_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, "deleted_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "sessions" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid NOT NULL, "token_hash" text NOT NULL, "user_agent" text, "ip" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "last_seen_at" timestamp with time zone DEFAULT now() NOT NULL, "expires_at" timestamp with time zone NOT NULL, "absolute_expires_at" timestamp with time zone NOT NULL, "revoked_at" timestamp with time zone, "mfa_passed" boolean DEFAULT false NOT NULL, CONSTRAINT "sessions_token_hash_unique" UNIQUE("token_hash") ); --> statement-breakpoint CREATE TABLE "settings" ( "key" text PRIMARY KEY NOT NULL, "value" jsonb NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "tags" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "name" text NOT NULL, "color" text DEFAULT 'gray', "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "tasks" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "page_id" uuid, "title" text NOT NULL, "status" text DEFAULT 'todo' NOT NULL, "priority" text DEFAULT 'medium', "due_at" timestamp with time zone, "completed_at" timestamp with time zone, "project" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, "deleted_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "users" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "username" text NOT NULL, "display_name" text NOT NULL, "email" text, "password_hash" text NOT NULL, "totp_secret" text, "totp_enabled" boolean DEFAULT false NOT NULL, "preferences" jsonb DEFAULT '{}'::jsonb NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "users_username_unique" UNIQUE("username") ); --> statement-breakpoint CREATE TABLE "webauthn_challenges" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid, "challenge" text NOT NULL, "kind" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "webauthn_credentials" ( "id" text PRIMARY KEY NOT NULL, "user_id" uuid NOT NULL, "public_key" text NOT NULL, "counter" bigint DEFAULT 0 NOT NULL, "transports" jsonb DEFAULT '[]'::jsonb, "device_type" text, "backed_up" boolean DEFAULT false, "label" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "last_used_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "workspaces" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "owner_id" uuid NOT NULL, "name" text NOT NULL, "slug" text NOT NULL, "settings" jsonb DEFAULT '{}'::jsonb NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "workspaces_slug_unique" UNIQUE("slug") ); --> statement-breakpoint ALTER TABLE "ai_comparisons" ADD CONSTRAINT "ai_comparisons_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "ai_conversations" ADD CONSTRAINT "ai_conversations_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "ai_conversations" ADD CONSTRAINT "ai_conversations_page_id_pages_id_fk" FOREIGN KEY ("page_id") REFERENCES "public"."pages"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "ai_executions" ADD CONSTRAINT "ai_executions_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "ai_messages" ADD CONSTRAINT "ai_messages_conversation_id_ai_conversations_id_fk" FOREIGN KEY ("conversation_id") REFERENCES "public"."ai_conversations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "ai_models" ADD CONSTRAINT "ai_models_provider_ai_providers_id_fk" FOREIGN KEY ("provider") REFERENCES "public"."ai_providers"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "attachments" ADD CONSTRAINT "attachments_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "attachments" ADD CONSTRAINT "attachments_page_id_pages_id_fk" FOREIGN KEY ("page_id") REFERENCES "public"."pages"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "chunks" ADD CONSTRAINT "chunks_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "dashboard_layouts" ADD CONSTRAINT "dashboard_layouts_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "database_properties" ADD CONSTRAINT "database_properties_database_id_databases_id_fk" FOREIGN KEY ("database_id") REFERENCES "public"."databases"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "database_rows" ADD CONSTRAINT "database_rows_database_id_databases_id_fk" FOREIGN KEY ("database_id") REFERENCES "public"."databases"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "database_rows" ADD CONSTRAINT "database_rows_page_id_pages_id_fk" FOREIGN KEY ("page_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "database_views" ADD CONSTRAINT "database_views_database_id_databases_id_fk" FOREIGN KEY ("database_id") REFERENCES "public"."databases"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "databases" ADD CONSTRAINT "databases_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "databases" ADD CONSTRAINT "databases_page_id_pages_id_fk" FOREIGN KEY ("page_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "embeddings" ADD CONSTRAINT "embeddings_chunk_id_chunks_id_fk" FOREIGN KEY ("chunk_id") REFERENCES "public"."chunks"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "experiments" ADD CONSTRAINT "experiments_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "experiments" ADD CONSTRAINT "experiments_page_id_pages_id_fk" FOREIGN KEY ("page_id") REFERENCES "public"."pages"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "knowledge_edges" ADD CONSTRAINT "knowledge_edges_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "links" ADD CONSTRAINT "links_from_page_id_pages_id_fk" FOREIGN KEY ("from_page_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "links" ADD CONSTRAINT "links_to_page_id_pages_id_fk" FOREIGN KEY ("to_page_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "page_tags" ADD CONSTRAINT "page_tags_page_id_pages_id_fk" FOREIGN KEY ("page_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "page_tags" ADD CONSTRAINT "page_tags_tag_id_tags_id_fk" FOREIGN KEY ("tag_id") REFERENCES "public"."tags"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "page_versions" ADD CONSTRAINT "page_versions_page_id_pages_id_fk" FOREIGN KEY ("page_id") REFERENCES "public"."pages"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "pages" ADD CONSTRAINT "pages_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "relations" ADD CONSTRAINT "relations_property_id_database_properties_id_fk" FOREIGN KEY ("property_id") REFERENCES "public"."database_properties"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "relations" ADD CONSTRAINT "relations_from_row_id_database_rows_id_fk" FOREIGN KEY ("from_row_id") REFERENCES "public"."database_rows"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "relations" ADD CONSTRAINT "relations_to_row_id_database_rows_id_fk" FOREIGN KEY ("to_row_id") REFERENCES "public"."database_rows"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "references" ADD CONSTRAINT "references_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "references" ADD CONSTRAINT "references_page_id_pages_id_fk" FOREIGN KEY ("page_id") REFERENCES "public"."pages"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "references" ADD CONSTRAINT "references_attachment_id_attachments_id_fk" FOREIGN KEY ("attachment_id") REFERENCES "public"."attachments"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "tags" ADD CONSTRAINT "tags_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "tasks" ADD CONSTRAINT "tasks_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "tasks" ADD CONSTRAINT "tasks_page_id_pages_id_fk" FOREIGN KEY ("page_id") REFERENCES "public"."pages"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "webauthn_credentials" ADD CONSTRAINT "webauthn_credentials_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "workspaces" ADD CONSTRAINT "workspaces_owner_id_users_id_fk" FOREIGN KEY ("owner_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint CREATE INDEX "ai_conv_ws_idx" ON "ai_conversations" USING btree ("workspace_id","updated_at");--> statement-breakpoint CREATE INDEX "ai_exec_ws_idx" ON "ai_executions" USING btree ("workspace_id","created_at");--> statement-breakpoint CREATE INDEX "ai_msgs_conv_idx" ON "ai_messages" USING btree ("conversation_id","created_at");--> statement-breakpoint CREATE INDEX "ai_usage_created_idx" ON "ai_usage" USING btree ("created_at");--> statement-breakpoint CREATE INDEX "attachments_page_idx" ON "attachments" USING btree ("page_id");--> statement-breakpoint CREATE INDEX "attachments_sha_idx" ON "attachments" USING btree ("sha256");--> statement-breakpoint CREATE INDEX "audit_created_idx" ON "audit_logs" USING btree ("created_at");--> statement-breakpoint CREATE INDEX "chunks_source_idx" ON "chunks" USING btree ("source_type","source_id");--> statement-breakpoint CREATE INDEX "chunks_tsv_idx" ON "chunks" USING gin ("tsv");--> statement-breakpoint CREATE UNIQUE INDEX "chunks_unique" ON "chunks" USING btree ("source_type","source_id","ordinal");--> statement-breakpoint CREATE INDEX "db_props_db_idx" ON "database_properties" USING btree ("database_id");--> statement-breakpoint CREATE INDEX "db_rows_db_idx" ON "database_rows" USING btree ("database_id");--> statement-breakpoint CREATE INDEX "db_rows_values_idx" ON "database_rows" USING gin ("values");--> statement-breakpoint CREATE INDEX "embeddings_hnsw_idx" ON "embeddings" USING hnsw ("embedding" vector_cosine_ops);--> statement-breakpoint CREATE UNIQUE INDEX "kedges_unique" ON "knowledge_edges" USING btree ("from_id","to_id","kind");--> statement-breakpoint CREATE INDEX "kedges_ws_idx" ON "knowledge_edges" USING btree ("workspace_id","status");--> statement-breakpoint CREATE UNIQUE INDEX "links_unique" ON "links" USING btree ("from_page_id","to_page_id","kind");--> statement-breakpoint CREATE INDEX "links_to_idx" ON "links" USING btree ("to_page_id");--> statement-breakpoint CREATE INDEX "login_attempts_ip_idx" ON "login_attempts" USING btree ("ip","created_at");--> statement-breakpoint CREATE UNIQUE INDEX "page_versions_unique" ON "page_versions" USING btree ("page_id","version");--> statement-breakpoint CREATE INDEX "pages_ws_parent_idx" ON "pages" USING btree ("workspace_id","parent_id");--> statement-breakpoint CREATE INDEX "pages_tsv_idx" ON "pages" USING gin ("tsv");--> statement-breakpoint CREATE INDEX "pages_updated_idx" ON "pages" USING btree ("updated_at");--> statement-breakpoint CREATE INDEX "pages_db_idx" ON "pages" USING btree ("database_id");--> statement-breakpoint CREATE INDEX "pages_title_trgm_idx" ON "pages" USING gin ("title" gin_trgm_ops);--> statement-breakpoint CREATE UNIQUE INDEX "relations_unique" ON "relations" USING btree ("property_id","from_row_id","to_row_id");--> statement-breakpoint CREATE INDEX "relations_to_idx" ON "relations" USING btree ("to_row_id");--> statement-breakpoint CREATE UNIQUE INDEX "references_key_unique" ON "references" USING btree ("workspace_id","cite_key");--> statement-breakpoint CREATE INDEX "sessions_user_idx" ON "sessions" USING btree ("user_id");--> statement-breakpoint CREATE UNIQUE INDEX "tags_unique" ON "tags" USING btree ("workspace_id","name");--> statement-breakpoint CREATE INDEX "tasks_due_idx" ON "tasks" USING btree ("due_at");