Fix database migration error by updating paths and configuration

This commit is contained in:
Automated Action 2025-05-16 04:47:01 +00:00
parent 059f240e2d
commit 06e08e36d6
6 changed files with 39 additions and 4 deletions

View File

@ -56,12 +56,17 @@ A simple Todo API built with FastAPI and SQLite.
pip install -r requirements.txt
```
3. Apply the database migrations:
3. Initialize the database directory:
```
python init_db.py
```
4. Apply the database migrations:
```
alembic upgrade head
```
4. Run the application:
5. Run the application:
```
uvicorn main:app --reload
```

View File

@ -35,7 +35,7 @@ script_location = alembic
# are written from script.py.mako
# output_encoding = utf-8
sqlalchemy.url = sqlite:////app/storage/db/db.sqlite
# We'll set this dynamically in env.py to match the application's config
[post_write_hooks]

View File

@ -1,14 +1,24 @@
from logging.config import fileConfig
import os
import sys
from pathlib import Path
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
# Add the parent directory to Python path so that 'app' can be imported
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Import the application config to use the same database URL
from app.core.config import settings
config.set_main_option("sqlalchemy.url", settings.SQLALCHEMY_DATABASE_URL)
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)

View File

@ -7,7 +7,9 @@ class Settings(BaseSettings):
PROJECT_NAME: str = "Simple Todo App"
# Database settings
DB_DIR = Path("/app") / "storage" / "db"
# Using a local path that will have proper permissions
BASE_DIR = Path(__file__).resolve().parent.parent.parent
DB_DIR = BASE_DIR / "db"
DB_DIR.mkdir(parents=True, exist_ok=True)
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite"

11
init_db.py Normal file
View File

@ -0,0 +1,11 @@
"""Initialize database directories and create initial tables."""
import os
from pathlib import Path
# Create directory for database
BASE_DIR = Path(__file__).resolve().parent
DB_DIR = BASE_DIR / "db"
DB_DIR.mkdir(parents=True, exist_ok=True)
print(f"Database directory created at: {DB_DIR}")
print("Now you can run: alembic upgrade head")

View File

@ -1,3 +1,10 @@
[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"
[tool.setuptools]
packages = ["app"]
[tool.ruff]
line-length = 88
target-version = "py37"