fix the path for the database initialize_db.py creation script
modified: initialize_db.py
This commit is contained in:
parent
69399b0729
commit
d77457ebf1
1 changed files with 17 additions and 16 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue