-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_database.py
More file actions
71 lines (61 loc) · 2.02 KB
/
Copy pathsetup_database.py
File metadata and controls
71 lines (61 loc) · 2.02 KB
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
import os
import sys
from sqlalchemy import create_engine, text
def main():
"""Initialize the database schema based on the SQL script."""
# Check if database URL is available
db_url = os.environ.get('DATABASE_URL')
if not db_url:
print("ERROR: DATABASE_URL environment variable not set")
sys.exit(1)
print("Connecting to database...")
try:
engine = create_engine(db_url)
print("Successfully connected to database")
except Exception as e:
print(f"ERROR: Failed to connect to database: {e}")
sys.exit(1)
# Check if schema file exists
schema_path = "utils/schema.sql"
if not os.path.exists(schema_path):
print(f"ERROR: Schema file not found at {schema_path}")
sys.exit(1)
# Read schema SQL
print(f"Reading schema from {schema_path}...")
try:
with open(schema_path, 'r') as f:
schema_sql = f.read()
except Exception as e:
print(f"ERROR: Failed to read schema file: {e}")
sys.exit(1)
# Create tables
print("Creating database tables...")
try:
with engine.begin() as conn:
conn.execute(text(schema_sql))
print("Successfully created database tables")
except Exception as e:
print(f"ERROR: Failed to create tables: {e}")
sys.exit(1)
# Check if tables were created
check_sql = """
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;
"""
try:
with engine.connect() as conn:
result = conn.execute(text(check_sql))
tables = [row[0] for row in result]
if tables:
print("Available tables:")
for table in tables:
print(f" - {table}")
else:
print("No tables were created")
except Exception as e:
print(f"ERROR: Failed to verify table creation: {e}")
print("\nDatabase setup complete!")
if __name__ == "__main__":
main()