File size: 1,383 Bytes
60470bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
from sqlalchemy import create_engine, text
from dotenv import load_dotenv

# Load env from backend
load_dotenv("backend/.env")
db_url = os.getenv("DATABASE_URL")

if not db_url:
    print("[ERROR] DATABASE_URL not found in backend/.env")
    exit(1)

# Handle potential psql prefix
if db_url.startswith("postgres://"):
    db_url = db_url.replace("postgres://", "postgresql://", 1)

engine = create_engine(db_url)

def check_db():
    with engine.connect() as conn:
        # Check Products
        p_res = conn.execute(text("SELECT count(*) FROM products"))
        p_count = p_res.scalar()
        
        # Check Categories
        c_res = conn.execute(text("SELECT count(*) FROM categories"))
        c_count = c_res.scalar()
        
        # Check Images
        i_res = conn.execute(text("SELECT count(*) FROM product_images"))
        i_count = i_res.scalar()
        
        # Check Sample Product
        sample_res = conn.execute(text("SELECT name_en, price FROM products LIMIT 5"))
        samples = sample_res.fetchall()
        
        print(f"--- Database Report ---")
        print(f"Products: {p_count}")
        print(f"Categories: {c_count}")
        print(f"Product Images: {i_count}")
        print(f"Sample Products:")
        for s in samples:
            print(f"   - {s[0]} ({s[1]} SAR)")

if __name__ == "__main__":
    check_db()