An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: Add Row-Level Security (RLS) to all tables
Yoshiki Miurabc1260133e50
diff --git a/drizzle/0005_awesome_riptide.sql b/drizzle/0005_awesome_riptide.sqlnew file mode 100644index 00000000..cb78cc8c--- /dev/null+++ b/drizzle/0005_awesome_riptide.sql@@ -0,0 +1,5 @@+ALTER TABLE "chats" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint+ALTER TABLE "feedback" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint+ALTER TABLE "messages" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint+ALTER TABLE "parts" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint+CREATE POLICY "anyone_can_insert_feedback" ON "feedback" AS PERMISSIVE FOR INSERT TO public WITH CHECK (true);\ No newline at end of filediff --git a/drizzle/meta/0005_snapshot.json b/drizzle/meta/0005_snapshot.jsonnew file mode 100644index 00000000..8ceede4c--- /dev/null+++ b/drizzle/meta/0005_snapshot.json@@ -0,0 +1,640 @@+{+ "id": "c2240142-0d69-482e-b6e0-a9561c421aa2",+ "prevId": "28e1690d-994a-4027-abe2-5f283bb4c720",+ "version": "7",+ "dialect": "postgresql",+ "tables": {+ "public.chats": {+ "name": "chats",+ "schema": "",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(191)",+ "primaryKey": true,+ "notNull": true+ },+ "created_at": {+ "name": "created_at",+ "type": "timestamp",+ "primaryKey": false,+ "notNull": true,+ "default": "now()"+ },+ "title": {+ "name": "title",+ "type": "text",+ "primaryKey": false,+ "notNull": true+ },+ "user_id": {+ "name": "user_id",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true+ },+ "visibility": {+ "name": "visibility",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": true,+ "default": "'private'"+ }+ },+ "indexes": {+ "chats_user_id_idx": {+ "name": "chats_user_id_idx",+ "columns": [+ {+ "expression": "user_id",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ },+ "chats_user_id_created_at_idx": {+ "name": "chats_user_id_created_at_idx",+ "columns": [+ {+ "expression": "user_id",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ },+ {+ "expression": "created_at",+ "isExpression": false,+ "asc": false,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ },+ "chats_created_at_idx": {+ "name": "chats_created_at_idx",+ "columns": [+ {+ "expression": "created_at",+ "isExpression": false,+ "asc": false,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "policies": {},+ "checkConstraints": {},+ "isRLSEnabled": true+ },+ "public.feedback": {+ "name": "feedback",+ "schema": "",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(191)",+ "primaryKey": true,+ "notNull": true+ },+ "user_id": {+ "name": "user_id",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false+ },+ "sentiment": {+ "name": "sentiment",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": true+ },+ "message": {+ "name": "message",+ "type": "text",+ "primaryKey": false,+ "notNull": true+ },+ "page_url": {+ "name": "page_url",+ "type": "text",+ "primaryKey": false,+ "notNull": true+ },+ "user_agent": {+ "name": "user_agent",+ "type": "text",+ "primaryKey": false,+ "notNull": false+ },+ "created_at": {+ "name": "created_at",+ "type": "timestamp",+ "primaryKey": false,+ "notNull": true,+ "default": "now()"+ }+ },+ "indexes": {+ "feedback_user_id_idx": {+ "name": "feedback_user_id_idx",+ "columns": [+ {+ "expression": "user_id",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ },+ "feedback_created_at_idx": {+ "name": "feedback_created_at_idx",+ "columns": [+ {+ "expression": "created_at",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "policies": {+ "anyone_can_insert_feedback": {+ "name": "anyone_can_insert_feedback",+ "as": "PERMISSIVE",+ "for": "INSERT",+ "to": [+ "public"+ ],+ "withCheck": "true"+ }+ },+ "checkConstraints": {},+ "isRLSEnabled": false+ },+ "public.messages": {+ "name": "messages",+ "schema": "",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(191)",+ "primaryKey": true,+ "notNull": true+ },+ "chat_id": {+ "name": "chat_id",+ "type": "varchar(191)",+ "primaryKey": false,+ "notNull": true+ },+ "role": {+ "name": "role",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": true+ },+ "created_at": {+ "name": "created_at",+ "type": "timestamp",+ "primaryKey": false,+ "notNull": true,+ "default": "now()"+ },+ "updated_at": {+ "name": "updated_at",+ "type": "timestamp",+ "primaryKey": false,+ "notNull": false+ },+ "metadata": {+ "name": "metadata",+ "type": "jsonb",+ "primaryKey": false,+ "notNull": false+ }+ },+ "indexes": {+ "messages_chat_id_idx": {+ "name": "messages_chat_id_idx",+ "columns": [+ {+ "expression": "chat_id",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ },+ "messages_chat_id_created_at_idx": {+ "name": "messages_chat_id_created_at_idx",+ "columns": [+ {+ "expression": "chat_id",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ },+ {+ "expression": "created_at",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ }+ },+ "foreignKeys": {+ "messages_chat_id_chats_id_fk": {+ "name": "messages_chat_id_chats_id_fk",+ "tableFrom": "messages",+ "tableTo": "chats",+ "columnsFrom": [+ "chat_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "no action"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "policies": {},+ "checkConstraints": {},+ "isRLSEnabled": true+ },+ "public.parts": {+ "name": "parts",+ "schema": "",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(191)",+ "primaryKey": true,+ "notNull": true+ },+ "message_id": {+ "name": "message_id",+ "type": "varchar(191)",+ "primaryKey": false,+ "notNull": true+ },+ "order": {+ "name": "order",+ "type": "integer",+ "primaryKey": false,+ "notNull": true+ },+ "type": {+ "name": "type",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": true+ },+ "text_text": {+ "name": "text_text",+ "type": "text",+ "primaryKey": false,+ "notNull": false+ },+ "reasoning_text": {+ "name": "reasoning_text",+ "type": "text",+ "primaryKey": false,+ "notNull": false+ },+ "file_media_type": {+ "name": "file_media_type",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": false+ },+ "file_filename": {+ "name": "file_filename",+ "type": "varchar(1024)",+ "primaryKey": false,+ "notNull": false+ },+ "file_url": {+ "name": "file_url",+ "type": "text",+ "primaryKey": false,+ "notNull": false+ },+ "source_url_source_id": {+ "name": "source_url_source_id",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": false+ },+ "source_url_url": {+ "name": "source_url_url",+ "type": "text",+ "primaryKey": false,+ "notNull": false+ },+ "source_url_title": {+ "name": "source_url_title",+ "type": "text",+ "primaryKey": false,+ "notNull": false+ },+ "source_document_source_id": {+ "name": "source_document_source_id",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": false+ },+ "source_document_media_type": {+ "name": "source_document_media_type",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": false+ },+ "source_document_title": {+ "name": "source_document_title",+ "type": "text",+ "primaryKey": false,+ "notNull": false+ },+ "source_document_filename": {+ "name": "source_document_filename",+ "type": "varchar(1024)",+ "primaryKey": false,+ "notNull": false+ },+ "source_document_url": {+ "name": "source_document_url",+ "type": "text",+ "primaryKey": false,+ "notNull": false+ },+ "source_document_snippet": {+ "name": "source_document_snippet",+ "type": "text",+ "primaryKey": false,+ "notNull": false+ },+ "tool_tool_call_id": {+ "name": "tool_tool_call_id",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": false+ },+ "tool_state": {+ "name": "tool_state",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": false+ },+ "tool_error_text": {+ "name": "tool_error_text",+ "type": "text",+ "primaryKey": false,+ "notNull": false+ },+ "tool_search_input": {+ "name": "tool_search_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_search_output": {+ "name": "tool_search_output",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_fetch_input": {+ "name": "tool_fetch_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_fetch_output": {+ "name": "tool_fetch_output",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_question_input": {+ "name": "tool_question_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_question_output": {+ "name": "tool_question_output",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_todoWrite_input": {+ "name": "tool_todoWrite_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_todoWrite_output": {+ "name": "tool_todoWrite_output",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_todoRead_input": {+ "name": "tool_todoRead_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_todoRead_output": {+ "name": "tool_todoRead_output",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_dynamic_input": {+ "name": "tool_dynamic_input",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_dynamic_output": {+ "name": "tool_dynamic_output",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "tool_dynamic_name": {+ "name": "tool_dynamic_name",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": false+ },+ "tool_dynamic_type": {+ "name": "tool_dynamic_type",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": false+ },+ "data_prefix": {+ "name": "data_prefix",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": false+ },+ "data_content": {+ "name": "data_content",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "data_id": {+ "name": "data_id",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": false+ },+ "provider_metadata": {+ "name": "provider_metadata",+ "type": "json",+ "primaryKey": false,+ "notNull": false+ },+ "created_at": {+ "name": "created_at",+ "type": "timestamp",+ "primaryKey": false,+ "notNull": true,+ "default": "now()"+ }+ },+ "indexes": {+ "parts_message_id_idx": {+ "name": "parts_message_id_idx",+ "columns": [+ {+ "expression": "message_id",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ },+ "parts_message_id_order_idx": {+ "name": "parts_message_id_order_idx",+ "columns": [+ {+ "expression": "message_id",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ },+ {+ "expression": "order",+ "isExpression": false,+ "asc": true,+ "nulls": "last"+ }+ ],+ "isUnique": false,+ "concurrently": false,+ "method": "btree",+ "with": {}+ }+ },+ "foreignKeys": {+ "parts_message_id_messages_id_fk": {+ "name": "parts_message_id_messages_id_fk",+ "tableFrom": "parts",+ "tableTo": "messages",+ "columnsFrom": [+ "message_id"+ ],+ "columnsTo": [+ "id"+ ],+ "onDelete": "cascade",+ "onUpdate": "no action"+ }+ },+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "policies": {},+ "checkConstraints": {+ "text_text_required": {+ "name": "text_text_required",+ "value": "(type != 'text' OR text_text IS NOT NULL)"+ },+ "reasoning_text_required": {+ "name": "reasoning_text_required",+ "value": "(type != 'reasoning' OR reasoning_text IS NOT NULL)"+ },+ "file_fields_required": {+ "name": "file_fields_required",+ "value": "(type != 'file' OR (file_media_type IS NOT NULL AND file_filename IS NOT NULL AND file_url IS NOT NULL))"+ },+ "tool_state_valid": {+ "name": "tool_state_valid",+ "value": "(tool_state IS NULL OR tool_state IN ('input-streaming', 'input-available', 'output-available', 'output-error'))"+ },+ "tool_fields_required": {+ "name": "tool_fields_required",+ "value": "(type NOT LIKE 'tool-%' OR (tool_tool_call_id IS NOT NULL AND tool_state IS NOT NULL))"+ }+ },+ "isRLSEnabled": true+ }+ },+ "enums": {},+ "schemas": {},+ "sequences": {},+ "roles": {},+ "policies": {},+ "views": {},+ "_meta": {+ "columns": {},+ "schemas": {},+ "tables": {}+ }+}\ No newline at end of filediff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.jsonindex 46cbd832..9c569bb1 100644--- a/drizzle/meta/_journal.json+++ b/drizzle/meta/_journal.json@@ -36,6 +36,13 @@"when": 1754894590844,"tag": "0004_natural_wallow","breakpoints": true+ },+ {+ "idx": 5,+ "version": "7",+ "when": 1755654069349,+ "tag": "0005_awesome_riptide",+ "breakpoints": true}]-}+}\ No newline at end of filediff --git a/lib/db/schema.ts b/lib/db/schema.tsindex 32596ab8..c23d4936 100644--- a/lib/db/schema.ts+++ b/lib/db/schema.ts@@ -6,6 +6,7 @@ import {integer,json,jsonb,+ pgPolicy,pgTable,text,timestamp,@@ -38,19 +39,16 @@ export const chats = pgTable(.notNull().default('private')},- table => ({- // Index on userId for faster user-specific queries- userIdIdx: index('chats_user_id_idx').on(table.userId),- // Composite index on (userId, createdAt DESC) for optimized sorting- userIdCreatedAtIdx: index('chats_user_id_created_at_idx').on(+ table => [+ // Indexes+ index('chats_user_id_idx').on(table.userId),+ index('chats_user_id_created_at_idx').on(table.userId,table.createdAt.desc()),- // Index on createdAt for time-based queries- createdAtIdx: index('chats_created_at_idx').on(table.createdAt.desc()),- enableRls: true- })-)+ index('chats_created_at_idx').on(table.createdAt.desc())+ ]+).enableRLS()export type Chat = InferSelectModel<typeof chats>@@ -69,15 +67,14 @@ export const messages = pgTable(updatedAt: timestamp('updated_at'),metadata: jsonb('metadata').$type<Record<string, any>>()},- table => ({- chatIdIdx: index('messages_chat_id_idx').on(table.chatId),- chatIdCreatedAtIdx: index('messages_chat_id_created_at_idx').on(+ table => [+ index('messages_chat_id_idx').on(table.chatId),+ index('messages_chat_id_created_at_idx').on(table.chatId,table.createdAt- ),- enableRls: true- })-)+ )+ ]+).enableRLS()export type Message = InferSelectModel<typeof messages>@@ -161,38 +158,37 @@ export const parts = pgTable(createdAt: timestamp('created_at').notNull().defaultNow()},- table => ({+ table => [// Indexes- messageIdIdx: index('parts_message_id_idx').on(table.messageId),- messageIdOrderIdx: index('parts_message_id_order_idx').on(+ index('parts_message_id_idx').on(table.messageId),+ index('parts_message_id_order_idx').on(table.messageId,table.order),- enableRls: true,// Constraints- textTextRequired: check(+ check('text_text_required',sql`(type != 'text' OR text_text IS NOT NULL)`),- reasoningTextRequired: check(+ check('reasoning_text_required',sql`(type != 'reasoning' OR reasoning_text IS NOT NULL)`),- fileFieldsRequired: check(+ check('file_fields_required',sql`(type != 'file' OR (file_media_type IS NOT NULL AND file_filename IS NOT NULL AND file_url IS NOT NULL))`),- toolStateValid: check(+ check('tool_state_valid',sql`(tool_state IS NULL OR tool_state IN ('input-streaming', 'input-available', 'output-available', 'output-error'))`),- toolFieldsRequired: check(+ check('tool_fields_required',sql`(type NOT LIKE 'tool-%' OR (tool_tool_call_id IS NOT NULL AND tool_state IS NOT NULL))`)- })-)+ ]+).enableRLS()export type Part = InferSelectModel<typeof parts>export type NewPart = typeof parts.$inferInsert@@ -214,11 +210,18 @@ export const feedback = pgTable(userAgent: text('user_agent'),createdAt: timestamp('created_at').notNull().defaultNow()},- table => ({- userIdIdx: index('feedback_user_id_idx').on(table.userId),- createdAtIdx: index('feedback_created_at_idx').on(table.createdAt),- enableRls: true- })+ table => [+ // Indexes+ index('feedback_user_id_idx').on(table.userId),+ index('feedback_created_at_idx').on(table.createdAt),++ // RLS Policy - Allow anyone to insert feedback+ pgPolicy('anyone_can_insert_feedback', {+ for: 'insert',+ to: 'public',+ withCheck: sql`true`+ })+ ])export type Feedback = InferSelectModel<typeof feedback>