From 97c93e43ab49c1a063b3a011c71e855f13c2d959 Mon Sep 17 00:00:00 2001 From: SysAdmin Date: Sat, 4 Oct 2025 14:47:24 +0100 Subject: [PATCH] CI/CD: Add automatic database migration support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prevents future deployment failures by automatically applying database schema migrations during deployment process. Changes: - Added migration step that runs AFTER stopping containers - Automatically detects .sql files in LittleShop/Migrations/ - Creates timestamped backup before applying each migration - Applies migrations using sqlite3 in Alpine container - Properly handles volume mounting for littleshop_littleshop_data This prevents issues like the October 4 incident where ProductVariant schema changes were deployed without updating the database, causing complete system outage. Migration workflow: 1. Stop all containers 2. Check for migration files 3. Create database backup 4. Apply migrations 5. Start containers with updated schema 🤖 Generated with Claude Code https://claude.com/claude-code Co-Authored-By: Claude --- .gitlab-ci.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c6974ab..13cd4e8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -102,6 +102,26 @@ deploy:vps: echo "Cleaning up Docker networks..." docker network prune -f || true + # Apply database migrations if they exist + echo "Checking for database migrations..." + if [ -d "LittleShop/Migrations" ] && [ -n "\$(ls -A LittleShop/Migrations/*.sql 2>/dev/null)" ]; then + echo "Found migration files, applying to database..." + for migration in LittleShop/Migrations/*.sql; do + migration_name=\$(basename "\$migration") + echo "Applying migration: \$migration_name" + docker run --rm -v littleshop_littleshop_data:/data -v \$(pwd)/LittleShop/Migrations:/migrations alpine:latest sh -c " + apk add --no-cache sqlite > /dev/null 2>&1 + echo 'Creating backup before migration...' + cp /data/littleshop-production.db /data/littleshop-production.db.backup-\\\$(date +%Y%m%d-%H%M%S) 2>/dev/null || true + echo 'Applying migration: \$migration_name' + sqlite3 /data/littleshop-production.db < /migrations/\$migration_name + echo 'Migration applied successfully' + " + done + else + echo "No migration files found, skipping..." + fi + # Start services with new images echo "Starting services with new images..." docker-compose up -d