Fix database migration issues and improve path handling

This commit is contained in:
Automated Action 2025-05-16 03:39:49 +00:00
parent d8e6820886
commit d13fdbd5a6
5 changed files with 84 additions and 5 deletions

51
.gitignore vendored Normal file
View File

@ -0,0 +1,51 @@
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# Virtual Environment
venv/
env/
ENV/
# VS Code
.vscode/
.history
# Logs
logs/
*.log
# Local database
storage/
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
# Jupyter Notebook
.ipynb_checkpoints

View File

@ -48,12 +48,19 @@ pip install -r requirements.txt
## Database Setup ## Database Setup
The application uses SQLite as the database. The database file will be created automatically in the following location:
- In production: `/app/storage/db/db.sqlite`
- In development: `./storage/db/db.sqlite` (relative to the project root)
1. Run database migrations: 1. Run database migrations:
```bash ```bash
# Make sure you're in the project root directory
alembic upgrade head alembic upgrade head
``` ```
Note: If you encounter import errors with Alembic, make sure you're running the command from the project root directory so that the `app` module can be found.
## Running the Application ## Running the Application
Start the application with Uvicorn: Start the application with Uvicorn:

View File

@ -35,7 +35,8 @@ script_location = alembic
# are written from script.py.mako # are written from script.py.mako
# output_encoding = utf-8 # output_encoding = utf-8
sqlalchemy.url = sqlite:////app/storage/db/db.sqlite # The SQLAlchemy connection string will be dynamically set in env.py
sqlalchemy.url = driver://user:pass@localhost/dbname
[post_write_hooks] [post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run # post_write_hooks defines scripts or Python functions that are run

View File

@ -1,10 +1,18 @@
from logging.config import fileConfig from logging.config import fileConfig
import os
import sys
from pathlib import Path
from sqlalchemy import engine_from_config from sqlalchemy import engine_from_config
from sqlalchemy import pool from sqlalchemy import pool
from alembic import context from alembic import context
# Append the parent directory to sys.path to allow imports from the app package
# This is crucial for Alembic to find the app module
BASE_DIR = Path(__file__).resolve().parent.parent
sys.path.append(str(BASE_DIR))
# this is the Alembic Config object, which provides # this is the Alembic Config object, which provides
# access to the values within the .ini file in use. # access to the values within the .ini file in use.
config = context.config config = context.config
@ -13,10 +21,14 @@ config = context.config
# This line sets up loggers basically. # This line sets up loggers basically.
fileConfig(config.config_file_name) fileConfig(config.config_file_name)
# add your model's MetaData object here # Import the database configuration
# for 'autogenerate' support from app.database import Base, SQLALCHEMY_DATABASE_URL
from app.database import Base
from app.models.todo import Todo from app.models.todo import Todo
# Set the SQLAlchemy URL in Alembic configuration
config.set_main_option("sqlalchemy.url", SQLALCHEMY_DATABASE_URL)
# Set the target metadata
target_metadata = Base.metadata target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py, # other values from the config, defined by the needs of env.py,

View File

@ -1,10 +1,18 @@
import os
from pathlib import Path from pathlib import Path
from sqlalchemy import create_engine from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
# Create database directory if it doesn't exist # Create database directory if it doesn't exist
# Check if /app/storage is available, otherwise use a local directory
if os.path.exists("/app"):
DB_DIR = Path("/app") / "storage" / "db" DB_DIR = Path("/app") / "storage" / "db"
else:
# Fallback to a local directory for development
DB_DIR = Path(__file__).resolve().parent.parent.parent / "storage" / "db"
# Ensure the directory exists
DB_DIR.mkdir(parents=True, exist_ok=True) DB_DIR.mkdir(parents=True, exist_ok=True)
# Database URL # Database URL