mirror of
https://github.com/sstent/foodplanner.git
synced 2025-12-06 08:01:47 +00:00
added alembic database migrations, json import/export
This commit is contained in:
12
Dockerfile
12
Dockerfile
@@ -40,6 +40,11 @@ COPY --from=builder /app/venv /app/venv
|
||||
# Activate the virtual environment
|
||||
ENV PATH="/app/venv/bin:$PATH"
|
||||
|
||||
# Create data directory and set permissions
|
||||
RUN mkdir -p /app/data && \
|
||||
touch /app/data/meal_planner.db && \
|
||||
chown -R appuser:appuser /app/data
|
||||
|
||||
# Copy application code
|
||||
COPY . .
|
||||
|
||||
@@ -49,6 +54,13 @@ RUN chown -R appuser:appuser /app
|
||||
# Switch to non-root user
|
||||
USER appuser
|
||||
|
||||
# Set working directory to /app for the application
|
||||
WORKDIR /app
|
||||
|
||||
# Set environment variables
|
||||
ENV DATABASE_PATH=/app/data
|
||||
ENV DATABASE_URL=sqlite:////app/data/meal_planner.db
|
||||
|
||||
# Expose port (as defined in main.py)
|
||||
EXPOSE 8999
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
|
||||
# are written from script.py.mako
|
||||
# output_encoding = utf-8
|
||||
|
||||
sqlalchemy.url = sqlite:///data/meal_planner.db
|
||||
sqlalchemy.url = sqlite:////app/data/meal_planner.db
|
||||
|
||||
|
||||
[post_write_hooks]
|
||||
|
||||
25
main.py
25
main.py
@@ -30,10 +30,13 @@ import sqlite3
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||
|
||||
# Database setup - Use SQLite for easier setup
|
||||
DATABASE_PATH = os.getenv('DATABASE_PATH', './data')
|
||||
DATABASE_URL = f"sqlite:///{DATABASE_PATH}/meal_planner.db"
|
||||
# Use environment variables if set, otherwise use defaults
|
||||
DATABASE_PATH = os.getenv('DATABASE_PATH', '/app/data')
|
||||
DATABASE_URL = os.getenv('DATABASE_URL', f'sqlite:///{DATABASE_PATH}/meal_planner.db')
|
||||
|
||||
logging.info(f"Database URL: {DATABASE_URL}")
|
||||
logging.info(f"Absolute database path: {os.path.abspath(DATABASE_PATH)}")
|
||||
logging.info(f"Absolute database path: {os.path.abspath(DATABASE_PATH)}")
|
||||
|
||||
# Ensure the database directory exists
|
||||
logging.info(f"Creating database directory at: {DATABASE_PATH}")
|
||||
@@ -350,11 +353,25 @@ def startup_event():
|
||||
def run_migrations():
|
||||
logging.info("Running database migrations...")
|
||||
try:
|
||||
alembic_cfg = Config("alembic.ini")
|
||||
# Get absolute path to alembic.ini
|
||||
alembic_ini_path = os.path.abspath("alembic.ini")
|
||||
logging.info(f"Alembic config path: {alembic_ini_path}")
|
||||
|
||||
alembic_cfg = Config(alembic_ini_path)
|
||||
|
||||
# Get database URL for logging
|
||||
db_url = alembic_cfg.get_main_option("sqlalchemy.url")
|
||||
logging.info(f"Database URL from alembic.ini: {db_url}")
|
||||
|
||||
# Verify alembic versions directory
|
||||
versions_dir = os.path.abspath("alembic/versions")
|
||||
logging.info(f"Alembic versions directory: {versions_dir}")
|
||||
logging.info(f"Version files: {os.listdir(versions_dir)}")
|
||||
|
||||
command.upgrade(alembic_cfg, "head")
|
||||
logging.info("Database migrations completed successfully.")
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to run database migrations: {e}")
|
||||
logging.error(f"Failed to run database migrations: {e}", exc_info=True)
|
||||
raise
|
||||
|
||||
# Utility functions
|
||||
|
||||
Reference in New Issue
Block a user