From d77457ebf1a16f25906c4fb44d4555f75bec2b59 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Wed, 22 Jan 2025 17:43:56 -0500 Subject: [PATCH] fix the path for the database initialize_db.py creation script modified: initialize_db.py --- initialize_db.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/initialize_db.py b/initialize_db.py index e4f6b5d..00d0298 100644 --- a/initialize_db.py +++ b/initialize_db.py @@ -1,18 +1,14 @@ #!/usr/bin/env python """ -Initialize or upgrade the main database for this web application. +Initialize or upgrade the main database for PyraLogs. Usage: python initialize_db.py -Environment Variables: - DB_URI (optional): - The SQLAlchemy database URL (e.g., "sqlite:///main.db"). - Defaults to "sqlite:///main.db" if not set. - Description: Creates or updates all tables referenced by `Base.metadata`. - If the database file/tables do not exist, they will be created. + The database file will be created (if it does not exist) in the + DATA_DIR as specified in the app file. No custom paths are allowed. """ import os @@ -24,9 +20,8 @@ from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.pool import StaticPool -# Adjust these imports to your actual project structure: -# Ensure 'Base' is imported from your 'app.py' where your models are defined. -from app import Base # Assuming 'app.py' contains your models and 'Base' +# Import Base and DATA_DIR from your app.py (Adjust if needed) +from app import Base, DATA_DIR # Make sure app.py defines Base and DATA_DIR def usage(): script = os.path.basename(sys.argv[0]) @@ -37,19 +32,25 @@ def usage(): def main(): logging.basicConfig(level=logging.INFO) + + # We do not allow any command-line arguments. if len(sys.argv) > 1: - # We only expect optional arguments. If needed, parse them here. usage() - # Read environment variable for DB URL - db_url = os.environ.get("DB_URI", "sqlite:///main.db") + # Ensure DATA_DIR exists + if not os.path.exists(DATA_DIR): + os.makedirs(DATA_DIR) + logging.info(f"Created data directory at {DATA_DIR}") + + # Always use the default path from app.py for the database + db_url = f"sqlite:///{os.path.join(DATA_DIR, 'main.db')}" logging.info(f"Using DB URL: {db_url}") - # Set up engine + # Set up the engine engine = create_engine( db_url, - connect_args={"check_same_thread": False} if "sqlite" in db_url else {}, - poolclass=StaticPool if "sqlite" in db_url else None, + connect_args={"check_same_thread": False}, + poolclass=StaticPool ) SessionFactory = sessionmaker(bind=engine)