AI Resource Pro / website / scripts / apply-image-url-migration.js
| 1 | import { createClient } from '@supabase/supabase-js'; |
| 2 | import fs from 'fs'; |
| 3 | import path from 'path'; |
| 4 | import * as dotenv from 'dotenv'; |
| 5 | import { fileURLToPath } from 'url'; |
| 6 | |
| 7 | // Load environment variables from .env.local |
| 8 | const __filename = fileURLToPath(import.meta.url); |
| 9 | const __dirname = path.dirname(__filename); |
| 10 | const envLocalPath = path.resolve(process.cwd(), '.env.local'); |
| 11 | |
| 12 | if (fs.existsSync(envLocalPath)) { |
| 13 | console.log(`Loading environment from: ${envLocalPath}`); |
| 14 | dotenv.config({ path: envLocalPath }); |
| 15 | } else { |
| 16 | console.log(`No .env.local file found at: ${envLocalPath}`); |
| 17 | dotenv.config(); // Try loading from default .env |
| 18 | } |
| 19 | |
| 20 | async function applyMigration() { |
| 21 | // Validate environment variables |
| 22 | const requiredEnvs = ['NEXT_PUBLIC_SUPABASE_URL', 'SUPABASE_SERVICE_ROLE_KEY']; |
| 23 | for (const env of requiredEnvs) { |
| 24 | if (!process.env[env]) { |
| 25 | console.error(`Missing required environment variable: ${env}`); |
| 26 | process.exit(1); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Create Supabase client with service role key |
| 31 | const supabase = createClient( |
| 32 | process.env.NEXT_PUBLIC_SUPABASE_URL, |
| 33 | process.env.SUPABASE_SERVICE_ROLE_KEY, |
| 34 | { |
| 35 | auth: { |
| 36 | autoRefreshToken: false, |
| 37 | persistSession: false |
| 38 | } |
| 39 | } |
| 40 | ); |
| 41 | |
| 42 | console.log('Creating Supabase client with service role key'); |
| 43 | |
| 44 | // Read migration SQL |
| 45 | const migrationFilePath = path.join(__dirname, '../supabase/migrations/add_image_url_to_tools.sql'); |
| 46 | console.log(`Reading migration file from: ${migrationFilePath}`); |
| 47 | |
| 48 | if (!fs.existsSync(migrationFilePath)) { |
| 49 | console.error(`Migration file not found at: ${migrationFilePath}`); |
| 50 | process.exit(1); |
| 51 | } |
| 52 | |
| 53 | const migrationSQL = fs.readFileSync(migrationFilePath, 'utf8'); |
| 54 | |
| 55 | // Apply migration |
| 56 | console.log('Applying migration...'); |
| 57 | try { |
| 58 | const { data, error } = await supabase.rpc('pg_catalog.pg_reload_conf'); |
| 59 | |
| 60 | if (error) { |
| 61 | console.warn('Could not reload configuration, continuing anyway:', error); |
| 62 | } |
| 63 | |
| 64 | const { data: migrationData, error: migrationError } = await supabase.rpc('exec_sql', { |
| 65 | sql: migrationSQL |
| 66 | }); |
| 67 | |
| 68 | if (migrationError) { |
| 69 | console.error('Error applying migration:', migrationError); |
| 70 | process.exit(1); |
| 71 | } |
| 72 | |
| 73 | console.log('Migration applied successfully:', migrationData); |
| 74 | |
| 75 | // Refresh schema cache |
| 76 | console.log('Refreshing schema cache...'); |
| 77 | const { error: reloadError } = await supabase.rpc('pg_catalog.pg_reload_conf'); |
| 78 | |
| 79 | if (reloadError) { |
| 80 | console.warn('Could not reload configuration after migration:', reloadError); |
| 81 | } else { |
| 82 | console.log('Schema cache refreshed successfully'); |
| 83 | } |
| 84 | } catch (error) { |
| 85 | console.error('Unexpected error applying migration:', error); |
| 86 | process.exit(1); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | applyMigration() |
| 91 | .then(() => { |
| 92 | console.log('Migration process completed'); |
| 93 | process.exit(0); |
| 94 | }) |
| 95 | .catch((error) => { |
| 96 | console.error('Unhandled error in migration process:', error); |
| 97 | process.exit(1); |
| 98 | }); |