-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_settings_complete.sql
More file actions
70 lines (58 loc) · 2.31 KB
/
fix_settings_complete.sql
File metadata and controls
70 lines (58 loc) · 2.31 KB
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
67
68
69
70
-- ======================================
-- COMPLETE FIX for site_settings RLS
-- Run this ENTIRE script in Supabase SQL Editor
-- ======================================
-- Step 1: Check if table exists, if not create it
CREATE TABLE IF NOT EXISTS site_settings (
id TEXT PRIMARY KEY DEFAULT 'main',
settings JSONB NOT NULL DEFAULT '{}',
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Step 2: Drop ALL existing policies (ignore errors if they don't exist)
DO $$
BEGIN
DROP POLICY IF EXISTS "Anyone can read settings" ON site_settings;
DROP POLICY IF EXISTS "Authenticated can update settings" ON site_settings;
DROP POLICY IF EXISTS "Authenticated can insert settings" ON site_settings;
DROP POLICY IF EXISTS "public_read_settings" ON site_settings;
DROP POLICY IF EXISTS "authenticated_insert_settings" ON site_settings;
DROP POLICY IF EXISTS "authenticated_update_settings" ON site_settings;
DROP POLICY IF EXISTS "allow_all_for_authenticated" ON site_settings;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'Some policies did not exist, continuing...';
END $$;
-- Step 3: Completely disable RLS temporarily
ALTER TABLE site_settings DISABLE ROW LEVEL SECURITY;
-- Step 4: Revoke and re-grant permissions
REVOKE ALL ON site_settings FROM PUBLIC;
REVOKE ALL ON site_settings FROM anon;
REVOKE ALL ON site_settings FROM authenticated;
GRANT SELECT ON site_settings TO anon;
GRANT ALL ON site_settings TO authenticated;
-- Step 5: Re-enable RLS
ALTER TABLE site_settings ENABLE ROW LEVEL SECURITY;
-- Step 6: Create simple, permissive policies
-- Allow ANYONE to read (for public site)
CREATE POLICY "allow_public_read" ON site_settings
FOR SELECT
TO PUBLIC
USING (true);
-- Allow authenticated users to do ANYTHING
CREATE POLICY "allow_authenticated_all" ON site_settings
FOR ALL
TO authenticated
USING (true)
WITH CHECK (true);
-- Step 7: Insert default row if not exists
INSERT INTO site_settings (id, settings, updated_at)
VALUES ('main', '{}'::jsonb, NOW())
ON CONFLICT (id) DO NOTHING;
-- Step 8: Verify setup
SELECT 'Table and policies created successfully!' as status;
-- Show existing policies
SELECT policyname, cmd, permissive, roles
FROM pg_policies
WHERE tablename = 'site_settings';
-- Show table data
SELECT * FROM site_settings;