🎉 Enjoying this package? Consider sponsoring me on GitHub or buying me a beer.
Metadata & Advanced Features
Laravel Patches automatically tracks comprehensive metadata about patch execution, providing valuable insights for monitoring and debugging.
Tracked Metadata
Every patch execution automatically captures:
- execution_time_ms - Duration in milliseconds
- memory_used_mb - Peak memory usage
- executed_by - User or process that ran the patch
- environment - Application environment (production, staging, local)
- status - Execution status (success, failed, rolled_back)
- error_message - Exception message if failed
- error_trace - Full stack trace if failed
- log - Custom log messages from
$this->log() - ran_on - Timestamp of execution
Configuration
Control metadata tracking in config/laravel-patches.php:
1return [2 'track_metadata' => env('PATCHES_TRACK_METADATA', true),3 'track_memory' => env('PATCHES_TRACK_MEMORY', true),4 'track_user' => env('PATCHES_TRACK_USER', true),5];
Viewing Metadata
Via Commands
1# See all metadata in table view2php artisan patch:status3 4# Get JSON output for processing5php artisan patch:list --json
Via Database
1use Rappasoft\LaravelPatches\Models\Patch;2 3$patches = Patch::where('status', 'success')4 ->orderBy('execution_time_ms', 'desc')5 ->get();6 7foreach ($patches as $patch) {8 echo "{$patch->patch}: {$patch->execution_time_ms}ms\n";9}
Via Events
1use Rappasoft\LaravelPatches\Events\PatchExecuted;2 3Event::listen(PatchExecuted::class, function ($event) {4 Log::info('Patch Metrics', [5 'patch' => $event->patch,6 'time_ms' => $event->executionTime,7 'memory_mb' => $event->memoryUsed,8 ]);9});
Patch Descriptions
Add descriptions to your patches for better documentation:
1use Rappasoft\LaravelPatches\Patch; 2 3class UpdateUserEmails extends Patch 4{ 5 public function description(): ?string 6 { 7 return 'Updates all user emails from old domain to new domain'; 8 } 9 10 public function up()11 {12 // Patch logic13 }14 15 public function down()16 {17 // Rollback logic18 }19}
Descriptions appear in:
patch:statuscommand output- Event payloads
- Error messages
Performance Analysis
Find Slow Patches
1$slowPatches = Patch::where('execution_time_ms', '>', 5000)2 ->orderByDesc('execution_time_ms')3 ->get();
Memory-Heavy Patches
1$memoryHeavy = Patch::where('memory_used_mb', '>', 100)2 ->get();
Failure Rate
1$totalPatches = Patch::count();2$failedPatches = Patch::where('status', 'failed')->count();3$failureRate = ($failedPatches / $totalPatches) * 100;
Monitoring Integration
Send Metrics to External Service
1Event::listen(PatchExecuted::class, function ($event) {2 Metrics::timing('patch.execution', $event->executionTime, [3 'patch' => $event->patch,4 'environment' => app()->environment(),5 ]);6 7 Metrics::gauge('patch.memory', $event->memoryUsed);8});
Alert on Anomalies
1Event::listen(PatchExecuted::class, function ($event) {2 $avgTime = Patch::where('patch', $event->patch)3 ->avg('execution_time_ms');4 5 if ($event->executionTime > ($avgTime * 2)) {6 Slack::send("⚠️ Slow patch detected: {$event->patch}");7 }8});
Best Practices
- Review metadata regularly to identify performance issues
- Add descriptions to all production patches
- Monitor execution times for degradation
- Set up alerts for failed patches
- Keep metadata enabled in production for troubleshooting