Raiff1982 commited on
Commit
03bff6f
·
verified ·
1 Parent(s): e6c38fc

Upload 20 files

Browse files
migrations/20250523100814_raspy_torch.sql ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Create codette_files table for file management
3
+
4
+ 1. New Tables
5
+ - `codette_files`
6
+ - `id` (uuid, primary key)
7
+ - `filename` (text)
8
+ - `storage_path` (text)
9
+ - `file_type` (text)
10
+ - `uploaded_at` (timestamptz)
11
+ - `created_at` (timestamptz)
12
+
13
+ 2. Security
14
+ - Enable RLS on `codette_files` table
15
+ - Add policies for:
16
+ - Authenticated users can read all files
17
+ - Authenticated users can insert their own files
18
+ */
19
+
20
+ CREATE TABLE IF NOT EXISTS public.codette_files (
21
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
22
+ filename text NOT NULL,
23
+ storage_path text NOT NULL,
24
+ file_type text,
25
+ uploaded_at timestamptz DEFAULT now(),
26
+ created_at timestamptz DEFAULT now()
27
+ );
28
+
29
+ -- Enable Row Level Security
30
+ ALTER TABLE public.codette_files ENABLE ROW LEVEL SECURITY;
31
+
32
+ -- Create policies
33
+ CREATE POLICY "Allow authenticated users to read files"
34
+ ON public.codette_files
35
+ FOR SELECT
36
+ TO authenticated
37
+ USING (true);
38
+
39
+ CREATE POLICY "Allow authenticated users to insert files"
40
+ ON public.codette_files
41
+ FOR INSERT
42
+ TO authenticated
43
+ WITH CHECK (true);
migrations/20250523120906_wild_torch.sql ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Create storage bucket for Codette files
3
+
4
+ 1. New Storage Bucket
5
+ - Creates 'codette-files' bucket for storing uploaded files
6
+ 2. Security
7
+ - Enable public access for authenticated users
8
+ - Add policies for read and write operations
9
+ */
10
+
11
+ -- Create the storage bucket
12
+ INSERT INTO storage.buckets (id, name)
13
+ VALUES ('codette-files', 'codette-files')
14
+ ON CONFLICT (id) DO NOTHING;
15
+
16
+ -- Set up RLS policies for the bucket
17
+ CREATE POLICY "Allow authenticated users to read files"
18
+ ON storage.objects FOR SELECT
19
+ TO authenticated
20
+ USING (bucket_id = 'codette-files');
21
+
22
+ CREATE POLICY "Allow authenticated users to upload files"
23
+ ON storage.objects FOR INSERT
24
+ TO authenticated
25
+ WITH CHECK (bucket_id = 'codette-files');
26
+
27
+ CREATE POLICY "Allow authenticated users to update files"
28
+ ON storage.objects FOR UPDATE
29
+ TO authenticated
30
+ USING (bucket_id = 'codette-files')
31
+ WITH CHECK (bucket_id = 'codette-files');
32
+
33
+ CREATE POLICY "Allow authenticated users to delete files"
34
+ ON storage.objects FOR DELETE
35
+ TO authenticated
36
+ USING (bucket_id = 'codette-files');
migrations/20250523121149_rough_jungle.sql ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Create storage bucket and policies
3
+
4
+ 1. Changes
5
+ - Create codette-files storage bucket if it doesn't exist
6
+ - Add RLS policies for authenticated users to:
7
+ - Read files
8
+ - Upload files
9
+ - Update files
10
+ - Delete files
11
+ - Add safety checks to prevent policy conflicts
12
+ */
13
+
14
+ -- Create the storage bucket
15
+ INSERT INTO storage.buckets (id, name)
16
+ VALUES ('codette-files', 'codette-files')
17
+ ON CONFLICT (id) DO NOTHING;
18
+
19
+ -- Set up RLS policies for the bucket with existence checks
20
+ DO $$
21
+ BEGIN
22
+ IF NOT EXISTS (
23
+ SELECT 1 FROM pg_policies
24
+ WHERE tablename = 'objects'
25
+ AND policyname = 'Allow authenticated users to read files'
26
+ ) THEN
27
+ CREATE POLICY "Allow authenticated users to read files"
28
+ ON storage.objects FOR SELECT
29
+ TO authenticated
30
+ USING (bucket_id = 'codette-files');
31
+ END IF;
32
+
33
+ IF NOT EXISTS (
34
+ SELECT 1 FROM pg_policies
35
+ WHERE tablename = 'objects'
36
+ AND policyname = 'Allow authenticated users to upload files'
37
+ ) THEN
38
+ CREATE POLICY "Allow authenticated users to upload files"
39
+ ON storage.objects FOR INSERT
40
+ TO authenticated
41
+ WITH CHECK (bucket_id = 'codette-files');
42
+ END IF;
43
+
44
+ IF NOT EXISTS (
45
+ SELECT 1 FROM pg_policies
46
+ WHERE tablename = 'objects'
47
+ AND policyname = 'Allow authenticated users to update files'
48
+ ) THEN
49
+ CREATE POLICY "Allow authenticated users to update files"
50
+ ON storage.objects FOR UPDATE
51
+ TO authenticated
52
+ USING (bucket_id = 'codette-files')
53
+ WITH CHECK (bucket_id = 'codette-files');
54
+ END IF;
55
+
56
+ IF NOT EXISTS (
57
+ SELECT 1 FROM pg_policies
58
+ WHERE tablename = 'objects'
59
+ AND policyname = 'Allow authenticated users to delete files'
60
+ ) THEN
61
+ CREATE POLICY "Allow authenticated users to delete files"
62
+ ON storage.objects FOR DELETE
63
+ TO authenticated
64
+ USING (bucket_id = 'codette-files');
65
+ END IF;
66
+ END $$;
migrations/20250523125621_rapid_flower.sql ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Update storage policies with existence checks
3
+
4
+ 1. Changes
5
+ - Add existence checks before creating each policy
6
+ - Only create policies that don't already exist
7
+ - Maintain all required policies for the storage bucket
8
+
9
+ 2. Security
10
+ - Maintain existing RLS policies
11
+ - Ensure proper access control for authenticated users
12
+ - Preserve admin-only upload restrictions
13
+ */
14
+
15
+ -- Wrap everything in a transaction
16
+ BEGIN;
17
+
18
+ -- Create policies with existence checks
19
+ DO $$
20
+ BEGIN
21
+ -- Check and create read policy
22
+ IF NOT EXISTS (
23
+ SELECT 1 FROM pg_policies
24
+ WHERE tablename = 'objects'
25
+ AND schemaname = 'storage'
26
+ AND policyname = 'Allow authenticated users to read files'
27
+ ) THEN
28
+ CREATE POLICY "Allow authenticated users to read files"
29
+ ON storage.objects FOR SELECT
30
+ TO authenticated
31
+ USING (bucket_id = 'codette-files');
32
+ END IF;
33
+
34
+ -- Check and create upload policy for admin users
35
+ IF NOT EXISTS (
36
+ SELECT 1 FROM pg_policies
37
+ WHERE tablename = 'objects'
38
+ AND schemaname = 'storage'
39
+ AND policyname = 'Allow admin users to upload files'
40
+ ) THEN
41
+ CREATE POLICY "Allow admin users to upload files"
42
+ ON storage.objects FOR INSERT
43
+ TO authenticated
44
+ WITH CHECK (bucket_id = 'codette-files' AND auth.jwt() ->> 'role' = 'admin');
45
+ END IF;
46
+
47
+ -- Check and create policy for admin file insertion
48
+ IF NOT EXISTS (
49
+ SELECT 1 FROM pg_policies
50
+ WHERE tablename = 'codette_files'
51
+ AND schemaname = 'public'
52
+ AND policyname = 'Allow admin users to insert files'
53
+ ) THEN
54
+ CREATE POLICY "Allow admin users to insert files"
55
+ ON public.codette_files FOR INSERT
56
+ TO authenticated
57
+ WITH CHECK (auth.jwt() ->> 'role' = 'admin');
58
+ END IF;
59
+ END $$;
60
+
61
+ COMMIT;
migrations/20250523141836_heavy_butterfly.sql ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Storage and RLS Policy Setup
3
+
4
+ 1. Changes
5
+ - Create storage bucket policies for file access
6
+ - Create table policies for file management
7
+ - Enable RLS on codette_files table
8
+
9
+ 2. Security
10
+ - Authenticated users can read files
11
+ - Admin users can upload files
12
+ - RLS enabled on codette_files table
13
+ */
14
+
15
+ -- Create storage bucket if it doesn't exist
16
+ DO $$
17
+ BEGIN
18
+ INSERT INTO storage.buckets (id, name)
19
+ VALUES ('codette-files', 'codette-files')
20
+ ON CONFLICT (id) DO NOTHING;
21
+ END $$;
22
+
23
+ -- Storage Policies
24
+ DO $$
25
+ BEGIN
26
+ -- Drop existing policies to avoid conflicts
27
+ DROP POLICY IF EXISTS "Allow authenticated users to read files" ON storage.objects;
28
+ DROP POLICY IF EXISTS "Allow admin users to upload files" ON storage.objects;
29
+
30
+ -- Create new storage policies
31
+ CREATE POLICY "Allow authenticated users to read files"
32
+ ON storage.objects FOR SELECT
33
+ TO authenticated
34
+ USING (bucket_id = 'codette-files');
35
+
36
+ CREATE POLICY "Allow admin users to upload files"
37
+ ON storage.objects FOR INSERT
38
+ TO authenticated
39
+ WITH CHECK (
40
+ bucket_id = 'codette-files'
41
+ AND (auth.jwt() ->> 'role' = 'admin')
42
+ );
43
+ END $$;
44
+
45
+ -- File Management Table Policies
46
+ DO $$
47
+ BEGIN
48
+ -- Drop existing policies to avoid conflicts
49
+ DROP POLICY IF EXISTS "Allow authenticated users to read files" ON public.codette_files;
50
+ DROP POLICY IF EXISTS "Allow admin users to insert files" ON public.codette_files;
51
+ DROP POLICY IF EXISTS "Allow authenticated users to insert files" ON public.codette_files;
52
+
53
+ -- Create new table policies
54
+ CREATE POLICY "Allow authenticated users to read files"
55
+ ON public.codette_files FOR SELECT
56
+ TO authenticated
57
+ USING (true);
58
+
59
+ CREATE POLICY "Allow admin users to insert files"
60
+ ON public.codette_files FOR INSERT
61
+ TO authenticated
62
+ WITH CHECK (auth.jwt() ->> 'role' = 'admin');
63
+
64
+ CREATE POLICY "Allow authenticated users to insert files"
65
+ ON public.codette_files FOR INSERT
66
+ TO authenticated
67
+ WITH CHECK (true);
68
+ END $$;
69
+
70
+ -- Enable RLS
71
+ ALTER TABLE public.codette_files ENABLE ROW LEVEL SECURITY;
migrations/20250523175402_white_torch.sql ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Storage and File Access Policies
3
+
4
+ 1. New Policies
5
+ - Enable RLS on codette_files table
6
+ - Create policies for file access and management
7
+
8
+ 2. Security
9
+ - Allow authenticated users to read files
10
+ - Allow admin users to upload files
11
+ - Allow authenticated users to insert file records
12
+ */
13
+
14
+ -- Enable RLS on the codette_files table if not already enabled
15
+ DO $$
16
+ BEGIN
17
+ IF NOT EXISTS (
18
+ SELECT 1 FROM pg_tables
19
+ WHERE tablename = 'codette_files'
20
+ AND rowsecurity = true
21
+ ) THEN
22
+ ALTER TABLE public.codette_files ENABLE ROW LEVEL SECURITY;
23
+ END IF;
24
+ END $$;
25
+
26
+ -- Create storage bucket if it doesn't exist
27
+ DO $$
28
+ BEGIN
29
+ IF NOT EXISTS (
30
+ SELECT 1 FROM storage.buckets WHERE name = 'codette-files'
31
+ ) THEN
32
+ INSERT INTO storage.buckets (id, name)
33
+ VALUES ('codette-files', 'codette-files');
34
+ END IF;
35
+ END $$;
36
+
37
+ -- Create policies for the codette_files table
38
+ DO $$
39
+ BEGIN
40
+ -- Check if the read policy exists
41
+ IF NOT EXISTS (
42
+ SELECT 1 FROM pg_policies
43
+ WHERE policyname = 'Allow authenticated users to read files'
44
+ AND tablename = 'codette_files'
45
+ ) THEN
46
+ CREATE POLICY "Allow authenticated users to read files"
47
+ ON public.codette_files FOR SELECT
48
+ TO authenticated
49
+ USING (true);
50
+ END IF;
51
+
52
+ -- Check if the admin insert policy exists
53
+ IF NOT EXISTS (
54
+ SELECT 1 FROM pg_policies
55
+ WHERE policyname = 'Allow admin users to insert files'
56
+ AND tablename = 'codette_files'
57
+ ) THEN
58
+ CREATE POLICY "Allow admin users to insert files"
59
+ ON public.codette_files FOR INSERT
60
+ TO authenticated
61
+ WITH CHECK (auth.jwt() ->> 'role' = 'admin');
62
+ END IF;
63
+
64
+ -- Check if the authenticated insert policy exists
65
+ IF NOT EXISTS (
66
+ SELECT 1 FROM pg_policies
67
+ WHERE policyname = 'Allow authenticated users to insert files'
68
+ AND tablename = 'codette_files'
69
+ ) THEN
70
+ CREATE POLICY "Allow authenticated users to insert files"
71
+ ON public.codette_files FOR INSERT
72
+ TO authenticated
73
+ WITH CHECK (true);
74
+ END IF;
75
+ END $$;
76
+
77
+ -- Note: Storage policies for the storage.objects table need to be created through the Supabase dashboard
78
+ -- or using the Supabase CLI, as they require special permissions that aren't available in migrations.
79
+ -- Please create the following policies manually:
80
+ -- 1. "Allow authenticated users to read files" - For SELECT operations on storage.objects where bucket_id = 'codette-files'
81
+ -- 2. "Allow admin users to upload files" - For INSERT operations on storage.objects where bucket_id = 'codette-files' AND auth.jwt() ->> 'role' = 'admin'
migrations/20250523182801_long_field.sql ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Storage and File Management Policies
3
+
4
+ 1. New Tables
5
+ - No new tables created
6
+ 2. Security
7
+ - Enable RLS on codette_files table
8
+ - Add policies for authenticated users to read files
9
+ - Add policies for authenticated users to insert files
10
+ - Add special policy for admin users to insert files
11
+ 3. Changes
12
+ - Ensures storage bucket exists for file storage
13
+ */
14
+
15
+ -- Enable RLS on the codette_files table if not already enabled
16
+ DO $$
17
+ BEGIN
18
+ IF NOT EXISTS (
19
+ SELECT 1 FROM pg_tables
20
+ WHERE tablename = 'codette_files'
21
+ AND rowsecurity = true
22
+ ) THEN
23
+ ALTER TABLE public.codette_files ENABLE ROW LEVEL SECURITY;
24
+ END IF;
25
+ END $$;
26
+
27
+ -- Create storage bucket if it doesn't exist
28
+ DO $$
29
+ BEGIN
30
+ IF NOT EXISTS (
31
+ SELECT 1 FROM storage.buckets WHERE name = 'codette-files'
32
+ ) THEN
33
+ INSERT INTO storage.buckets (id, name)
34
+ VALUES ('codette-files', 'codette-files');
35
+ END IF;
36
+ END $$;
37
+
38
+ -- Create policies for the codette_files table
39
+ DO $$
40
+ BEGIN
41
+ -- Check if the read policy exists
42
+ IF NOT EXISTS (
43
+ SELECT 1 FROM pg_policies
44
+ WHERE policyname = 'Allow authenticated users to read files'
45
+ AND tablename = 'codette_files'
46
+ ) THEN
47
+ CREATE POLICY "Allow authenticated users to read files"
48
+ ON public.codette_files FOR SELECT
49
+ TO authenticated
50
+ USING (true);
51
+ END IF;
52
+
53
+ -- Check if the admin insert policy exists
54
+ IF NOT EXISTS (
55
+ SELECT 1 FROM pg_policies
56
+ WHERE policyname = 'Allow admin users to insert files'
57
+ AND tablename = 'codette_files'
58
+ ) THEN
59
+ CREATE POLICY "Allow admin users to insert files"
60
+ ON public.codette_files FOR INSERT
61
+ TO authenticated
62
+ WITH CHECK (auth.jwt() ->> 'role' = 'admin');
63
+ END IF;
64
+
65
+ -- Check if the authenticated insert policy exists
66
+ IF NOT EXISTS (
67
+ SELECT 1 FROM pg_policies
68
+ WHERE policyname = 'Allow authenticated users to insert files'
69
+ AND tablename = 'codette_files'
70
+ ) THEN
71
+ CREATE POLICY "Allow authenticated users to insert files"
72
+ ON public.codette_files FOR INSERT
73
+ TO authenticated
74
+ WITH CHECK (true);
75
+ END IF;
76
+ END $$;
77
+
78
+ -- Note: Storage policies for the storage.objects table need to be created through the Supabase dashboard
79
+ -- or using the Supabase CLI, as they require special permissions that aren't available in migrations.
80
+ -- Please create the following policies manually:
81
+ -- 1. "Allow authenticated users to read files" - For SELECT operations on storage.objects where bucket_id = 'codette-files'
82
+ -- 2. "Allow admin users to upload files" - For INSERT operations on storage.objects where bucket_id = 'codette-files' AND auth.jwt() ->> 'role' = 'admin'
migrations/20250523183206_odd_moon.sql ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Storage and File Management Setup
3
+
4
+ 1. New Storage
5
+ - Create 'codette-files' storage bucket if it doesn't exist
6
+
7
+ 2. Security
8
+ - Enable Row Level Security on codette_files table
9
+ - Create policies for authenticated users to read files
10
+ - Create policies for authenticated users to insert files
11
+ - Create special policy for admin users to insert files
12
+ */
13
+
14
+ -- Enable RLS on the codette_files table if not already enabled
15
+ DO $$
16
+ BEGIN
17
+ IF NOT EXISTS (
18
+ SELECT 1 FROM pg_tables
19
+ WHERE tablename = 'codette_files'
20
+ AND rowsecurity = true
21
+ ) THEN
22
+ ALTER TABLE public.codette_files ENABLE ROW LEVEL SECURITY;
23
+ END IF;
24
+ END $$;
25
+
26
+ -- Create storage bucket if it doesn't exist
27
+ DO $$
28
+ BEGIN
29
+ IF NOT EXISTS (
30
+ SELECT 1 FROM storage.buckets WHERE name = 'codette-files'
31
+ ) THEN
32
+ INSERT INTO storage.buckets (id, name)
33
+ VALUES ('codette-files', 'codette-files');
34
+ END IF;
35
+ END $$;
36
+
37
+ -- Create policies for the codette_files table
38
+ DO $$
39
+ BEGIN
40
+ -- Check if the read policy exists
41
+ IF NOT EXISTS (
42
+ SELECT 1 FROM pg_policies
43
+ WHERE policyname = 'Allow authenticated users to read files'
44
+ AND tablename = 'codette_files'
45
+ ) THEN
46
+ CREATE POLICY "Allow authenticated users to read files"
47
+ ON public.codette_files FOR SELECT
48
+ TO authenticated
49
+ USING (true);
50
+ END IF;
51
+
52
+ -- Check if the admin insert policy exists
53
+ IF NOT EXISTS (
54
+ SELECT 1 FROM pg_policies
55
+ WHERE policyname = 'Allow admin users to insert files'
56
+ AND tablename = 'codette_files'
57
+ ) THEN
58
+ CREATE POLICY "Allow admin users to insert files"
59
+ ON public.codette_files FOR INSERT
60
+ TO authenticated
61
+ WITH CHECK ((auth.jwt() ->> 'role')::text = 'admin');
62
+ END IF;
63
+
64
+ -- Check if the authenticated insert policy exists
65
+ IF NOT EXISTS (
66
+ SELECT 1 FROM pg_policies
67
+ WHERE policyname = 'Allow authenticated users to insert files'
68
+ AND tablename = 'codette_files'
69
+ ) THEN
70
+ CREATE POLICY "Allow authenticated users to insert files"
71
+ ON public.codette_files FOR INSERT
72
+ TO authenticated
73
+ WITH CHECK (true);
74
+ END IF;
75
+ END $$;
76
+
77
+ -- Note: For storage.objects policies, you'll need to create them through the Supabase dashboard
78
+ -- as migrations don't have sufficient permissions to create these policies directly.
79
+ -- Create these policies manually:
80
+ -- 1. Policy name: "Allow authenticated users to read files"
81
+ -- - For: SELECT operations
82
+ -- - Using expression: bucket_id = 'codette-files'
83
+ --
84
+ -- 2. Policy name: "Allow admin users to upload files"
85
+ -- - For: INSERT operations
86
+ -- - Using expression: bucket_id = 'codette-files' AND (auth.jwt() ->> 'role')::text = 'admin'
migrations/20250523213744_long_sun.sql ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Storage and File Management Setup
3
+
4
+ 1. New Storage Configuration
5
+ - Creates 'codette-files' storage bucket if it doesn't exist
6
+ - Sets up proper file management structure
7
+
8
+ 2. Table Policies
9
+ - Enables RLS on codette_files table
10
+ - Creates read policy for authenticated users
11
+ - Creates insert policies for both admin and authenticated users
12
+ - Ensures proper access control and security
13
+
14
+ Note: Storage object policies must be created manually through Supabase dashboard
15
+ */
16
+
17
+ -- Enable RLS on the codette_files table if not already enabled
18
+ DO $$
19
+ BEGIN
20
+ IF NOT EXISTS (
21
+ SELECT 1 FROM pg_tables
22
+ WHERE tablename = 'codette_files'
23
+ AND rowsecurity = true
24
+ ) THEN
25
+ ALTER TABLE public.codette_files ENABLE ROW LEVEL SECURITY;
26
+ END IF;
27
+ END $$;
28
+
29
+ -- Create storage bucket if it doesn't exist
30
+ DO $$
31
+ BEGIN
32
+ IF NOT EXISTS (
33
+ SELECT 1 FROM storage.buckets WHERE name = 'codette-files'
34
+ ) THEN
35
+ INSERT INTO storage.buckets (id, name, public)
36
+ VALUES ('codette-files', 'codette-files', false);
37
+ END IF;
38
+ END $$;
39
+
40
+ -- Create policies for the codette_files table
41
+ DO $$
42
+ BEGIN
43
+ -- Create read policy if it doesn't exist
44
+ IF NOT EXISTS (
45
+ SELECT 1 FROM pg_policies
46
+ WHERE policyname = 'Allow authenticated users to read files'
47
+ AND tablename = 'codette_files'
48
+ ) THEN
49
+ CREATE POLICY "Allow authenticated users to read files"
50
+ ON public.codette_files FOR SELECT
51
+ TO authenticated
52
+ USING (true);
53
+ END IF;
54
+
55
+ -- Create admin insert policy if it doesn't exist
56
+ IF NOT EXISTS (
57
+ SELECT 1 FROM pg_policies
58
+ WHERE policyname = 'Allow admin users to insert files'
59
+ AND tablename = 'codette_files'
60
+ ) THEN
61
+ CREATE POLICY "Allow admin users to insert files"
62
+ ON public.codette_files FOR INSERT
63
+ TO authenticated
64
+ WITH CHECK ((auth.jwt() ->> 'role')::text = 'admin');
65
+ END IF;
66
+
67
+ -- Create authenticated insert policy if it doesn't exist
68
+ IF NOT EXISTS (
69
+ SELECT 1 FROM pg_policies
70
+ WHERE policyname = 'Allow authenticated users to insert files'
71
+ AND tablename = 'codette_files'
72
+ ) THEN
73
+ CREATE POLICY "Allow authenticated users to insert files"
74
+ ON public.codette_files FOR INSERT
75
+ TO authenticated
76
+ WITH CHECK (true);
77
+ END IF;
78
+ END $$;
79
+
80
+ -- Important: Storage object policies must be created manually through the Supabase dashboard
81
+ -- Create the following policies:
82
+ -- 1. "Allow authenticated users to read files"
83
+ -- - Operation: SELECT
84
+ -- - Target roles: authenticated
85
+ -- - Using expression: bucket_id = 'codette-files'
86
+ --
87
+ -- 2. "Allow admin users to upload files"
88
+ -- - Operation: INSERT
89
+ -- - Target roles: authenticated
90
+ -- - Using expression: bucket_id = 'codette-files' AND (auth.jwt() ->> 'role')::text = 'admin'
migrations/20250523222316_square_gate.sql ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Fix RLS policies for codette_files table
3
+
4
+ 1. Changes
5
+ - Drop existing RLS policies that might be conflicting
6
+ - Add new RLS policies for admin users
7
+ - Allow admin users to insert files
8
+ - Allow admin users to read files
9
+ - Allow admin users to update files
10
+ - Allow admin users to delete files
11
+ - Add RLS policies for regular authenticated users
12
+ - Allow reading files only
13
+
14
+ 2. Security
15
+ - Ensures only admin users can upload/modify files
16
+ - All authenticated users can read files
17
+ - Proper RLS enforcement for file management
18
+ */
19
+
20
+ -- Drop existing policies to avoid conflicts
21
+ DROP POLICY IF EXISTS "Allow admin users to insert files" ON codette_files;
22
+ DROP POLICY IF EXISTS "Allow authenticated users to insert files" ON codette_files;
23
+ DROP POLICY IF EXISTS "Allow authenticated users to read files" ON codette_files;
24
+
25
+ -- Create new policies with proper checks
26
+ CREATE POLICY "Allow admin users to manage files"
27
+ ON codette_files
28
+ FOR ALL
29
+ TO authenticated
30
+ USING (
31
+ (auth.jwt() ->> 'role')::text = 'admin'
32
+ )
33
+ WITH CHECK (
34
+ (auth.jwt() ->> 'role')::text = 'admin'
35
+ );
36
+
37
+ CREATE POLICY "Allow authenticated users to read files"
38
+ ON codette_files
39
+ FOR SELECT
40
+ TO authenticated
41
+ USING (true);
42
+
43
+ -- Enable RLS if not already enabled
44
+ ALTER TABLE codette_files ENABLE ROW LEVEL SECURITY;
migrations/20250523222514_muddy_desert.sql ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Storage bucket and RLS policies
3
+
4
+ 1. Changes
5
+ - Create storage bucket for Codette files
6
+ - Set up RLS policies for the bucket
7
+
8
+ 2. Security
9
+ - Enable RLS policies for storage bucket
10
+ - Allow authenticated users to read files
11
+ - Allow authenticated users to upload files
12
+ - Allow authenticated users to update files
13
+ - Allow authenticated users to delete files
14
+ */
15
+
16
+ -- Create the storage bucket
17
+ INSERT INTO storage.buckets (id, name)
18
+ VALUES ('codette-files', 'codette-files')
19
+ ON CONFLICT (id) DO NOTHING;
20
+
21
+ -- Drop existing policies if they exist
22
+ DROP POLICY IF EXISTS "Allow authenticated users to read files" ON storage.objects;
23
+ DROP POLICY IF EXISTS "Allow authenticated users to upload files" ON storage.objects;
24
+ DROP POLICY IF EXISTS "Allow authenticated users to update files" ON storage.objects;
25
+ DROP POLICY IF EXISTS "Allow authenticated users to delete files" ON storage.objects;
26
+
27
+ -- Set up RLS policies for the bucket
28
+ CREATE POLICY "Allow authenticated users to read files"
29
+ ON storage.objects FOR SELECT
30
+ TO authenticated
31
+ USING (bucket_id = 'codette-files');
32
+
33
+ CREATE POLICY "Allow authenticated users to upload files"
34
+ ON storage.objects FOR INSERT
35
+ TO authenticated
36
+ WITH CHECK (bucket_id = 'codette-files');
37
+
38
+ CREATE POLICY "Allow authenticated users to update files"
39
+ ON storage.objects FOR UPDATE
40
+ TO authenticated
41
+ USING (bucket_id = 'codette-files')
42
+ WITH CHECK (bucket_id = 'codette-files');
43
+
44
+ CREATE POLICY "Allow authenticated users to delete files"
45
+ ON storage.objects FOR DELETE
46
+ TO authenticated
47
+ USING (bucket_id = 'codette-files');
migrations/20250523222518_bronze_dew.sql ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Update RLS policies for file management
3
+
4
+ 1. Changes
5
+ - Update storage.objects policies
6
+ - Update codette_files table policies
7
+
8
+ 2. Security
9
+ - Allow authenticated users to read files
10
+ - Allow admin users to upload files
11
+ - Allow admin users to insert file records
12
+ */
13
+
14
+ BEGIN;
15
+
16
+ -- Drop existing policies if they exist
17
+ DROP POLICY IF EXISTS "Allow authenticated users to read files" ON storage.objects;
18
+ DROP POLICY IF EXISTS "Allow admin users to upload files" ON storage.objects;
19
+ DROP POLICY IF EXISTS "Allow admin users to insert files" ON public.codette_files;
20
+
21
+ -- Create policy to allow authenticated users to read any file
22
+ CREATE POLICY "Allow authenticated users to read files"
23
+ ON storage.objects FOR SELECT
24
+ TO authenticated
25
+ USING (bucket_id = 'codette-files');
26
+
27
+ -- Create policy to allow only admin users to upload files
28
+ CREATE POLICY "Allow admin users to upload files"
29
+ ON storage.objects FOR INSERT
30
+ TO authenticated
31
+ WITH CHECK (bucket_id = 'codette-files' AND auth.jwt() ->> 'role' = 'admin');
32
+
33
+ -- Update the codette_files table policies
34
+ CREATE POLICY "Allow admin users to insert files"
35
+ ON public.codette_files FOR INSERT
36
+ TO authenticated
37
+ WITH CHECK (auth.jwt() ->> 'role' = 'admin');
38
+
39
+ COMMIT;
migrations/20250523222523_orange_bread.sql ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Update RLS policies for file management
3
+
4
+ 1. Changes
5
+ - Update storage.objects policies
6
+ - Update codette_files table policies
7
+ - Enable RLS on codette_files table
8
+
9
+ 2. Security
10
+ - Allow authenticated users to read files
11
+ - Allow admin users to upload files
12
+ - Allow authenticated users to insert files
13
+ */
14
+
15
+ -- Drop existing policies if they exist
16
+ DROP POLICY IF EXISTS "Allow authenticated users to read files" ON storage.objects;
17
+ DROP POLICY IF EXISTS "Allow admin users to upload files" ON storage.objects;
18
+ DROP POLICY IF EXISTS "Allow authenticated users to read files" ON public.codette_files;
19
+ DROP POLICY IF EXISTS "Allow admin users to insert files" ON public.codette_files;
20
+ DROP POLICY IF EXISTS "Allow authenticated users to insert files" ON public.codette_files;
21
+
22
+ -- Storage Policies
23
+ CREATE POLICY "Allow authenticated users to read files"
24
+ ON storage.objects FOR SELECT
25
+ TO authenticated
26
+ USING (bucket_id = 'codette-files');
27
+
28
+ CREATE POLICY "Allow admin users to upload files"
29
+ ON storage.objects FOR INSERT
30
+ TO authenticated
31
+ WITH CHECK (
32
+ bucket_id = 'codette-files'
33
+ AND (auth.jwt() ->> 'role' = 'admin')
34
+ );
35
+
36
+ -- File Management Policies
37
+ CREATE POLICY "Allow authenticated users to read files"
38
+ ON public.codette_files FOR SELECT
39
+ TO authenticated
40
+ USING (true);
41
+
42
+ CREATE POLICY "Allow admin users to insert files"
43
+ ON public.codette_files FOR INSERT
44
+ TO authenticated
45
+ WITH CHECK (auth.jwt() ->> 'role' = 'admin');
46
+
47
+ CREATE POLICY "Allow authenticated users to insert files"
48
+ ON public.codette_files FOR INSERT
49
+ TO authenticated
50
+ WITH CHECK (true);
51
+
52
+ -- Enable RLS
53
+ ALTER TABLE public.codette_files ENABLE ROW LEVEL SECURITY;
migrations/20250524062844_tender_thunder.sql ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Update codette_files table and policies
3
+
4
+ 1. New Tables
5
+ - Ensures codette_files table exists with proper structure
6
+ - id (uuid, primary key)
7
+ - filename (text)
8
+ - storage_path (text)
9
+ - file_type (text, nullable)
10
+ - uploaded_at (timestamptz)
11
+ - created_at (timestamptz)
12
+
13
+ 2. Security
14
+ - Enables RLS if not already enabled
15
+ - Adds admin-specific policies for file management
16
+ */
17
+
18
+ -- Create table if it doesn't exist
19
+ CREATE TABLE IF NOT EXISTS public.codette_files (
20
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
21
+ filename text NOT NULL,
22
+ storage_path text NOT NULL,
23
+ file_type text,
24
+ uploaded_at timestamptz DEFAULT now(),
25
+ created_at timestamptz DEFAULT now()
26
+ );
27
+
28
+ -- Enable Row Level Security (idempotent operation)
29
+ ALTER TABLE public.codette_files ENABLE ROW LEVEL SECURITY;
30
+
31
+ -- Drop existing policies to avoid conflicts
32
+ DROP POLICY IF EXISTS "Allow authenticated users to read files" ON public.codette_files;
33
+ DROP POLICY IF EXISTS "Allow authenticated users to insert files" ON public.codette_files;
34
+ DROP POLICY IF EXISTS "Allow admin users to manage files" ON public.codette_files;
35
+ DROP POLICY IF EXISTS "Allow admin users to insert files" ON public.codette_files;
36
+
37
+ -- Create new policies
38
+ CREATE POLICY "Allow authenticated users to read files"
39
+ ON public.codette_files
40
+ FOR SELECT
41
+ TO authenticated
42
+ USING (true);
43
+
44
+ CREATE POLICY "Allow authenticated users to insert files"
45
+ ON public.codette_files
46
+ FOR INSERT
47
+ TO authenticated
48
+ WITH CHECK (true);
49
+
50
+ -- Add admin-specific policies
51
+ CREATE POLICY "Allow admin users to manage files"
52
+ ON public.codette_files
53
+ FOR ALL
54
+ TO authenticated
55
+ USING ((auth.jwt() ->> 'role'::text) = 'admin'::text)
56
+ WITH CHECK ((auth.jwt() ->> 'role'::text) = 'admin'::text);
57
+
58
+ CREATE POLICY "Allow admin users to insert files"
59
+ ON public.codette_files
60
+ FOR INSERT
61
+ TO authenticated
62
+ WITH CHECK ((auth.jwt() ->> 'role'::text) = 'admin'::text);
migrations/20250524213845_mellow_recipe.sql ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Add user roles table and admin role policy
3
+
4
+ 1. New Tables
5
+ - `user_roles`
6
+ - `id` (uuid, primary key)
7
+ - `user_id` (uuid, references auth.users)
8
+ - `role` (text)
9
+ - `created_at` (timestamptz)
10
+
11
+ 2. Security
12
+ - Enable RLS on `user_roles` table
13
+ - Add policies for admin role management
14
+ */
15
+
16
+ -- Create user_roles table
17
+ CREATE TABLE IF NOT EXISTS user_roles (
18
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
19
+ user_id uuid REFERENCES auth.users NOT NULL,
20
+ role text NOT NULL,
21
+ created_at timestamptz DEFAULT now()
22
+ );
23
+
24
+ -- Enable RLS
25
+ ALTER TABLE user_roles ENABLE ROW LEVEL SECURITY;
26
+
27
+ -- Policies for user_roles table
28
+ CREATE POLICY "Users can read their own role"
29
+ ON user_roles
30
+ FOR SELECT
31
+ TO authenticated
32
+ USING (auth.uid() = user_id);
33
+
34
+ CREATE POLICY "Only admins can manage roles"
35
+ ON user_roles
36
+ FOR ALL
37
+ TO authenticated
38
+ USING (
39
+ EXISTS (
40
+ SELECT 1 FROM user_roles
41
+ WHERE user_id = auth.uid()
42
+ AND role = 'admin'
43
+ )
44
+ );
migrations/20250524214450_green_poetry.sql ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Authentication and User Roles Setup
3
+
4
+ 1. New Tables
5
+ - `user_roles`
6
+ - `id` (uuid, primary key)
7
+ - `user_id` (uuid, references auth.users)
8
+ - `role` (text)
9
+ - `created_at` (timestamp with time zone)
10
+
11
+ 2. Security
12
+ - Enable RLS on `user_roles` table
13
+ - Add policies for authenticated users to read their own role
14
+ - Add policy for admin users to manage roles
15
+ */
16
+
17
+ -- Create user_roles table
18
+ CREATE TABLE IF NOT EXISTS public.user_roles (
19
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
20
+ user_id uuid REFERENCES auth.users NOT NULL,
21
+ role text NOT NULL,
22
+ created_at timestamptz DEFAULT now()
23
+ );
24
+
25
+ -- Enable RLS
26
+ ALTER TABLE public.user_roles ENABLE ROW LEVEL SECURITY;
27
+
28
+ -- Policies
29
+ CREATE POLICY "Users can read own role"
30
+ ON public.user_roles
31
+ FOR SELECT
32
+ TO authenticated
33
+ USING (auth.uid() = user_id);
34
+
35
+ CREATE POLICY "Admin users can manage roles"
36
+ ON public.user_roles
37
+ FOR ALL
38
+ TO authenticated
39
+ USING ((SELECT role FROM public.user_roles WHERE user_id = auth.uid()) = 'admin')
40
+ WITH CHECK ((SELECT role FROM public.user_roles WHERE user_id = auth.uid()) = 'admin');
41
+
42
+ -- Create admin user if not exists
43
+ DO $$
44
+ BEGIN
45
+ IF NOT EXISTS (
46
+ SELECT 1 FROM auth.users WHERE email = '[email protected]'
47
+ ) THEN
48
+ INSERT INTO auth.users (
49
+ instance_id,
50
+ id,
51
+ aud,
52
+ role,
53
+ email,
54
+ encrypted_password,
55
+ email_confirmed_at,
56
+ created_at,
57
+ updated_at,
58
+ confirmation_token,
59
+ recovery_token
60
+ )
61
+ VALUES (
62
+ '00000000-0000-0000-0000-000000000000',
63
+ gen_random_uuid(),
64
+ 'authenticated',
65
+ 'authenticated',
66
67
+ crypt('admin123', gen_salt('bf')), -- Default password: admin123
68
+ now(),
69
+ now(),
70
+ now(),
71
+ encode(gen_random_bytes(32), 'hex'),
72
+ encode(gen_random_bytes(32), 'hex')
73
+ );
74
+
75
+ -- Add admin role
76
+ INSERT INTO public.user_roles (user_id, role)
77
+ SELECT id, 'admin'
78
+ FROM auth.users
79
+ WHERE email = '[email protected]';
80
+ END IF;
81
+ END $$;
migrations/20250524214705_sunny_sunset.sql ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Storage bucket and policies setup
3
+
4
+ 1. Changes
5
+ - Creates storage bucket for file storage
6
+ - Sets up RLS policies for authenticated users
7
+
8
+ 2. Security
9
+ - Enables secure file access for authenticated users
10
+ - Implements proper access control through RLS policies
11
+ */
12
+
13
+ -- Create the storage bucket if it doesn't exist
14
+ INSERT INTO storage.buckets (id, name)
15
+ VALUES ('codette-files', 'codette-files')
16
+ ON CONFLICT (id) DO NOTHING;
migrations/20250524214708_lively_cell.sql ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # File management policies
3
+
4
+ 1. Changes
5
+ - Creates policies for file management
6
+ - Sets up proper access control for authenticated users and admins
7
+
8
+ 2. Security
9
+ - Implements RLS policies for the codette_files table
10
+ - Ensures proper access control based on user roles
11
+ */
12
+
13
+ -- Enable RLS on codette_files table
14
+ ALTER TABLE public.codette_files ENABLE ROW LEVEL SECURITY;
15
+
16
+ -- Create policies for the codette_files table
17
+ DO $$
18
+ BEGIN
19
+ -- Check if the read policy exists
20
+ IF NOT EXISTS (
21
+ SELECT 1 FROM pg_policies
22
+ WHERE policyname = 'Allow authenticated users to read files'
23
+ AND tablename = 'codette_files'
24
+ ) THEN
25
+ CREATE POLICY "Allow authenticated users to read files"
26
+ ON public.codette_files FOR SELECT
27
+ TO authenticated
28
+ USING (true);
29
+ END IF;
30
+
31
+ -- Check if the admin insert policy exists
32
+ IF NOT EXISTS (
33
+ SELECT 1 FROM pg_policies
34
+ WHERE policyname = 'Allow admin users to insert files'
35
+ AND tablename = 'codette_files'
36
+ ) THEN
37
+ CREATE POLICY "Allow admin users to insert files"
38
+ ON public.codette_files FOR INSERT
39
+ TO authenticated
40
+ WITH CHECK (auth.jwt() ->> 'role' = 'admin');
41
+ END IF;
42
+
43
+ -- Check if the authenticated insert policy exists
44
+ IF NOT EXISTS (
45
+ SELECT 1 FROM pg_policies
46
+ WHERE policyname = 'Allow authenticated users to insert files'
47
+ AND tablename = 'codette_files'
48
+ ) THEN
49
+ CREATE POLICY "Allow authenticated users to insert files"
50
+ ON public.codette_files FOR INSERT
51
+ TO authenticated
52
+ WITH CHECK (true);
53
+ END IF;
54
+ END $$;
migrations/20250524214713_yellow_dawn.sql ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # File management and storage setup
3
+
4
+ 1. Changes
5
+ - Enables RLS on codette_files table
6
+ - Creates necessary policies for file management
7
+
8
+ 2. Security
9
+ - Implements proper access control through RLS
10
+ - Sets up role-based permissions
11
+ */
12
+
13
+ -- Enable RLS on codette_files table if not already enabled
14
+ DO $$
15
+ BEGIN
16
+ IF NOT EXISTS (
17
+ SELECT 1 FROM pg_tables
18
+ WHERE tablename = 'codette_files'
19
+ AND rowsecurity = true
20
+ ) THEN
21
+ ALTER TABLE public.codette_files ENABLE ROW LEVEL SECURITY;
22
+ END IF;
23
+ END $$;
24
+
25
+ -- Create policies for the codette_files table
26
+ DO $$
27
+ BEGIN
28
+ -- Check if the read policy exists
29
+ IF NOT EXISTS (
30
+ SELECT 1 FROM pg_policies
31
+ WHERE policyname = 'Allow authenticated users to read files'
32
+ AND tablename = 'codette_files'
33
+ ) THEN
34
+ CREATE POLICY "Allow authenticated users to read files"
35
+ ON public.codette_files FOR SELECT
36
+ TO authenticated
37
+ USING (true);
38
+ END IF;
39
+
40
+ -- Check if the admin insert policy exists
41
+ IF NOT EXISTS (
42
+ SELECT 1 FROM pg_policies
43
+ WHERE policyname = 'Allow admin users to insert files'
44
+ AND tablename = 'codette_files'
45
+ ) THEN
46
+ CREATE POLICY "Allow admin users to insert files"
47
+ ON public.codette_files FOR INSERT
48
+ TO authenticated
49
+ WITH CHECK (auth.jwt() ->> 'role' = 'admin');
50
+ END IF;
51
+
52
+ -- Check if the authenticated insert policy exists
53
+ IF NOT EXISTS (
54
+ SELECT 1 FROM pg_policies
55
+ WHERE policyname = 'Allow authenticated users to insert files'
56
+ AND tablename = 'codette_files'
57
+ ) THEN
58
+ CREATE POLICY "Allow authenticated users to insert files"
59
+ ON public.codette_files FOR INSERT
60
+ TO authenticated
61
+ WITH CHECK (true);
62
+ END IF;
63
+ END $$;
migrations/20250524215300_flat_firefly.sql ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ # Add get_user_role function
3
+
4
+ 1. New Functions
5
+ - `get_user_role`: Returns the role of the authenticated user
6
+
7
+ 2. Security
8
+ - Function is only accessible to authenticated users
9
+ - Returns the user's role from user_roles table
10
+ */
11
+
12
+ -- Create function to get user role
13
+ CREATE OR REPLACE FUNCTION public.get_user_role()
14
+ RETURNS TABLE (role text)
15
+ LANGUAGE plpgsql
16
+ SECURITY DEFINER
17
+ SET search_path = public
18
+ AS $$
19
+ BEGIN
20
+ RETURN QUERY
21
+ SELECT ur.role
22
+ FROM public.user_roles ur
23
+ WHERE ur.user_id = auth.uid()
24
+ LIMIT 1;
25
+ END;
26
+ $$;