auto-claude: subtask-1-2 - Apply migration to Supabase database
Created migration application scripts and comprehensive documentation: - scripts/apply-migration.js - Automated migration (requires service role key) - scripts/verify-migration.js - Table verification script - scripts/test-env.js - Environment diagnostics - supabase/APPLY_MIGRATION.md - Comprehensive migration guide - supabase/MIGRATION_INSTRUCTIONS.md - Quick reference Manual application via Supabase dashboard is recommended approach. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Script to apply Supabase migration
|
||||
*
|
||||
* This script applies the rate_limits table migration to Supabase.
|
||||
*
|
||||
* IMPORTANT: This script requires SUPABASE_SERVICE_ROLE_KEY to be set
|
||||
* in your .env.local file to execute DDL statements.
|
||||
*
|
||||
* If you don't have the service role key, you can apply the migration manually:
|
||||
* 1. Go to https://app.supabase.com/project/mxadgucxhmstlzsbgmoz/sql
|
||||
* 2. Copy the contents of supabase/migrations/20260125_create_rate_limits_table.sql
|
||||
* 3. Paste into the SQL editor
|
||||
* 4. Click "Run" to execute
|
||||
* 5. Verify the table was created by clicking "Table Editor" and checking for "rate_limits"
|
||||
*/
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { createClient } = require('@supabase/supabase-js');
|
||||
|
||||
// Load .env.local manually
|
||||
const envPath = path.join(__dirname, '..', '.env.local');
|
||||
if (fs.existsSync(envPath)) {
|
||||
const envContent = fs.readFileSync(envPath, 'utf8');
|
||||
envContent.split('\n').forEach(line => {
|
||||
const trimmed = line.trim();
|
||||
const match = trimmed.match(/^([^=:#]+)=(.*)$/);
|
||||
if (match) {
|
||||
const key = match[1].trim();
|
||||
const value = match[2].trim();
|
||||
if (!process.env[key]) {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function applyMigration() {
|
||||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
||||
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
|
||||
|
||||
if (!supabaseUrl) {
|
||||
console.error('❌ Error: NEXT_PUBLIC_SUPABASE_URL not found in .env.local');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (!serviceRoleKey) {
|
||||
console.error('❌ Error: SUPABASE_SERVICE_ROLE_KEY not found in .env.local');
|
||||
console.error('\n📝 Manual Migration Instructions:');
|
||||
console.error(' 1. Go to https://app.supabase.com/project/mxadgucxhmstlzsbgmoz/sql');
|
||||
console.error(' 2. Open: supabase/migrations/20260125_create_rate_limits_table.sql');
|
||||
console.error(' 3. Copy the SQL content and paste into the SQL editor');
|
||||
console.error(' 4. Click "Run" to execute the migration');
|
||||
console.error(' 5. Verify the "rate_limits" table appears in Table Editor\n');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('🚀 Applying migration to Supabase...\n');
|
||||
|
||||
// Create Supabase client with service role key
|
||||
const supabase = createClient(supabaseUrl, serviceRoleKey, {
|
||||
auth: {
|
||||
autoRefreshToken: false,
|
||||
persistSession: false
|
||||
}
|
||||
});
|
||||
|
||||
// Read migration file
|
||||
const migrationPath = path.join(__dirname, '..', 'supabase', 'migrations', '20260125_create_rate_limits_table.sql');
|
||||
const migrationSQL = fs.readFileSync(migrationPath, 'utf8');
|
||||
|
||||
console.log('📄 Migration file loaded:', migrationPath);
|
||||
|
||||
try {
|
||||
// Execute the migration
|
||||
const { data, error } = await supabase.rpc('exec_sql', { sql: migrationSQL });
|
||||
|
||||
if (error) {
|
||||
// If exec_sql RPC doesn't exist, try direct SQL execution
|
||||
console.log('⚠️ exec_sql RPC not found, attempting direct SQL execution...\n');
|
||||
|
||||
// Split the SQL into individual statements
|
||||
const statements = migrationSQL
|
||||
.split(';')
|
||||
.map(s => s.trim())
|
||||
.filter(s => s.length > 0 && !s.startsWith('--'));
|
||||
|
||||
for (const statement of statements) {
|
||||
const { error: execError } = await supabase.from('_sql').select().sql(statement);
|
||||
if (execError) {
|
||||
throw execError;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('✅ Migration applied successfully!\n');
|
||||
|
||||
// Verify the table was created
|
||||
console.log('🔍 Verifying table creation...');
|
||||
const { data: tableData, error: tableError } = await supabase
|
||||
.from('rate_limits')
|
||||
.select('*')
|
||||
.limit(1);
|
||||
|
||||
if (tableError && tableError.code !== 'PGRST116') { // PGRST116 = no rows found, which is OK
|
||||
throw tableError;
|
||||
}
|
||||
|
||||
console.log('✅ Table "rate_limits" verified successfully!\n');
|
||||
console.log('📊 Migration complete. You can view the table at:');
|
||||
console.log(` https://app.supabase.com/project/mxadgucxhmstlzsbgmoz/editor/rate_limits\n`);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error applying migration:', error.message);
|
||||
console.error('\n📝 Please apply the migration manually:');
|
||||
console.error(' 1. Go to https://app.supabase.com/project/mxadgucxhmstlzsbgmoz/sql');
|
||||
console.error(' 2. Copy the contents of supabase/migrations/20260125_create_rate_limits_table.sql');
|
||||
console.error(' 3. Paste into the SQL editor and click "Run"\n');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
applyMigration();
|
||||
Reference in New Issue
Block a user