File size: 1,078 Bytes
1804b24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import postgres from 'postgres';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const schemaPath = path.join(__dirname, 'postgres_schema.sql');
const connectionString = process.env.DATABASE_URL;

if (!connectionString) {
  console.error("DATABASE_URL is not set");
  process.exit(1);
}

const sql = postgres(connectionString);

async function applySchema() {
  try {
    console.log("Reading schema file...");
    const schemaSql = fs.readFileSync(schemaPath, 'utf8');
    
    console.log("Applying schema to Supabase...");
    // Split the SQL by semicolon to execute commands (basic splitting, might be issues with quotes but schema is simple)
    // Actually, postgres-js can execute multiple statements in one query if it's just a string.
    await sql.unsafe(schemaSql);
    
    console.log("Schema applied successfully!");
  } catch (err) {
    console.error("Error applying schema:", err);
    process.exit(1);
  } finally {
    await sql.end();
  }
}

applySchema();