Skip to main content

A comprehensive database migration system with FastAPI integration

Project description

Database Migration System with PostgreSQL and Docker

A comprehensive, ORM-agnostic database migration system built with FastAPI and PostgreSQL, featuring:

  • Version Control: Track and apply database schema changes systematically
  • Auto-diff: Generate migrations automatically from schema differences
  • Rollback Support: Safely rollback migrations when needed
  • FastAPI Integration: REST API for migration management
  • Docker Support: Easy setup with Docker Compose
  • PostgreSQL Optimized: Secure parameterized queries and proper transaction handling

๐Ÿš€ Quick Start

Prerequisites

  • Docker and Docker Compose
  • Python 3.11+ (if running locally)

1. Clone and Setup

git clone <your-repo>
cd demo

# Start services
docker-compose up --build

2. Verify Setup

The application will be available at:

3. Test the System

# Check health
curl http://localhost:8000/health

# Get migration status
curl http://localhost:8000/migrations/status

# Run demo setup (creates and applies example migrations)
curl -X POST http://localhost:8000/demo/setup

๐Ÿ“ Project Structure

demo/
โ”œโ”€โ”€ database_manager.py          # Original migration system
โ”œโ”€โ”€ database_manager_fixed.py    # Fixed version with security improvements
โ”œโ”€โ”€ main.py                     # FastAPI application
โ”œโ”€โ”€ docker-compose.yml          # Docker services configuration
โ”œโ”€โ”€ Dockerfile                  # Application container
โ”œโ”€โ”€ requirements.txt            # Python dependencies
โ”œโ”€โ”€ .env                       # Environment variables
โ”œโ”€โ”€ init.sql                   # PostgreSQL initialization
โ”œโ”€โ”€ migrations/                # Migration files directory
โ””โ”€โ”€ README.md                  # This file

๐Ÿ› ๏ธ API Endpoints

Core Migration Endpoints

Method Endpoint Description
GET /health Health check and database connectivity
GET /migrations/status Get current migration status
GET /migrations/pending List pending migrations
POST /migrations/migrate Apply pending migrations
POST /migrations/rollback Rollback to specific version
POST /migrations/create Create new migration

Demo Endpoints

Method Endpoint Description
POST /demo/setup Create and apply example migrations

๐Ÿ“Š Usage Examples

1. Check Migration Status

curl http://localhost:8000/migrations/status

Response:

{
  "success": true,
  "message": "Migration status retrieved successfully",
  "data": {
    "applied_count": 2,
    "pending_count": 0,
    "last_applied": "20241220_143022",
    "next_pending": null,
    "applied_migrations": [...],
    "pending_migrations": []
  }
}

2. Create a New Migration

curl -X POST http://localhost:8000/migrations/create \
  -H "Content-Type: application/json" \
  -d '{
    "name": "add_user_profile",
    "up_sql": "ALTER TABLE users ADD COLUMN profile_image VARCHAR(500);",
    "down_sql": "ALTER TABLE users DROP COLUMN profile_image;"
  }'

3. Apply Migrations

curl -X POST http://localhost:8000/migrations/migrate \
  -H "Content-Type: application/json" \
  -d '{"target_version": null}'

4. Rollback Migrations

curl -X POST http://localhost:8000/migrations/rollback \
  -H "Content-Type: application/json" \
  -d '{"target_version": "20241220_143022"}'

๐Ÿ”ง Configuration

Environment Variables

Variable Default Description
DATABASE_URL postgresql://migration_user:migration_password@postgres:5432/migration_db PostgreSQL connection string
MIGRATIONS_DIR migrations Directory for migration files
DEBUG True Enable debug mode
HOST 0.0.0.0 API host
PORT 8000 API port

Docker Compose Configuration

The docker-compose.yml includes:

  • PostgreSQL 15 with health checks
  • Application container with hot reload
  • Volume persistence for database data
  • Network isolation between services

๐Ÿ›ก๏ธ Security Features

The fixed version (database_manager_fixed.py) includes:

  1. Parameterized Queries: Prevents SQL injection attacks
  2. Transaction Safety: Proper rollback on failures
  3. Connection Management: Secure connection handling
  4. Input Validation: Pydantic models for request validation

๐Ÿ“ Migration File Format

Migrations are Python files with a specific structure:

