|
| 1 | +-- ============================================================================= |
| 2 | +-- Create workspace_files table for IDE file management |
| 3 | +-- ============================================================================= |
| 4 | + |
| 5 | +CREATE TABLE IF NOT EXISTS public.workspace_files ( |
| 6 | + id uuid NOT NULL DEFAULT gen_random_uuid(), |
| 7 | + user_id uuid NOT NULL, |
| 8 | + project_id uuid, |
| 9 | + path text NOT NULL, |
| 10 | + name text NOT NULL, |
| 11 | + content text NOT NULL DEFAULT '', |
| 12 | + is_directory boolean DEFAULT false, |
| 13 | + parent_path text, |
| 14 | + mime_type text, |
| 15 | + size_bytes bigint DEFAULT 0, |
| 16 | + metadata jsonb DEFAULT '{}'::jsonb, |
| 17 | + created_at timestamp with time zone DEFAULT now(), |
| 18 | + updated_at timestamp with time zone DEFAULT now(), |
| 19 | + CONSTRAINT workspace_files_pkey PRIMARY KEY (id), |
| 20 | + CONSTRAINT workspace_files_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE, |
| 21 | + CONSTRAINT workspace_files_project_id_fkey FOREIGN KEY (project_id) REFERENCES public.projects(id) ON DELETE CASCADE, |
| 22 | + CONSTRAINT workspace_files_unique_path UNIQUE (user_id, path), |
| 23 | + CONSTRAINT workspace_files_path_check CHECK (char_length(path) >= 1), |
| 24 | + CONSTRAINT workspace_files_name_check CHECK (char_length(name) >= 1), |
| 25 | + CONSTRAINT workspace_files_size_check CHECK (size_bytes >= 0) |
| 26 | +); |
| 27 | + |
| 28 | +-- Create indexes for performance |
| 29 | +CREATE INDEX IF NOT EXISTS idx_workspace_files_user_id ON public.workspace_files (user_id); |
| 30 | +CREATE INDEX IF NOT EXISTS idx_workspace_files_project_id ON public.workspace_files (project_id); |
| 31 | +CREATE INDEX IF NOT EXISTS idx_workspace_files_path ON public.workspace_files (user_id, path); |
| 32 | +CREATE INDEX IF NOT EXISTS idx_workspace_files_parent_path ON public.workspace_files (user_id, parent_path); |
| 33 | +CREATE INDEX IF NOT EXISTS idx_workspace_files_created_at ON public.workspace_files (created_at DESC); |
| 34 | + |
| 35 | +-- Enable Row Level Security |
| 36 | +ALTER TABLE public.workspace_files ENABLE ROW LEVEL SECURITY; |
| 37 | + |
| 38 | +-- RLS Policies |
| 39 | +CREATE POLICY "Users can view their own workspace files" ON public.workspace_files |
| 40 | + FOR SELECT USING (auth.uid() = user_id); |
| 41 | + |
| 42 | +CREATE POLICY "Users can insert their own workspace files" ON public.workspace_files |
| 43 | + FOR INSERT WITH CHECK (auth.uid() = user_id); |
| 44 | + |
| 45 | +CREATE POLICY "Users can update their own workspace files" ON public.workspace_files |
| 46 | + FOR UPDATE USING (auth.uid() = user_id); |
| 47 | + |
| 48 | +CREATE POLICY "Users can delete their own workspace files" ON public.workspace_files |
| 49 | + FOR DELETE USING (auth.uid() = user_id); |
| 50 | + |
| 51 | +-- Add trigger for automatic timestamp updates |
| 52 | +CREATE TRIGGER update_workspace_files_updated_at BEFORE UPDATE ON public.workspace_files |
| 53 | + FOR EACH ROW EXECUTE FUNCTION update_updated_at_column(); |
| 54 | + |
| 55 | +-- Grant permissions |
| 56 | +GRANT ALL ON public.workspace_files TO authenticated; |
0 commit comments