57 lines
2.1 KiB
Bash
57 lines
2.1 KiB
Bash
|
|
#!/bin/bash
|
||
|
|
|
||
|
|
# WealthWise Database Migration Script
|
||
|
|
# This script initializes Alembic and creates the initial migration
|
||
|
|
|
||
|
|
echo "Setting up Alembic migrations for WealthWise..."
|
||
|
|
|
||
|
|
# Check if alembic is installed
|
||
|
|
if ! command -v alembic &> /dev/null; then
|
||
|
|
echo "Installing alembic..."
|
||
|
|
pip install alembic
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Initialize Alembic if not already initialized
|
||
|
|
if [ ! -d "alembic" ]; then
|
||
|
|
echo "Initializing Alembic..."
|
||
|
|
alembic init alembic
|
||
|
|
|
||
|
|
# Update alembic.ini with database URL
|
||
|
|
sed -i '' 's|sqlalchemy.url = driver://user:pass@localhost/dbname|sqlalchemy.url = postgresql://postgres:postgres@localhost:5432/wealthwise|' alembic/alembic.ini 2>/dev/null || sed -i 's|sqlalchemy.url = driver://user:pass@localhost/dbname|sqlalchemy.url = postgresql://postgres:postgres@localhost:5432/wealthwise|' alembic/alembic.ini
|
||
|
|
|
||
|
|
echo "Alembic initialized successfully!"
|
||
|
|
else
|
||
|
|
echo "Alembic already initialized."
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo ""
|
||
|
|
echo "Next steps:"
|
||
|
|
echo "1. Update alembic/env.py to import your models (see instructions below)"
|
||
|
|
echo "2. Run: alembic revision --autogenerate -m 'Initial migration'"
|
||
|
|
echo "3. Run: alembic upgrade head"
|
||
|
|
echo ""
|
||
|
|
echo "=== alembic/env.py Configuration ==="
|
||
|
|
echo "Add these imports to alembic/env.py:"
|
||
|
|
echo ""
|
||
|
|
echo "import asyncio"
|
||
|
|
echo "from sqlalchemy.ext.asyncio import AsyncEngine"
|
||
|
|
echo "from app.models import User, Portfolio, Transaction"
|
||
|
|
echo "from app.models.base import BaseModel"
|
||
|
|
echo ""
|
||
|
|
echo "Then update run_migrations_online() to use async engine:"
|
||
|
|
echo ""
|
||
|
|
echo "def do_run_migrations(connection):"
|
||
|
|
echo " context.configure(connection=connection, target_metadata=BaseModel.metadata)"
|
||
|
|
echo " with context.begin_transaction():"
|
||
|
|
echo " context.run_migrations()"
|
||
|
|
echo ""
|
||
|
|
echo "async def run_migrations_online():"
|
||
|
|
echo " connectable = AsyncEngine(create_async_engine(config.get_main_option('sqlalchemy.url')))"
|
||
|
|
echo " async with connectable.connect() as connection:"
|
||
|
|
echo " await connection.run_sync(do_run_migrations)"
|
||
|
|
echo " await connectable.dispose()"
|
||
|
|
echo ""
|
||
|
|
echo "if context.is_offline_mode():"
|
||
|
|
echo " run_migrations_offline()"
|
||
|
|
echo "else:"
|
||
|
|
echo " asyncio.run(run_migrations_online())"
|