PHP Laravel

Battling Ghost Migrations: A Midnight Cleanup on the Platform

Working on the Reimpact platform often involves juggling multiple moving pieces. Recently, we encountered a peculiar issue: the lingering presence of duplicate migration files.

The Phantom Menace

Our deployment process began failing intermittently. After some digging, we discovered the root cause: duplicate migration files for key components like warehouses, brands, and massive uploads. These weren't actual duplicates in the codebase, but rather, older migration files that had somehow persisted through previous deployments, causing conflicts during schema updates.

The Hunt

The initial approach was to manually inspect each server and remove the offending files. However, this was time-consuming and prone to error. We needed a more systematic solution. We focused on identifying the source of the problem, realizing that our deployment scripts were not properly cleaning up old migration files after each run.

The Solution

We modified our deployment scripts to include a cleanup step. This step ensures that only the latest migration files are present before running the php artisan migrate command. Here's a simplified example of how we implemented this:

// Deployment script snippet

// Remove old migration files
$files = glob(database_path('migrations/*'));
foreach ($files as $file) {
    if (strpos($file, 'warehouse') !== false ||
        strpos($file, 'brand') !== false ||
        strpos($file, 'massive_upload') !== false) {
        unlink($file);
    }
}

// Run migrations
Artisan::call('migrate', ['--force' => true]);

Note: This example provides a simplified illustration. In a real-world scenario, a more robust and generic solution should be implemented.

The Aftermath

After implementing the cleanup script, the deployment errors vanished. The platform now deploys smoothly, and we've gained a valuable lesson in the importance of proper cleanup procedures. By ensuring that old migration files are removed, we prevent potential conflicts and maintain a clean database schema.

The Takeaway

Always implement robust cleanup procedures in your deployment scripts. Failing to do so can lead to unexpected issues, such as duplicate migration conflicts. Regularly review your deployment processes to identify and address potential cleanup gaps.

Battling Ghost Migrations: A Midnight Cleanup on the Platform
GERARDO RUIZ

GERARDO RUIZ

Author

Share: