Fix database migration issues and improve path handling
This commit is contained in:
parent
d8e6820886
commit
d13fdbd5a6
51
.gitignore
vendored
Normal file
51
.gitignore
vendored
Normal 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
|
@ -48,12 +48,19 @@ pip install -r requirements.txt
|
||||
|
||||
## 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:
|
||||
|
||||
```bash
|
||||
# Make sure you're in the project root directory
|
||||
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
|
||||
|
||||
Start the application with Uvicorn:
|
||||
|
@ -35,7 +35,8 @@ script_location = alembic
|
||||
# are written from script.py.mako
|
||||
# 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 defines scripts or Python functions that are run
|
||||
|
@ -1,10 +1,18 @@
|
||||
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
|
||||
|
||||
# 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
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
@ -13,10 +21,14 @@ config = context.config
|
||||
# This line sets up loggers basically.
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
from app.database import Base
|
||||
# Import the database configuration
|
||||
from app.database import Base, SQLALCHEMY_DATABASE_URL
|
||||
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
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
|
@ -1,10 +1,18 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
# Create database directory if it doesn't exist
|
||||
DB_DIR = Path("/app") / "storage" / "db"
|
||||
# Check if /app/storage is available, otherwise use a local directory
|
||||
if os.path.exists("/app"):
|
||||
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)
|
||||
|
||||
# Database URL
|
||||
|
Loading…
x
Reference in New Issue
Block a user