"""
Migration: create_users_table
Created: 2024-12-20T14:30:22.123456
"""

from database_manager_fixed import Migration

class CreateUsersTable(Migration):
    def __init__(self):
        super().__init__("20241220_143022", "create_users_table")
        self.up_sql = """
        CREATE TABLE users (
            id SERIAL PRIMARY KEY,
            email VARCHAR(255) UNIQUE NOT NULL,
            name VARCHAR(255) NOT NULL,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        );
        """
        self.down_sql = "DROP TABLE users;"

๐Ÿ”„ Migration Workflow

  1. Create Migration: Use API or manually create migration file
  2. Review: Check generated SQL before applying
  3. Apply: Run migrations in order
  4. Verify: Check database schema and data
  5. Rollback: If needed, rollback to previous version

๐Ÿงช Testing

Manual Testing

# Start services
docker-compose up -d

# Wait for services to be ready
sleep 10

# Test API endpoints
curl http://localhost:8000/health
curl http://localhost:8000/migrations/status
curl -X POST http://localhost:8000/demo/setup

# Check database directly
docker exec -it migration_postgres psql -U migration_user -d migration_db -c "\dt"

Database Connection

# Connect to PostgreSQL container
docker exec -it migration_postgres psql -U migration_user -d migration_db

# Check migration history
SELECT * FROM migration_history;

# Check created tables
\dt

๐Ÿ› Troubleshooting

Common Issues

  1. Connection Refused

    # Check container status
    docker-compose ps
    
    # Check logs
    docker-compose logs postgres
    docker-compose logs app
    
  2. Migration Fails

    # Check migration status
    curl http://localhost:8000/migrations/status
    
    # Check application logs
    docker-compose logs app
    
  3. Database Not Ready

    # Check health
    curl http://localhost:8000/health
    
    # Restart services
    docker-compose restart
    

Reset Database

# Stop services
docker-compose down

# Remove volumes
docker-compose down -v

# Restart clean
docker-compose up --build

๐Ÿ”„ Development

Local Development

# Install dependencies
pip install -r requirements.txt

# Set environment variables
export DATABASE_URL="postgresql://migration_user:migration_password@localhost:5432/migration_db"

# Start PostgreSQL only
docker-compose up postgres -d

# Run application locally
uvicorn main:app --reload

Adding New Features

  1. Extend DatabaseAdapter for new database types
  2. Add new migration types by extending Migration class
  3. Create additional API endpoints in main.py
  4. Update documentation

๐Ÿ“š Advanced Features

Custom Migration Types

from database_manager_fixed import Migration

class DataMigration(Migration):
    """Migration with data transformation"""
    
    async def up_with_data(self, db_adapter):
        # Apply schema changes
        await db_adapter.execute_sql(self.up())
        
        # Transform existing data
        await self.transform_data(db_adapter)
    
    async def transform_data(self, db_adapter):
        # Custom data transformation logic
        pass

Schema Validation

class ValidatedMigration(Migration):
    """Migration with pre/post validation"""
    
    async def validate_pre_migration(self, db_adapter):
        # Validate prerequisites
        return True
    
    async def validate_post_migration(self, db_adapter):
        # Validate results
        return True

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Update documentation
  5. Submit a pull request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • FastAPI for the excellent web framework
  • asyncpg for PostgreSQL async driver
  • Docker for containerization

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

db_migration_manager-1.1.0.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

db_migration_manager-1.1.0-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file db_migration_manager-1.1.0.tar.gz.

File metadata

  • Download URL: db_migration_manager-1.1.0.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for db_migration_manager-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a8059fc675a95007baeab72ab7e2f07c490704b3034284b0e9238462f2f3667a
MD5 d0d4a82f4cc48d03caf1dbe8a27d89be
BLAKE2b-256 6847534d5a2b94a4eeec6095ef781cf340616fd3ee8dd738d229a76617520397

See more details on using hashes here.

File details

Details for the file db_migration_manager-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for db_migration_manager-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c63f88667495166d4307aa1d42132a3408c1cca22967da399157bd516d61aa31
MD5 6abc406598a91ce26ec88c2a833ab2ae
BLAKE2b-256 4853487a5be85fdda1f8d8d8f97c2ff76c98620dc87d7aebb9bef921921db25c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page