File size: 719 Bytes
f0c0e92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import psycopg2

SQL_FILE = os.path.join(os.path.dirname(__file__), 'sql', 'create_tables.sql')
DB_URL = os.getenv('DATABASE_URL') or 'postgresql://postgres:Rana1515@localhost:5432/chess'

print('Using DB URL:', DB_URL)

if not os.path.exists(SQL_FILE):
    print('SQL file not found:', SQL_FILE)
    sys.exit(1)

with open(SQL_FILE, 'r', encoding='utf-8') as fh:
    sql = fh.read()

try:
    conn = psycopg2.connect(DB_URL)
    conn.autocommit = True
    with conn.cursor() as cur:
        cur.execute(sql)
    print('SQL executed successfully')
except Exception as e:
    print('Error executing SQL:', e)
    sys.exit(2)
finally:
    try:
        conn.close()
    except Exception:
        pass