-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathensure_db.py
More file actions
28 lines (24 loc) · 982 Bytes
/
Copy pathensure_db.py
File metadata and controls
28 lines (24 loc) · 982 Bytes
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
import sys
import os
from sqlalchemy import create_engine
from sqlalchemy_utils import database_exists, create_database
from urllib.parse import quote_plus
# Add backend to path
sys.path.append(os.path.join(os.getcwd(), "backend"))
from app.core.config import settings
def ensure_db_exists():
url = settings.assemble_db_url()
print(f"Checking database at {settings.POSTGRES_SERVER}:{settings.POSTGRES_PORT}...")
try:
if not database_exists(url):
print(f"Database {settings.POSTGRES_DB} does not exist. Attempting to create it...")
create_database(url)
print("Database created successfully.")
else:
print(f"Database {settings.POSTGRES_DB} already exists.")
except Exception as e:
print(f"Error checking/creating database: {e}")
print("\nTIP: Make sure PostgreSQL is installed and running on the specified port.")
sys.exit(1)
if __name__ == "__main__":
ensure_db_exists()