AI Resource Pro / website / scripts / create-admin-rpc.ts
| 1 | import { createClient } from '@supabase/supabase-js'; |
| 2 | import * as fs from 'fs'; |
| 3 | import * as path from 'path'; |
| 4 | import * as dotenv from 'dotenv'; |
| 5 | import { fileURLToPath } from 'url'; |
| 6 | |
| 7 | // Get the equivalent of __dirname in ESM |
| 8 | const __filename = fileURLToPath(import.meta.url); |
| 9 | const __dirname = path.dirname(__filename); |
| 10 | |
| 11 | // Load environment variables from .env.local in project root, not scripts directory |
| 12 | const envLocalPath = path.resolve(process.cwd(), '..', '.env.local'); |
| 13 | if (fs.existsSync(envLocalPath)) { |
| 14 | console.log(`Loading environment from: ${envLocalPath}`); |
| 15 | dotenv.config({ path: envLocalPath }); |
| 16 | } else { |
| 17 | console.log(`No .env.local file found at: ${envLocalPath}`); |
| 18 | // Try loading from root directory's .env |
| 19 | const envPath = path.resolve(process.cwd(), '..', '.env'); |
| 20 | if (fs.existsSync(envPath)) { |
| 21 | console.log(`Loading environment from: ${envPath}`); |
| 22 | dotenv.config({ path: envPath }); |
| 23 | } else { |
| 24 | dotenv.config(); // Try loading from default .env |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || ''; |
| 29 | const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY || ''; |
| 30 | |
| 31 | if (!supabaseUrl || !supabaseServiceKey) { |
| 32 | console.error('Missing Supabase environment variables. Please check your .env file.'); |
| 33 | console.error(`NEXT_PUBLIC_SUPABASE_URL: ${supabaseUrl ? '[SET]' : '[MISSING]'}`); |
| 34 | console.error(`SUPABASE_SERVICE_ROLE_KEY: ${supabaseServiceKey ? '[SET]' : '[MISSING]'}`); |
| 35 | process.exit(1); |
| 36 | } |
| 37 | |
| 38 | async function createAdminRpcFunction() { |
| 39 | try { |
| 40 | // Create a Supabase client with the service role key |
| 41 | const supabase = createClient(supabaseUrl, supabaseServiceKey, { |
| 42 | auth: { |
| 43 | persistSession: false, |
| 44 | }, |
| 45 | }); |
| 46 | |
| 47 | console.log('Creating RPC function for admin check...'); |
| 48 | |
| 49 | // Read the SQL file content |
| 50 | const sqlFile = path.join(__dirname, 'create-admin-rpc.sql'); |
| 51 | const sqlContent = fs.readFileSync(sqlFile, 'utf8'); |
| 52 | |
| 53 | // Execute the SQL to create the function |
| 54 | const { error } = await supabase.rpc('exec_sql', { sql: sqlContent }); |
| 55 | |
| 56 | if (error) { |
| 57 | // If exec_sql RPC doesn't exist, try using raw SQL query instead |
| 58 | console.warn('exec_sql RPC not available, attempting direct SQL API...'); |
| 59 | console.error('Error with RPC method:', error.message); |
| 60 | |
| 61 | // Try the REST API for executing SQL directly |
| 62 | try { |
| 63 | const response = await fetch(`${supabaseUrl}/rest/v1/`, { |
| 64 | method: 'POST', |
| 65 | headers: { |
| 66 | 'Content-Type': 'application/json', |
| 67 | 'apikey': supabaseServiceKey, |
| 68 | 'Authorization': `Bearer ${supabaseServiceKey}`, |
| 69 | 'Prefer': 'return=minimal' |
| 70 | } as HeadersInit, |
| 71 | body: JSON.stringify({ |
| 72 | query: sqlContent |
| 73 | }) |
| 74 | }); |
| 75 | |
| 76 | if (!response.ok) { |
| 77 | const errorData = await response.json(); |
| 78 | throw new Error(`Failed to execute SQL directly: ${JSON.stringify(errorData)}`); |
| 79 | } |
| 80 | |
| 81 | console.log('Successfully created RPC function via direct SQL API'); |
| 82 | } catch (fetchError) { |
| 83 | console.error('Direct SQL API failed:', fetchError); |
| 84 | |
| 85 | // As a last resort, output the SQL for manual execution |
| 86 | console.log('\n--- SQL for manual execution in Supabase SQL Editor ---'); |
| 87 | console.log(sqlContent); |
| 88 | console.log('------------------------------------------------------\n'); |
| 89 | console.log('Please copy the SQL above and execute it manually in your Supabase SQL Editor.'); |
| 90 | } |
| 91 | } else { |
| 92 | console.log('Successfully created RPC function'); |
| 93 | } |
| 94 | } catch (error) { |
| 95 | console.error('Error creating admin RPC function:', error); |
| 96 | process.exit(1); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Run the script |
| 101 | createAdminRpcFunction(); |