AI Resource Pro / website / scripts / apply-fix-recursive-policies.ts
| 1 | import { createClient } from '@supabase/supabase-js'; |
| 2 | import { readFileSync, existsSync } from 'fs'; |
| 3 | import * as path from 'path'; |
| 4 | import * as dotenv from 'dotenv'; |
| 5 | |
| 6 | // Load environment variables from .env.local |
| 7 | const envLocalPath = path.resolve(process.cwd(), '.env.local'); |
| 8 | if (existsSync(envLocalPath)) { |
| 9 | console.log(`Loading environment from: ${envLocalPath}`); |
| 10 | dotenv.config({ path: envLocalPath }); |
| 11 | } else { |
| 12 | console.log(`No .env.local file found at: ${envLocalPath}`); |
| 13 | dotenv.config(); // Try loading from default .env |
| 14 | } |
| 15 | |
| 16 | // Get Supabase credentials from environment variables |
| 17 | const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || ''; |
| 18 | const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY || ''; |
| 19 | |
| 20 | if (!supabaseUrl || !supabaseServiceKey) { |
| 21 | console.error('Missing Supabase environment variables'); |
| 22 | process.exit(1); |
| 23 | } |
| 24 | |
| 25 | // Create a service role client |
| 26 | const supabase = createClient(supabaseUrl, supabaseServiceKey, { |
| 27 | auth: { |
| 28 | persistSession: false, |
| 29 | }, |
| 30 | }); |
| 31 | |
| 32 | async function main() { |
| 33 | console.log('Applying fix for recursive policies...'); |
| 34 | |
| 35 | try { |
| 36 | // Read the SQL file |
| 37 | const sqlPath = path.join(process.cwd(), 'scripts', 'fix-recursive-policies.sql'); |
| 38 | const sqlContent = readFileSync(sqlPath, 'utf8'); |
| 39 | |
| 40 | // Execute the SQL |
| 41 | const { data, error } = await supabase.rpc('exec_sql', { |
| 42 | sql: sqlContent |
| 43 | }); |
| 44 | |
| 45 | if (error) { |
| 46 | throw error; |
| 47 | } |
| 48 | |
| 49 | console.log('Successfully applied fix for recursive policies:', data); |
| 50 | console.log('Please restart your application for the changes to take effect.'); |
| 51 | } catch (error) { |
| 52 | console.error('Error applying fix:', error); |
| 53 | process.exit(1); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | main(); |