Repository navigation
Expand file tree
/
Copy pathTARGETED_APP_FIX.sql
More file actions
187 lines (159 loc) · 5.49 KB
/
Copy pathTARGETED_APP_FIX.sql
File metadata and controls
187 lines (159 loc) · 5.49 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
-- =====================================================
-- TARGETED APPLICATION FIX
-- =====================================================
-- Since your database has 57 good products, this fixes the app connection issues
-- 1. FIX RLS POLICIES FOR APP ACCESS
-- =====================================================
SELECT 'Fixing RLS policies for application access...' as status;
-- Drop all restrictive policies
DROP POLICY IF EXISTS "Enable read access for all users" ON lats_products;
DROP POLICY IF EXISTS "Enable insert for authenticated users only" ON lats_products;
DROP POLICY IF EXISTS "Enable update for authenticated users only" ON lats_products;
DROP POLICY IF EXISTS "Enable delete for authenticated users only" ON lats_products;
DROP POLICY IF EXISTS "Allow authenticated users to manage products" ON lats_products;
DROP POLICY IF EXISTS "Allow all operations on products" ON lats_products;
-- Create simple, permissive policies for app access
CREATE POLICY "App can access all products" ON lats_products
FOR ALL USING (true);
CREATE POLICY "App can access all variants" ON lats_product_variants
FOR ALL USING (true);
CREATE POLICY "App can access all categories" ON lats_categories
FOR ALL USING (true);
CREATE POLICY "App can access all suppliers" ON lats_suppliers
FOR ALL USING (true);
-- 2. GRANT PERMISSIONS FOR APP ACCESS
-- =====================================================
SELECT 'Granting permissions for application access...' as status;
-- Grant all permissions to authenticated users
GRANT ALL ON lats_products TO authenticated;
GRANT ALL ON lats_product_variants TO authenticated;
GRANT ALL ON lats_categories TO authenticated;
GRANT ALL ON lats_suppliers TO authenticated;
-- Grant all permissions to anon users (for development)
GRANT ALL ON lats_products TO anon;
GRANT ALL ON lats_product_variants TO anon;
GRANT ALL ON lats_categories TO anon;
GRANT ALL ON lats_suppliers TO anon;
-- 3. VERIFY APP CAN ACCESS DATA
-- =====================================================
SELECT 'Verifying application can access data...' as status;
-- Test basic queries that the app uses
SELECT
'Products accessible' as test,
COUNT(*) as count
FROM lats_products
WHERE is_active = true;
SELECT
'Variants accessible' as test,
COUNT(*) as count
FROM lats_product_variants;
SELECT
'Categories accessible' as test,
COUNT(*) as count
FROM lats_categories;
SELECT
'Suppliers accessible' as test,
COUNT(*) as count
FROM lats_suppliers;
-- 4. TEST COMPLEX QUERIES (like the app uses)
-- =====================================================
SELECT 'Testing complex queries like the application...' as status;
-- Test the type of query your app uses
SELECT
p.id,
p.name,
p.sku,
p.is_active,
p.total_quantity,
p.total_value,
c.name as category_name,
s.name as supplier_name,
COUNT(pv.id) as variant_count
FROM lats_products p
LEFT JOIN lats_categories c ON p.category_id = c.id
LEFT JOIN lats_suppliers s ON p.supplier_id = s.id
LEFT JOIN lats_product_variants pv ON p.id = pv.product_id
WHERE p.is_active = true
GROUP BY p.id, p.name, p.sku, p.is_active, p.total_quantity, p.total_value, c.name, s.name
ORDER BY p.created_at DESC
LIMIT 10;
-- 5. CHECK FOR ANY REMAINING ISSUES
-- =====================================================
SELECT 'Checking for remaining issues...' as status;
-- Check for products without variants (app needs these)
SELECT
'Products without variants' as issue,
COUNT(*) as count
FROM lats_products p
LEFT JOIN lats_product_variants pv ON p.id = pv.product_id
WHERE pv.id IS NULL AND p.is_active = true;
-- Check for missing required fields
SELECT
'Products missing names' as issue,
COUNT(*) as count
FROM lats_products
WHERE name IS NULL OR name = '';
SELECT
'Products missing SKUs' as issue,
COUNT(*) as count
FROM lats_products
WHERE sku IS NULL OR sku = '';
-- 6. CREATE SIMPLE TEST VIEW FOR APP
-- =====================================================
SELECT 'Creating test view for application...' as status;
-- Create a simple view that the app can use
CREATE OR REPLACE VIEW app_products_view AS
SELECT
p.id,
p.name,
p.sku,
p.description,
p.is_active,
p.total_quantity,
p.total_value,
p.cost_price,
p.selling_price,
p.stock_quantity,
p.min_stock_level,
p.category_id,
p.supplier_id,
c.name as category_name,
s.name as supplier_name,
p.created_at,
p.updated_at
FROM lats_products p
LEFT JOIN lats_categories c ON p.category_id = c.id
LEFT JOIN lats_suppliers s ON p.supplier_id = s.id
WHERE p.is_active = true;
-- Grant access to the view
GRANT SELECT ON app_products_view TO authenticated;
GRANT SELECT ON app_products_view TO anon;
-- 7. FINAL VERIFICATION
-- =====================================================
SELECT 'Final verification...' as status;
-- Test the view
SELECT
'View accessible' as test,
COUNT(*) as count
FROM app_products_view;
-- Show sample data from the view
SELECT
id,
name,
sku,
category_name,
supplier_name,
total_quantity,
total_value
FROM app_products_view
ORDER BY created_at DESC
LIMIT 5;
-- 8. SUMMARY
-- =====================================================
SELECT '=== TARGETED FIX COMPLETE ===' as status;
SELECT
'Your database has good data (57 products)' as message,
'RLS policies have been fixed for app access' as fix1,
'Permissions have been granted' as fix2,
'Test view created for app use' as fix3,
'Please refresh your application now' as next_step;