File size: 2,011 Bytes
03bff6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
  # Create storage bucket and policies

  1. Changes
    - Create codette-files storage bucket if it doesn't exist
    - Add RLS policies for authenticated users to:
      - Read files
      - Upload files
      - Update files
      - Delete files
    - Add safety checks to prevent policy conflicts
*/

-- Create the storage bucket
INSERT INTO storage.buckets (id, name)
VALUES ('codette-files', 'codette-files')
ON CONFLICT (id) DO NOTHING;

-- Set up RLS policies for the bucket with existence checks
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_policies 
        WHERE tablename = 'objects' 
        AND policyname = 'Allow authenticated users to read files'
    ) THEN
        CREATE POLICY "Allow authenticated users to read files"
        ON storage.objects FOR SELECT
        TO authenticated
        USING (bucket_id = 'codette-files');
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM pg_policies 
        WHERE tablename = 'objects' 
        AND policyname = 'Allow authenticated users to upload files'
    ) THEN
        CREATE POLICY "Allow authenticated users to upload files"
        ON storage.objects FOR INSERT
        TO authenticated
        WITH CHECK (bucket_id = 'codette-files');
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM pg_policies 
        WHERE tablename = 'objects' 
        AND policyname = 'Allow authenticated users to update files'
    ) THEN
        CREATE POLICY "Allow authenticated users to update files"
        ON storage.objects FOR UPDATE
        TO authenticated
        USING (bucket_id = 'codette-files')
        WITH CHECK (bucket_id = 'codette-files');
    END IF;

    IF NOT EXISTS (
        SELECT 1 FROM pg_policies 
        WHERE tablename = 'objects' 
        AND policyname = 'Allow authenticated users to delete files'
    ) THEN
        CREATE POLICY "Allow authenticated users to delete files"
        ON storage.objects FOR DELETE
        TO authenticated
        USING (bucket_id = 'codette-files');
    END IF;
END $$;