A test_agent.sh & a README.rst
deleted: .app.py.swp modified: .gitignore new file: README.rst modified: app.py new file: initialize_db.py new file: test_agent.sh
This commit is contained in:
parent
af2b5acc21
commit
596dbb923d
6 changed files with 547 additions and 80 deletions
68
initialize_db.py
Normal file
68
initialize_db.py
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
#!/usr/bin/env python
|
||||
"""
|
||||
Initialize or upgrade the main database for pyrafiles.
|
||||
|
||||
Usage:
|
||||
python initialize_db.py
|
||||
|
||||
Environment Variables:
|
||||
PYRAFILES_DB_URL (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.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
import transaction
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
# Adjust these imports to your actual project structure:
|
||||
# For example, if your models and Base are defined in "models.py", do:
|
||||
# from pyrafiles import Base
|
||||
# If you keep Base in a separate module, adjust accordingly.
|
||||
from app import Base # or from your_project.models import Base
|
||||
|
||||
def usage():
|
||||
script = os.path.basename(sys.argv[0])
|
||||
print(f"Usage: {script}")
|
||||
print("Example:")
|
||||
print(f" python {script}")
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
if len(sys.argv) > 2:
|
||||
# We only expect optional arguments. If needed, parse them here.
|
||||
usage()
|
||||
|
||||
# Read environment variable for DB URL
|
||||
db_url = os.environ.get("PYRAFILES_DB_URL", "sqlite:///main.db")
|
||||
logging.info(f"Using DB URL: {db_url}")
|
||||
|
||||
# Set up 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,
|
||||
)
|
||||
|
||||
SessionFactory = sessionmaker(bind=engine)
|
||||
|
||||
# Create or upgrade tables
|
||||
with transaction.manager:
|
||||
logging.info("Creating or upgrading tables using Base.metadata.create_all()")
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
logging.info("Database initialization complete.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue