mirror of
https://github.com/sstent/foodplanner.git
synced 2026-02-16 15:55:25 +00:00
reverted to manual db table creation, nned to fix alembic
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from logging.config import fileConfig
|
||||
|
||||
import logging
|
||||
import os
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
|
||||
@@ -16,8 +17,9 @@ if config.config_file_name is not None:
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
from main import Base
|
||||
target_metadata = Base.metadata
|
||||
# We create an empty metadata object since we're not using autogenerate
|
||||
# and we have explicit migration files
|
||||
target_metadata = None
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
@@ -37,10 +39,11 @@ def run_migrations_offline() -> None:
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
# Use environment variable if available, otherwise fall back to config
|
||||
url = os.getenv('DATABASE_URL', config.get_main_option("sqlalchemy.url"))
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
target_metadata=None,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
@@ -56,22 +59,33 @@ def run_migrations_online() -> None:
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
logging.info("DEBUG: Creating database engine for alembic")
|
||||
# Use environment variable for database URL if available
|
||||
db_url = os.getenv('DATABASE_URL', config.get_main_option("sqlalchemy.url"))
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
{'sqlalchemy.url': db_url},
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
logging.info("DEBUG: Database engine created successfully")
|
||||
|
||||
with connectable.connect() as connection:
|
||||
logging.info("DEBUG: Database connection established for alembic")
|
||||
context.configure(
|
||||
connection=connection, target_metadata=target_metadata
|
||||
connection=connection, target_metadata=None
|
||||
)
|
||||
logging.info("DEBUG: Alembic context configured")
|
||||
|
||||
with context.begin_transaction():
|
||||
logging.info("DEBUG: Starting alembic transaction")
|
||||
context.run_migrations()
|
||||
logging.info("DEBUG: Alembic migrations completed within transaction")
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
logging.info("DEBUG: Running migrations in offline mode")
|
||||
run_migrations_offline()
|
||||
else:
|
||||
logging.info("DEBUG: Running migrations in online mode")
|
||||
run_migrations_online()
|
||||
logging.info("DEBUG: Online migrations completed")
|
||||
|
||||
@@ -20,11 +20,151 @@ depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
op.create_table('foods',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(), nullable=True),
|
||||
sa.Column('serving_size', sa.String(), nullable=True),
|
||||
sa.Column('serving_unit', sa.String(), nullable=True),
|
||||
sa.Column('calories', sa.Float(), nullable=True),
|
||||
sa.Column('protein', sa.Float(), nullable=True),
|
||||
sa.Column('carbs', sa.Float(), nullable=True),
|
||||
sa.Column('fat', sa.Float(), nullable=True),
|
||||
sa.Column('fiber', sa.Float(), nullable=True),
|
||||
sa.Column('sugar', sa.Float(), nullable=True),
|
||||
sa.Column('sodium', sa.Float(), nullable=True),
|
||||
sa.Column('calcium', sa.Float(), nullable=True),
|
||||
sa.Column('source', sa.String(), nullable=True),
|
||||
sa.Column('brand', sa.String(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_foods_id'), 'foods', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_foods_name'), 'foods', ['name'], unique=True)
|
||||
|
||||
op.create_table('meals',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(), nullable=True),
|
||||
sa.Column('meal_type', sa.String(), nullable=True),
|
||||
sa.Column('meal_time', sa.String(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_meals_id'), 'meals', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_meals_name'), 'meals', ['name'], unique=False)
|
||||
|
||||
op.create_table('meal_foods',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('meal_id', sa.Integer(), nullable=True),
|
||||
sa.Column('food_id', sa.Integer(), nullable=True),
|
||||
sa.Column('quantity', sa.Float(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['food_id'], ['foods.id'], ),
|
||||
sa.ForeignKeyConstraint(['meal_id'], ['meals.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_meal_foods_id'), 'meal_foods', ['id'], unique=False)
|
||||
|
||||
op.create_table('plans',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('person', sa.String(), nullable=True),
|
||||
sa.Column('date', sa.Date(), nullable=True),
|
||||
sa.Column('meal_id', sa.Integer(), nullable=True),
|
||||
sa.Column('meal_time', sa.String(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['meal_id'], ['meals.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_plans_id'), 'plans', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_plans_person'), 'plans', ['person'], unique=False)
|
||||
op.create_index(op.f('ix_plans_date'), 'plans', ['date'], unique=False)
|
||||
|
||||
op.create_table('templates',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_templates_id'), 'templates', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_templates_name'), 'templates', ['name'], unique=True)
|
||||
|
||||
op.create_table('template_meals',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('template_id', sa.Integer(), nullable=True),
|
||||
sa.Column('meal_id', sa.Integer(), nullable=True),
|
||||
sa.Column('meal_time', sa.String(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['meal_id'], ['meals.id'], ),
|
||||
sa.ForeignKeyConstraint(['template_id'], ['templates.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_template_meals_id'), 'template_meals', ['id'], unique=False)
|
||||
|
||||
op.create_table('weekly_menus',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_weekly_menus_id'), 'weekly_menus', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_weekly_menus_name'), 'weekly_menus', ['name'], unique=True)
|
||||
|
||||
op.create_table('weekly_menu_days',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('weekly_menu_id', sa.Integer(), nullable=True),
|
||||
sa.Column('day_of_week', sa.Integer(), nullable=True),
|
||||
sa.Column('template_id', sa.Integer(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['template_id'], ['templates.id'], ),
|
||||
sa.ForeignKeyConstraint(['weekly_menu_id'], ['weekly_menus.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_weekly_menu_days_id'), 'weekly_menu_days', ['id'], unique=False)
|
||||
|
||||
op.create_table('tracked_days',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('person', sa.String(), nullable=True),
|
||||
sa.Column('date', sa.Date(), nullable=True),
|
||||
sa.Column('is_modified', sa.Boolean(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_tracked_days_id'), 'tracked_days', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_tracked_days_person'), 'tracked_days', ['person'], unique=False)
|
||||
op.create_index(op.f('ix_tracked_days_date'), 'tracked_days', ['date'], unique=False)
|
||||
|
||||
op.create_table('tracked_meals',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('tracked_day_id', sa.Integer(), nullable=True),
|
||||
sa.Column('meal_id', sa.Integer(), nullable=True),
|
||||
sa.Column('meal_time', sa.String(), nullable=True),
|
||||
sa.Column('quantity', sa.Float(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['meal_id'], ['meals.id'], ),
|
||||
sa.ForeignKeyConstraint(['tracked_day_id'], ['tracked_days.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_tracked_meals_id'), 'tracked_meals', ['id'], unique=False)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
op.drop_index(op.f('ix_tracked_meals_id'), table_name='tracked_meals')
|
||||
op.drop_table('tracked_meals')
|
||||
op.drop_index(op.f('ix_tracked_days_date'), table_name='tracked_days')
|
||||
op.drop_index(op.f('ix_tracked_days_person'), table_name='tracked_days')
|
||||
op.drop_index(op.f('ix_tracked_days_id'), table_name='tracked_days')
|
||||
op.drop_table('tracked_days')
|
||||
op.drop_index(op.f('ix_weekly_menu_days_id'), table_name='weekly_menu_days')
|
||||
op.drop_table('weekly_menu_days')
|
||||
op.drop_index(op.f('ix_weekly_menus_name'), table_name='weekly_menus')
|
||||
op.drop_index(op.f('ix_weekly_menus_id'), table_name='weekly_menus')
|
||||
op.drop_table('weekly_menus')
|
||||
op.drop_index(op.f('ix_template_meals_id'), table_name='template_meals')
|
||||
op.drop_table('template_meals')
|
||||
op.drop_index(op.f('ix_templates_name'), table_name='templates')
|
||||
op.drop_index(op.f('ix_templates_id'), table_name='templates')
|
||||
op.drop_table('templates')
|
||||
op.drop_index(op.f('ix_plans_date'), table_name='plans')
|
||||
op.drop_index(op.f('ix_plans_person'), table_name='plans')
|
||||
op.drop_index(op.f('ix_plans_id'), table_name='plans')
|
||||
op.drop_table('plans')
|
||||
op.drop_index(op.f('ix_meal_foods_id'), table_name='meal_foods')
|
||||
op.drop_table('meal_foods')
|
||||
op.drop_index(op.f('ix_meals_name'), table_name='meals')
|
||||
op.drop_index(op.f('ix_meals_id'), table_name='meals')
|
||||
op.drop_table('meals')
|
||||
op.drop_index(op.f('ix_foods_name'), table_name='foods')
|
||||
op.drop_index(op.f('ix_foods_id'), table_name='foods')
|
||||
op.drop_table('foods')
|
||||
# ### end Alembic commands ###
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
"""Create all tables
|
||||
|
||||
Revision ID: fd4f39482990
|
||||
Revises: cf94fca21104
|
||||
Create Date: 2025-09-28 12:15:02.412270
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = 'fd4f39482990'
|
||||
down_revision: Union[str, None] = 'cf94fca21104'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
pass
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user