CI/CD: Add automatic database migration support

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 <noreply@anthropic.com>
This commit is contained in:
SysAdmin 2025-10-04 14:47:24 +01:00
parent d8dcaa51c9
commit 97c93e43ab

View File

@ -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