🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
4.1 KiB
Executable File
4.1 KiB
Executable File
LLM-Friendly Migration System - Automatic Snapshot Protection
Problem Statement
LLMs struggle with partial migration failures because:
- MySQL DDL operations auto-commit and cannot be rolled back
- Partial migrations leave the database in an inconsistent state
- LLMs get confused about what has/hasn't been applied
- Recovery requires manual intervention or database reset
Implemented Solution: Automatic Docker Snapshot System
Core Design
The php artisan migrate command automatically handles database snapshots in development mode. No separate commands needed.
How It Works
-
Development Mode (
RSX_MODE=development)- Automatically creates MySQL snapshot before migrations
- Runs all pending migrations with validation
- On success: commits changes, regenerates constants, recompiles bundles
- On failure: automatically rolls back to snapshot
-
Debug/Production Mode (
RSX_MODE=debugorproduction)- No snapshot protection (source code is read-only)
- Migrations run directly
- Schema normalization still runs
- Constants and bundles NOT regenerated
Migration Flow
# Development - just one command does everything
$ php artisan migrate
Development mode: Using automatic snapshot protection
[1/4] Creating database snapshot...
[OK] Snapshot created successfully.
[2/4] Running migrations...
[OK] All 3 migrations completed successfully
[3/4] Running schema quality check...
[OK] Schema quality check passed.
[4/4] Committing changes...
[OK] Snapshot committed - backup removed.
Running post-migration tasks...
[OK] Migration completed successfully!
Automatic Recovery on Failure
$ php artisan migrate
Development mode: Using automatic snapshot protection
[1/4] Creating database snapshot...
[OK] Snapshot created successfully.
[2/4] Running migrations...
[ERROR] Migration failed!
Automatically rolling back to snapshot...
[OK] Database restored to pre-migration state.
Fix your migration files and run "php artisan migrate" again.
Implementation Details
The unified migrate command in development mode:
- Stops MySQL via supervisord
- Creates backup of
/var/lib/mysqlto/var/lib/mysql_backup - Restarts MySQL and runs migrations
- On success: removes backup, regenerates constants, recompiles bundles
- On failure: restores backup, removes flag file
Environment Detection
The system automatically detects the environment:
-
Development Mode (snapshots enabled):
RSX_MODE=developmentin.env- Docker environment detected (
/.dockerenvexists)
-
Debug/Production Mode (no snapshots):
RSX_MODE=debugorRSX_MODE=production- OR not running in Docker
Benefits for LLMs
-
Zero-Step Recovery
- Failed migrations automatically restore database
- No commands to remember
- Just fix migration files and run
migrateagain
-
Safe Experimentation
- LLMs can try migrations without fear
- Complete rollback always available
- Learn from failures without consequences
-
Simple Mental Model
- One command:
php artisan migrate - System handles all the complexity
- Clear success/failure messaging
- One command:
-
Production Safety
- System automatically adapts to environment
- Source code treated as read-only in debug/production
- Clear separation of dev/prod behaviors
Limitations
- Docker Only: Snapshots require Docker environment with supervisord
- Development Only: Not available in debug/production modes
- Disk Space: Requires space for MySQL data directory copy
Implementation Files
/app/RSpade/Commands/Migrate/Maint_Migrate.php- Unified migrate command/app/Http/Middleware/CheckMigrationMode.php- Web UI protection during migration
Why This Approach
This implementation was chosen because:
- Complete Safety: Filesystem snapshots guarantee perfect rollback
- Zero Friction: Single command handles everything
- LLM-Friendly: No multi-step workflow to remember
- Production Compatible: Gracefully adapts to environment
- Laravel Integration: Works with existing migration system