Skip to content
This repository has been archived by the owner on Feb 15, 2025. It is now read-only.

Commit

Permalink
chore: consolidate 0.8.0 database migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
YrrepNoj committed Jun 14, 2024
1 parent d2e4281 commit 6161d44
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 180 deletions.
17 changes: 0 additions & 17 deletions packages/api/supabase/migrations/20240419164109_assistant.sql

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
-- Create a table to store OpenAI Assistant Objects
create table
assistant_objects (
id uuid primary key DEFAULT uuid_generate_v4(),
created_at bigint default extract(epoch from now()) not null,
description varchar(512),
instructions text,
metadata jsonb,
model varchar(255) not null,
name varchar(255),
object text check (object in ('assistant')),
tools jsonb,
response_format jsonb,
temperature float,
tool_resources jsonb,
top_p float
);

-- Create a table to store the OpenAI Thread Objects
create table
thread_objects (
id uuid primary key DEFAULT uuid_generate_v4(),
user_id uuid references auth.users not null,
object text check (object in ('thread')),
created_at bigint default extract(epoch FROM NOW()) NOT NULL,
tool_resources jsonb,
metadata jsonb
);

-- Create a table to store the OpenAI Message Objects
create table
message_objects (
id uuid primary key DEFAULT uuid_generate_v4(),
user_id uuid references auth.users not null,
object text check (object in ('thread.message')),
created_at bigint default extract(epoch FROM NOW()) not null,
thread_id uuid references thread_objects (id) on delete cascade not null,
status text,
incomplete_details jsonb,
completed_at bigint,
incomplete_at bigint,
role text,
content jsonb,
assistant_id uuid, -- No foreign key constraint, can be null and doesn't have to refer to an assistant that exists
run_id uuid, -- No foreign key constraint, can be null and doesn't have to refer to a thread that exists
attachments jsonb,
metadata jsonb
);

-- RLS policies
alter table thread_objects enable row level security;
alter table message_objects enable row level security;

-- Policies for thread_objects
create policy "Individuals can view their own thread_objects." on thread_objects for
select using (auth.uid() = user_id);
create policy "Individuals can create thread_objects." on thread_objects for
insert with check (auth.uid() = user_id);
create policy "Individuals can update their own thread_objects." on thread_objects for
update using (auth.uid() = user_id);
create policy "Individuals can delete their own thread_objects." on thread_objects for
delete using (auth.uid() = user_id);

-- Policies for message_objects
create policy "Individuals can view their own message_objects." on message_objects for
select using (auth.uid() = user_id);
create policy "Individuals can create message_objects." on message_objects for
insert with check (auth.uid() = user_id);
create policy "Individuals can update their own message_objects." on message_objects for
update using (auth.uid() = user_id);
create policy "Individuals can delete their own message_objects." on message_objects for
delete using (auth.uid() = user_id);

-- Indexes for foreign keys for message_objects
CREATE INDEX message_objects_user_id ON message_objects (user_id);
CREATE INDEX message_objects_thread_id ON message_objects (thread_id);
CREATE INDEX message_objects_created_at ON thread_objects (created_at);

-- Indexes for common filtering and sorting for thread_objects
CREATE INDEX thread_objects_id ON thread_objects (id);
CREATE INDEX thread_objects_user_id ON thread_objects (user_id);
CREATE INDEX thread_objects_created_at ON thread_objects (created_at);

-- Add user_id column to assistant_objects and file_objects tables
alter table assistant_objects
add column user_id uuid references auth.users not null;
alter table file_objects
add column user_id uuid references auth.users not null;
-- Set buckets to private
update storage.buckets
set public = false
where id = 'file_bucket';

-- RLS policies
alter table assistant_objects enable row level security;
alter table file_objects enable row level security;

-- Policies for assistant_objects
create policy "Individuals can view their own assistant_objects. " on assistant_objects for
select using (auth.uid() = user_id);
create policy "Individuals can create assistant_objects." on assistant_objects for
insert with check (auth.uid() = user_id);
create policy "Individuals can update their own assistant_objects." on assistant_objects for
update using (auth.uid() = user_id);
create policy "Individuals can delete their own assistant_objects." on assistant_objects for
delete using (auth.uid() = user_id);

-- Policies for file_objects
create policy "Individuals can view their own file_objects." on file_objectsfor
select using (auth.uid() = user_id);
create policy "Individuals can create file_objects." on file_objects for
insert with check (auth.uid() = user_id);
create policy "Individuals can update their own file_objects." on file_objects for
update using (auth.uid() = user_id);
create policy "Individuals can delete their own file_objects." on file_objects for
delete using (auth.uid() = user_id);

-- Policies for file_bucket
create policy "Any authenticated individual can add files to file_bucket."
on storage.objects for
insert to authenticated with check (bucket_id = 'file_bucket');
create policy "Individuals can view their own files in the file_bucket."
on storage.objects for
select using (bucket_id = 'file_bucket' AND auth.uid() = owner);
create policy "Individuals can delete their own files."
on storage.objects for
delete using (bucket_id = 'file_bucket' AND auth.uid() = owner);
create policy "Individuals can update their own files in file_bucket."
on storage.objects for
update using (auth.uid() = owner) with check (bucket_id = 'file_bucket');

ALTER TABLE file_objects ALTER COLUMN created_at SET DEFAULT extract(epoch from now());
ALTER TABLE file_objects ALTER COLUMN created_at SET NOT NULL;
ALTER TABLE file_objects ADD CHECK (object in ('file'));
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,38 @@ begin
limit match_limit;
end;
$$;

-- RLS policies
alter table vector_store enable row level security;
alter table vector_store_file enable row level security;
alter table vector_content enable row level security;

-- Policies for vector_store
create policy "Individuals can view their own vector_store." on vector_store for
select using (auth.uid() = user_id);
create policy "Individuals can create vector_store." on vector_store for
insert with check (auth.uid() = user_id);
create policy "Individuals can update their own vector_store." on vector_store for
update using (auth.uid() = user_id);
create policy "Individuals can delete their own vector_store." on vector_store for
delete using (auth.uid() = user_id);

-- Policies for vector_store_file
create policy "Individuals can view their own vector_store_file." on vector_store_file for
select using (auth.uid() = user_id);
create policy "Individuals can create vector_store_file." on vector_store_file for
insert with check (auth.uid() = user_id);
create policy "Individuals can update their own vector_store_file." on vector_store_file for
update using (auth.uid() = user_id);
create policy "Individuals can delete their own vector_store_file." on vector_store_file for
delete using (auth.uid() = user_id);

-- Policies for vector_content
create policy "Individuals can view their own vector_content." on vector_content for
select using (auth.uid() = user_id);
create policy "Individuals can create vector_content." on vector_content for
insert with check (auth.uid() = user_id);
create policy "Individuals can update their own vector_content." on vector_content for
update using (auth.uid() = user_id);
create policy "Individuals can delete their own vector_content." on vector_content for
delete using (auth.uid() = user_id);
47 changes: 0 additions & 47 deletions packages/api/supabase/migrations/20240516152530_enable_rls.sql

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,19 @@ create policy "Users can insert their own profile." on profiles
for insert with check (auth.uid() = id);
create policy "Users can update own profile." on profiles
for update using (auth.uid() = id);

-- Drop tables we no longer need (These tables are managed by the API now)
DO $$
BEGIN
BEGIN
DROP TABLE IF EXISTS messages;
END;

BEGIN
DROP TABLE IF EXISTS conversations;
END;

BEGIN
DROP TABLE IF EXISTS assistants;
END;
END $$;

This file was deleted.

0 comments on commit 6161d44

Please sign in to comment.