File size: 1,978 Bytes
beb354f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { createClient } = require('@supabase/supabase-js');
const fs = require('fs');
const path = require('path');

const supabaseUrl = process.env.SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.SUPABASE_KEY;

if (!supabaseUrl || !supabaseKey) {
  console.error('Error: SUPABASE_URL and SUPABASE_KEY environment variables are required.');
  process.exit(1);
}

const supabase = createClient(supabaseUrl, supabaseKey);

async function runMigration(migrationFile) {
  const filePath = path.join(__dirname, 'migrations', migrationFile);

  if (!fs.existsSync(filePath)) {
    console.error(`Error: Migration file not found: ${filePath}`);
    return false;
  }

  const sql = fs.readFileSync(filePath, 'utf8');

  console.log(`Running migration: ${migrationFile}`);

  // Execute SQL via Supabase RPC or direct query
  const { data, error } = await supabase.rpc('exec_sql', { sql });

  if (error) {
    // Some migrations may need to be run differently
    console.log(`Note: RPC exec_sql not available, trying direct execution...`);
    console.log(`SQL to execute:\n${sql}`);
    return false;
  }

  console.log(`Migration ${migrationFile} completed successfully.`);
  return true;
}

async function main() {
  const migrations = process.argv.slice(2);

  if (migrations.length === 0) {
    console.log('Available migrations:');
    const files = fs.readdirSync(path.join(__dirname, 'migrations'));
    files.filter(f => f.endsWith('.sql')).forEach(f => console.log(`  - ${f}`));
    console.log('\nUsage: node run-migration.js <migration_file.sql> [migration_file2.sql ...]');
    process.exit(0);
  }

  for (const migration of migrations) {
    try {
      const success = await runMigration(migration);
      if (!success) {
        console.log(`Skipping migration: ${migration} (manual execution required)`);
      }
    } catch (err) {
      console.error(`Error running migration ${migration}:`, err.message);
    }
  }
}

main();