Fix database migration error by updating paths and configuration
This commit is contained in:
parent
059f240e2d
commit
06e08e36d6
@ -56,12 +56,17 @@ A simple Todo API built with FastAPI and SQLite.
|
|||||||
pip install -r requirements.txt
|
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
|
alembic upgrade head
|
||||||
```
|
```
|
||||||
|
|
||||||
4. Run the application:
|
5. Run the application:
|
||||||
```
|
```
|
||||||
uvicorn main:app --reload
|
uvicorn main:app --reload
|
||||||
```
|
```
|
||||||
|
@ -35,7 +35,7 @@ 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
|
# We'll set this dynamically in env.py to match the application's config
|
||||||
|
|
||||||
|
|
||||||
[post_write_hooks]
|
[post_write_hooks]
|
||||||
|
@ -1,14 +1,24 @@
|
|||||||
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
|
||||||
|
|
||||||
|
# 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
|
# 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
|
||||||
|
|
||||||
|
# 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.
|
# Interpret the config file for Python logging.
|
||||||
# This line sets up loggers basically.
|
# This line sets up loggers basically.
|
||||||
fileConfig(config.config_file_name)
|
fileConfig(config.config_file_name)
|
||||||
|
@ -7,7 +7,9 @@ class Settings(BaseSettings):
|
|||||||
PROJECT_NAME: str = "Simple Todo App"
|
PROJECT_NAME: str = "Simple Todo App"
|
||||||
|
|
||||||
# Database settings
|
# 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)
|
DB_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite"
|
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite"
|
||||||
|
11
init_db.py
Normal file
11
init_db.py
Normal 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")
|
@ -1,3 +1,10 @@
|
|||||||
|
[build-system]
|
||||||
|
requires = ["setuptools>=42", "wheel"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
|
[tool.setuptools]
|
||||||
|
packages = ["app"]
|
||||||
|
|
||||||
[tool.ruff]
|
[tool.ruff]
|
||||||
line-length = 88
|
line-length = 88
|
||||||
target-version = "py37"
|
target-version = "py37"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user