Changed the base directory where the data generated by the program to be at the home directory and also added a check to ensure the creation of the program directory

This commit is contained in:
Gustavo Henrique Santos Souza de Miranda
2025-07-05 07:29:09 -03:00
parent 394f813f6f
commit d114357d50
2 changed files with 60 additions and 1 deletions
+29 -1
View File
@@ -1,14 +1,42 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from pathlib import Path
import os
import shutil
Base = declarative_base()
def get_database_path() -> Path:
"""
Get the database file path following XDG Base Directory specification.
Creates the directory if it doesn't exist.
"""
# Get home directory
home = Path.home()
# Create .pilgrim directory if it doesn't exist
pilgrim_dir = home / ".pilgrim"
pilgrim_dir.mkdir(exist_ok=True)
# Database file path
db_path = pilgrim_dir / "database.db"
# If database doesn't exist in new location but exists in current directory,
# migrate it
if not db_path.exists():
current_db = Path("database.db")
if current_db.exists():
shutil.copy2(current_db, db_path)
print(f"Database migrated from {current_db} to {db_path}")
return db_path
class Database:
def __init__(self):
db_path = get_database_path()
self.engine = create_engine(
"sqlite:///database.db",
f"sqlite:///{db_path}",
echo=False,
connect_args={"check_same_thread": False},
)