summaryrefslogtreecommitdiff
path: root/scripts/create-payload-schema.mts
blob: 35c13a6036129e790e496b1573839fafb213e990 (plain)
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
import { config } from 'node:process';
import pg from 'pg';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

// 读取 .env 文件
const envPath = join(process.cwd(), '.env');
const envContent = readFileSync(envPath, 'utf-8');
const envLines = envContent.split('\n');
for (const line of envLines) {
  const match = line.match(/^([^=]+)=(.*)$/);
  if (match) {
    const key = match[1]?.trim();
    let value = match[2]?.trim() || '';
    // 移除引号
    if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
      value = value.slice(1, -1);
    }
    if (key) {
      process.env[key] = value;
    }
  }
}

const { Pool } = pg;

async function createPayloadSchema() {
  const pool = new Pool({
    connectionString: process.env.DATABASE_URL,
  });

  try {
    await pool.query('CREATE SCHEMA IF NOT EXISTS payload');
    console.log('✅ Payload schema created successfully');
  } catch (error) {
    console.error('❌ Error creating schema:', error);
    process.exit(1);
  } finally {
    await pool.end();
  }
}

createPayloadSchema();