File size: 2,248 Bytes
8395368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76

import { PrismaClient } from '@prisma/client';
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

dotenv.config({ path: path.join(__dirname, '.env') });

const prisma = new PrismaClient();

async function main() {
  try {
    const output = [];
    const log = (...args) => {
        console.log(...args);
        output.push(args.map(a => typeof a === 'object' ? JSON.stringify(a) : String(a)).join(' '));
    };

    log('--- DB DIAGNOSTIC START ---');
    log('DATABASE_URL:', process.env.DATABASE_URL ? process.env.DATABASE_URL.substring(0, 20) + '...' : 'UNDEFINED');
    
    // 1. Check if the table exists
    const tableExists = await prisma.$queryRawUnsafe(`
      SELECT EXISTS (
        SELECT FROM information_schema.tables 
        WHERE table_schema = 'public' 
        AND table_name = 'Product'
      );
    `);
    log('Table "Product" exists:', tableExists);

    // 2. Check if the column exists
    const columns = await prisma.$queryRawUnsafe(`
      SELECT column_name, data_type 
      FROM information_schema.columns 
      WHERE table_name = 'Product';
    `);
    
    log('Columns in "Product" table:');
    const columnNames = Array.isArray(columns) ? columns.map(c => c.column_name) : [];
    log(columnNames.join(', '));

    const hasScrapedReviews = columnNames.includes('scrapedReviews');
    log('Has "scrapedReviews" column:', hasScrapedReviews);

    if (!hasScrapedReviews) {
        log('ATTEMPTING TO ADD COLUMN...');
        try {
            await prisma.$executeRawUnsafe(`ALTER TABLE "Product" ADD COLUMN IF NOT EXISTS "scrapedReviews" JSONB;`);
            log('Column added successfully.');
        } catch (err) {
            log('Failed to add column:', err.message);
        }
    } else {
        log('Column already exists. No action needed.');
    }

    log('--- DB DIAGNOSTIC END ---');
    
    // Write to file
    const fs = await import('fs');
    fs.writeFileSync(path.join(__dirname, 'db_check_result.txt'), output.join('\n'));


  } catch (e) {
    console.error('Diagnostic Error:', e);
  } finally {
    await prisma.$disconnect();
  }
}

main();