An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat: enable RLS for chats and messages tables
Yoshiki Miura4c5244a29d87
diff --git a/drizzle/migrations/0002_enable_rls.sql b/drizzle/migrations/0002_enable_rls.sqlnew file mode 100644index 00000000..64cf4f61--- /dev/null+++ b/drizzle/migrations/0002_enable_rls.sql@@ -0,0 +1,87 @@+-- Enable RLS on tables+ALTER TABLE "chats" ENABLE ROW LEVEL SECURITY;+ALTER TABLE "messages" ENABLE ROW LEVEL SECURITY;++-- Create policies for chats table+CREATE POLICY "Users can view their own chats" ON "chats"+FOR SELECT+TO authenticated+USING ((auth.uid())::uuid = "user_id");++CREATE POLICY "Users can view public chats" ON "chats"+FOR SELECT+TO authenticated, anon+USING ("visibility" = 'public');++CREATE POLICY "Users can insert their own chats" ON "chats"+FOR INSERT+TO authenticated+WITH CHECK ((auth.uid())::uuid = "user_id");++CREATE POLICY "Users can update their own chats" ON "chats"+FOR UPDATE+TO authenticated+USING ((auth.uid())::uuid = "user_id")+WITH CHECK ((auth.uid())::uuid = "user_id");++CREATE POLICY "Users can delete their own chats" ON "chats"+FOR DELETE+TO authenticated+USING ((auth.uid())::uuid = "user_id");++-- Create policies for messages table+CREATE POLICY "Users can view messages from their own chats" ON "messages"+FOR SELECT+TO authenticated+USING (+ "chat_id" IN (+ SELECT "id" FROM "chats"+ WHERE (auth.uid())::uuid = "user_id"+ )+);++CREATE POLICY "Users can view messages from public chats" ON "messages"+FOR SELECT+TO authenticated, anon+USING (+ "chat_id" IN (+ SELECT "id" FROM "chats"+ WHERE "visibility" = 'public'+ )+);++CREATE POLICY "Users can insert messages to their own chats" ON "messages"+FOR INSERT+TO authenticated+WITH CHECK (+ "chat_id" IN (+ SELECT "id" FROM "chats"+ WHERE (auth.uid())::uuid = "user_id"+ )+);++CREATE POLICY "Users can update messages in their own chats" ON "messages"+FOR UPDATE+TO authenticated+USING (+ "chat_id" IN (+ SELECT "id" FROM "chats"+ WHERE (auth.uid())::uuid = "user_id"+ )+)+WITH CHECK (+ "chat_id" IN (+ SELECT "id" FROM "chats"+ WHERE (auth.uid())::uuid = "user_id"+ )+);++CREATE POLICY "Users can delete messages from their own chats" ON "messages"+FOR DELETE+TO authenticated+USING (+ "chat_id" IN (+ SELECT "id" FROM "chats"+ WHERE (auth.uid())::uuid = "user_id"+ )+);\ No newline at end of filediff --git a/drizzle/migrations/meta/0002_snapshot.json b/drizzle/migrations/meta/0002_snapshot.jsonnew file mode 100644index 00000000..e6a3bad4--- /dev/null+++ b/drizzle/migrations/meta/0002_snapshot.json@@ -0,0 +1,189 @@+{+ "id": "fb8e1dca-4a37-41e5-b928-c32e86e9f5b6",+ "prevId": "74bfe8c4-6a83-4e8a-9d1c-2b5a7f31e0f4",+ "version": "7",+ "dialect": "postgresql",+ "tables": {+ "public.chats": {+ "name": "chats",+ "schema": "",+ "columns": {+ "id": {+ "name": "id",+ "type": "uuid",+ "primaryKey": true,+ "notNull": true,+ "default": "gen_random_uuid()"+ },+ "created_at": {+ "name": "created_at",+ "type": "timestamp with time zone",+ "primaryKey": false,+ "notNull": true,+ "default": "now()"+ },+ "title": {+ "name": "title",+ "type": "text",+ "primaryKey": false,+ "notNull": true+ },+ "user_id": {+ "name": "user_id",+ "type": "uuid",+ "primaryKey": false,+ "notNull": true+ },+ "visibility": {+ "name": "visibility",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": true,+ "default": "'private'"+ }+ },+ "indexes": {},+ "foreignKeys": {},+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "policies": {+ "Users can view their own chats": {+ "using": "(auth.uid()::uuid = user_id)",+ "check": null,+ "operation": "select",+ "roles": ["authenticated"]+ },+ "Users can view public chats": {+ "using": "(visibility = 'public')",+ "check": null,+ "operation": "select",+ "roles": ["authenticated", "anon"]+ },+ "Users can insert their own chats": {+ "using": null,+ "check": "(auth.uid()::uuid = user_id)",+ "operation": "insert",+ "roles": ["authenticated"]+ },+ "Users can update their own chats": {+ "using": "(auth.uid()::uuid = user_id)",+ "check": "(auth.uid()::uuid = user_id)",+ "operation": "update",+ "roles": ["authenticated"]+ },+ "Users can delete their own chats": {+ "using": "(auth.uid()::uuid = user_id)",+ "check": null,+ "operation": "delete",+ "roles": ["authenticated"]+ }+ },+ "checkConstraints": {},+ "isRLSEnabled": true+ },+ "public.messages": {+ "name": "messages",+ "schema": "",+ "columns": {+ "id": {+ "name": "id",+ "type": "uuid",+ "primaryKey": true,+ "notNull": true,+ "default": "gen_random_uuid()"+ },+ "chat_id": {+ "name": "chat_id",+ "type": "uuid",+ "primaryKey": false,+ "notNull": true+ },+ "role": {+ "name": "role",+ "type": "varchar(256)",+ "primaryKey": false,+ "notNull": true+ },+ "parts": {+ "name": "parts",+ "type": "json",+ "primaryKey": false,+ "notNull": true+ },+ "attachments": {+ "name": "attachments",+ "type": "json",+ "primaryKey": false,+ "notNull": true,+ "default": "'{}'::jsonb"+ },+ "created_at": {+ "name": "created_at",+ "type": "timestamp with time zone",+ "primaryKey": false,+ "notNull": true,+ "default": "now()"+ }+ },+ "indexes": {},+ "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": {+ "Users can view messages from their own chats": {+ "using": "(chat_id IN (SELECT id FROM chats WHERE (auth.uid())::uuid = user_id))",+ "check": null,+ "operation": "select",+ "roles": ["authenticated"]+ },+ "Users can view messages from public chats": {+ "using": "(chat_id IN (SELECT id FROM chats WHERE visibility = 'public'))",+ "check": null,+ "operation": "select",+ "roles": ["authenticated", "anon"]+ },+ "Users can insert messages to their own chats": {+ "using": null,+ "check": "(chat_id IN (SELECT id FROM chats WHERE (auth.uid())::uuid = user_id))",+ "operation": "insert",+ "roles": ["authenticated"]+ },+ "Users can update messages in their own chats": {+ "using": "(chat_id IN (SELECT id FROM chats WHERE (auth.uid())::uuid = user_id))",+ "check": "(chat_id IN (SELECT id FROM chats WHERE (auth.uid())::uuid = user_id))",+ "operation": "update",+ "roles": ["authenticated"]+ },+ "Users can delete messages from their own chats": {+ "using": "(chat_id IN (SELECT id FROM chats WHERE (auth.uid())::uuid = user_id))",+ "check": null,+ "operation": "delete",+ "roles": ["authenticated"]+ }+ },+ "checkConstraints": {},+ "isRLSEnabled": true+ }+ },+ "enums": {},+ "schemas": {},+ "sequences": {},+ "roles": {},+ "policies": {},+ "views": {},+ "_meta": {+ "columns": {},+ "schemas": {},+ "tables": {}+ }+}diff --git a/drizzle/migrations/meta/_journal.json b/drizzle/migrations/meta/_journal.jsonindex 7cc47ea0..2d9836ec 100644--- a/drizzle/migrations/meta/_journal.json+++ b/drizzle/migrations/meta/_journal.json@@ -15,6 +15,13 @@"when": 1747030781234,"tag": "0001_update_schema_drop_updated_at","breakpoints": true+ },+ {+ "idx": 2,+ "version": "7",+ "when": 1747032500000,+ "tag": "0002_enable_rls",+ "breakpoints": true}]}diff --git a/lib/db/schema.ts b/lib/db/schema.tsindex a6e7106e..ca8aa26a 100644--- a/lib/db/schema.ts+++ b/lib/db/schema.ts@@ -8,34 +8,50 @@ import {varchar} from 'drizzle-orm/pg-core'-export const chats = pgTable('chats', {- id: uuid('id').primaryKey().notNull().defaultRandom(),- createdAt: timestamp('created_at', { mode: 'string', withTimezone: true })- .notNull()- .defaultNow(),- title: text('title').notNull(),- userId: uuid('user_id').notNull(), // User ID from the authentication system- visibility: varchar('visibility', {- length: 256,- enum: ['public', 'private']- })- .notNull()- .default('private')-})+export const chats = pgTable(+ 'chats',+ {+ id: uuid('id').primaryKey().notNull().defaultRandom(),+ createdAt: timestamp('created_at', { mode: 'string', withTimezone: true })+ .notNull()+ .defaultNow(),+ title: text('title').notNull(),+ userId: uuid('user_id').notNull(), // User ID from the authentication system+ visibility: varchar('visibility', {+ length: 256,+ enum: ['public', 'private']+ })+ .notNull()+ .default('private')+ },+ table => {+ return {+ enableRls: true+ }+ }+)export type Chat = InferSelectModel<typeof chats>-export const messages = pgTable('messages', {- id: uuid('id').primaryKey().notNull().defaultRandom(),- chatId: uuid('chat_id')- .notNull()- .references(() => chats.id, { onDelete: 'cascade' }),- role: varchar('role', { length: 256 }).notNull(),- parts: json('parts').notNull(), // Stores structured message content (e.g., Vercel AI SDK Message parts)- attachments: json('attachments').notNull(),- createdAt: timestamp('created_at', { mode: 'string', withTimezone: true })- .notNull()- .defaultNow()-})+export const messages = pgTable(+ 'messages',+ {+ id: uuid('id').primaryKey().notNull().defaultRandom(),+ chatId: uuid('chat_id')+ .notNull()+ .references(() => chats.id, { onDelete: 'cascade' }),+ role: varchar('role', { length: 256 }).notNull(),+ parts: json('parts').notNull(), // Stores structured message content (e.g., Vercel AI SDK Message parts)+ attachments: json('attachments').notNull(),+ createdAt: timestamp('created_at', { mode: 'string', withTimezone: true })+ .notNull()+ .defaultNow()+ },+ table => {+ return {+ enableRls: true+ }+ }+)export type Message = InferSelectModel<typeof messages>