Alembic configuration

This commit is contained in:
2025-09-21 16:03:44 +02:00
parent b47c88b24b
commit 09b92eceb8
10 changed files with 695 additions and 281 deletions

202
Makefile
View File

@@ -20,7 +20,6 @@ install-test:
install-dev:
pip install -r requirements.txt
pip install -r tests/requirements-test.txt
pip install -r requirements-dev.txt
install-docs:
@@ -33,9 +32,9 @@ dev:
uvicorn main:app --reload --host 0.0.0.0 --port 8000
# Safe development startup (checks migrations first)
dev-safe: migrate-check
@echo 🔍 Migration check passed, starting development server...
uvicorn main:app --reload --host 0.0.0.0 --port 8000
# Development workflow with migration check
dev-safe: migrate-check dev
@echo Development server started with migration check
dev-with-docs:
@echo Starting API server and documentation server...
@@ -148,7 +147,7 @@ ci: format lint test-coverage
# Database-aware CI pipeline
ci-db: format lint migrate-check test-coverage
@echo CI pipeline with database checks completed
@echo CI pipeline with database checks completed
# Database migrations
migrate-create:
@@ -158,69 +157,69 @@ migrate-create-manual:
@if "$(message)"=="" (echo Error: Please provide a message. Usage: make migrate-create-manual message="your_description") else (alembic revision -m "$(message)")
migrate-up:
@echo Running database migrations...
@echo [MIGRATE] Running database migrations...
alembic upgrade head
@echo Migrations completed successfully
@echo [MIGRATE] Migrations completed successfully
migrate-down:
@echo Rolling back last migration...
@echo [MIGRATE] Rolling back last migration...
alembic downgrade -1
@echo Rollback completed
@echo [MIGRATE] Rollback completed
migrate-down-to:
@if "$(revision)"=="" (echo Error: Please provide revision. Usage: make migrate-down-to revision="revision_id") else (alembic downgrade $(revision))
migrate-reset:
@echo Resetting database...
@echo [MIGRATE] Resetting database...
alembic downgrade base
alembic upgrade head
@echo Database reset completed
@echo [MIGRATE] Database reset completed
migrate-status:
@echo 📊 Current migration status:
@echo [STATUS] Current migration status:
alembic current
@echo.
@echo 📋 Migration history:
@echo [HISTORY] Migration history:
alembic history --verbose
migrate-show:
@if "$(revision)"=="" (echo Error: Please provide revision. Usage: make migrate-show revision="revision_id") else (alembic show $(revision))
migrate-heads:
@echo 📍 Current migration heads:
@echo [INFO] Current migration heads:
alembic heads
migrate-check:
@echo 🔍 Checking for pending migrations...
@python -c "from alembic import command, config; cfg = config.Config('alembic.ini'); command.check(cfg)" && echo " No pending migrations" || echo "⚠️ Pending migrations found"
@echo [CHECK] Checking for pending migrations...
@python -c "from alembic import command, config; cfg = config.Config('alembic.ini'); command.check(cfg)" && echo "[CHECK] No pending migrations" || echo "[WARNING] Pending migrations found"
# Create the index migration we discussed
migrate-create-indexes:
@echo [CREATE] Creating index migration...
alembic revision -m "add_database_indexes"
@echo [CREATE] Index migration created. Please edit the file to add your indexes.
# Database initialization (enhanced)
db-init: migrate-up
@echo 🚀 Database initialization completed
@echo [INIT] Database initialization completed
db-fresh: migrate-reset
@echo 🔄 Fresh database setup completed
# Database backup before risky operations (if using PostgreSQL/MySQL)
backup-db:
@echo 💾 Creating database backup...
@python scripts/backup_database.py
@echo ✅ Database backup created
@echo [INIT] Fresh database setup completed
# === FRESH START COMMANDS (Development) ===
fresh-backup:
@echo 💾 Creating backup of current state...
@echo [BACKUP] Creating backup of current state...
@if not exist scripts mkdir scripts
@python scripts/backup_database.py
fresh-clean:
@echo 🧹 Cleaning up for fresh start...
@echo [CLEAN] Cleaning up for fresh start...
@if exist ecommerce.db del ecommerce.db
@if exist alembic\versions\*.py (for %%f in (alembic\versions\*.py) do if not "%%~nf"=="__init__" del "%%f")
@echo Cleanup completed
@echo [CLEAN] Cleanup completed
fresh-setup: fresh-backup fresh-clean
@echo 🚀 Setting up fresh database with proper migrations...
@echo [SETUP] Setting up fresh database with proper migrations...
@echo.
@echo Step 1: Creating initial migration from models...
alembic revision --autogenerate -m "initial_schema_and_indexes"
@@ -228,12 +227,12 @@ fresh-setup: fresh-backup fresh-clean
@echo Step 2: Running the migration to create database...
alembic upgrade head
@echo.
@echo Fresh setup completed!
@echo [SETUP] Fresh setup completed!
@echo Database is now managed entirely by Alembic migrations.
# Check what the fresh migration would contain
fresh-preview:
@echo 🔍 Previewing what the fresh migration would contain...
@echo [PREVIEW] Showing what the fresh migration would contain...
@echo This will show what tables/indexes would be created.
@echo.
@if exist ecommerce.db (echo Current database detected - showing diff) else (echo No database - showing full schema)
@@ -241,12 +240,12 @@ fresh-preview:
# Complete development environment setup with fresh database
dev-fresh-setup: install-all fresh-setup
@echo 🎉 Complete fresh development setup completed!
@echo [SUCCESS] Complete fresh development setup completed!
@echo.
@echo What was done:
@echo All dependencies installed
@echo Database created with migrations
@echo Migration tracking initialized
@echo [OK] All dependencies installed
@echo [OK] Database created with migrations
@echo [OK] Migration tracking initialized
@echo.
@echo Next steps:
@echo 1. Review the migration file in alembic/versions/
@@ -256,15 +255,8 @@ dev-fresh-setup: install-all fresh-setup
# Verify the fresh setup worked
verify-fresh:
@echo 🔍 Verifying fresh setup...
@echo.
@echo Migration status:
@alembic current
@echo.
@echo Database tables:
@python -c "from sqlalchemy import create_engine, text; engine = create_engine('sqlite:///./ecommerce.db'); print('Tables:', [r[0] for r in engine.execute(text('SELECT name FROM sqlite_master WHERE type=\"table\"')).fetchall()])"
@echo.
@echo ✅ Verification completed
@echo [VERIFY] Running comprehensive verification...
@python scripts/verify_setup.py
# Docker commands
docker-build:
@@ -283,10 +275,10 @@ docker-restart: docker-down docker-up
# Pre-deployment checks
pre-deploy: qa migrate-status
@echo 🚀 Pre-deployment checks completed!
@echo [DEPLOY] Pre-deployment checks completed!
@echo Ready for deployment.
# Production deployment
# Production deployment with migrations
deploy-staging: migrate-up
docker-compose -f docker-compose.staging.yml up -d
@@ -315,6 +307,7 @@ clean-all: clean docs-clean
@echo All build artifacts cleaned!
# Development workflow shortcuts
# Enhanced setup commands
setup: install-all db-init
@echo Development environment setup complete!
@echo Run 'make dev-full' to start both API and documentation servers
@@ -323,6 +316,7 @@ setup-fresh: install-all db-fresh
@echo Fresh development environment setup complete!
@echo Run 'make dev-full' to start both API and documentation servers
setup-test: install-test
@echo Test environment setup complete!
@@ -360,6 +354,106 @@ workflow-deploy:
@echo 2. Ready for deployment!
@echo Run 'make deploy-staging' or 'make deploy-prod' to deploy.
# Create missing __init__.py files
fix-init-files:
@echo [FIX] Creating missing __init__.py files...
@if not exist models\__init__.py (echo # models/__init__.py > models\__init__.py && echo Created models\__init__.py)
@if not exist models\database\__init__.py (echo # models/database/__init__.py > models\database\__init__.py && echo Created models\database\__init__.py)
@if not exist models\api\__init__.py (echo # models/api/__init__.py > models\api\__init__.py && echo Created models\api\__init__.py)
@echo [FIX] Init files created
# Test model imports separately
test-database-imports:
@echo [TEST] Testing database model imports...
@python -c "from models.database.base import Base; print('✓ Base imported:', type(Base)); print('✓ Base metadata tables:', list(Base.metadata.tables.keys()))"
test-api-imports:
@echo [TEST] Testing API model imports...
@python -c "import models.api; print('✓ API models package imported')"
test-all-imports:
@echo [TEST] Testing all model imports...
@python -c "from models import Base, User, Product, Stock; print('✓ Database models imported'); print('✓ Found tables:', list(Base.metadata.tables.keys())); import models.api; print('✓ API models imported')"
# Enhanced migration fix with better error handling
fix-migration: fix-init-files
@echo [FIX] Fixing migration with proper model imports...
@echo Step 1: Testing imports first...
@$(MAKE) test-database-imports
@echo Step 2: Removing current migration...
@alembic downgrade base || echo "No migrations to downgrade"
@echo Step 3: Deleting migration files...
@for %%f in (alembic\versions\*.py) do if not "%%~nf"=="__init__" del "%%f"
@echo Step 4: Creating new migration with all models...
@alembic revision --autogenerate -m "initial_complete_schema"
@echo Step 5: Applying new migration...
@alembic upgrade head
@echo [FIX] Migration fix completed!
# Comprehensive project verification
verify-project:
@echo [VERIFY] Comprehensive project verification...
@echo.
@echo Testing database models:
@$(MAKE) test-database-imports
@echo.
@echo Testing API models:
@$(MAKE) test-api-imports
@echo.
@echo Database verification:
@python scripts/verify_setup.py
@echo.
@echo [VERIFY] Project verification completed!
# Complete fresh restart for your project structure
fresh-restart: test-all-imports fix-migration verify-project
@echo [SUCCESS] Complete fresh restart with full project structure completed!
@echo.
@echo Your project now has:
@echo ✓ Database models (SQLAlchemy) for data storage
@echo ✓ API models (Pydantic) for request/response validation
@echo ✓ Proper Alembic migrations
@echo ✓ All imports working correctly
@echo.
@echo Next steps:
@echo 1. Run 'make dev' to start your API server
@echo 2. Visit http://localhost:8000/docs for API documentation
@echo 3. Use 'make migrate-create message="description"' for future changes
# Simple fix commands - add to your Makefile
# Simple fix for import issues
simple-fix:
@echo [SIMPLE-FIX] Running simple import fix...
@python scripts/simple_fix.py
# Test imports one by one
test-step-by-step:
@echo [TEST] Testing imports step by step...
@python -c "from models.database.base import Base; print('✓ Base OK:', len(Base.metadata.tables), 'tables')"
@python -c "from models.database.user import User; print('✓ User OK')"
@python -c "from models.database.product import Product; print('✓ Product OK')"
@python -c "from models.database.stock import Stock; print('✓ Stock OK')"
@python -c "from models.database.shop import Shop; print('✓ Shop OK')"
@python -c "from models.database.marketplace import MarketplaceImportJob; print('✓ Marketplace OK')"
# Simple migration fix
simple-migration-fix:
@echo [MIGRATION] Simple migration fix...
@echo Step 1: Remove old migration...
@alembic downgrade base || echo "No migration to downgrade"
@echo Step 2: Delete migration files...
@for %%f in (alembic\versions\*.py) do if not "%%~nf"=="__init__" del "%%f"
@echo Step 3: Create new migration...
@alembic revision --autogenerate -m "complete_schema"
@echo Step 4: Apply migration...
@alembic upgrade head
@echo [MIGRATION] Done!
# Complete simple workflow
simple-restart: simple-fix test-step-by-step simple-migration-fix verify-fresh
@echo [SUCCESS] Simple restart completed!
# Help command
help:
@echo Available commands:
@@ -440,6 +534,24 @@ help:
@echo make docs-serve # Start documentation server
@echo make qa # Run quality checks
# Help for migration
help-migrations:
@echo === DATABASE MIGRATIONS ===
@echo migrate-create message="msg" - Create auto-generated migration
@echo migrate-create-manual message="msg" - Create empty migration template
@echo migrate-up - Run all pending migrations
@echo migrate-down - Rollback last migration
@echo migrate-down-to revision="id" - Rollback to specific revision
@echo migrate-reset - Reset database to base and rerun all
@echo migrate-status - Show current migration status and history
@echo migrate-show revision="id" - Show specific migration details
@echo migrate-heads - Show current migration heads
@echo migrate-check - Check for pending migrations
@echo migrate-create-indexes - Create the database indexes migration
@echo db-init - Initialize database with migrations
@echo db-fresh - Fresh database setup
@echo.
# Help for fresh start
help-fresh:
@echo === FRESH START COMMANDS ===