DEV Community

Luigui Moreno
Luigui Moreno

Posted on

Laravel upgrade 10 to 11 Modifying Columns change

Acá un script para cuando se actualize Laravel de la version 10 a la 11, este script permite encontrar los archivos a donde esta la columna de una tabla a la que se le han hecho cambios

Here's a script for when Laravel is updated from version 10 to 11. This script allows you to find the files where the column of a table that has undergone changes is located.

<?php

function searchMigrations($directory, $tableName, $columnName) {
    $pattern = '/(?:create|table)\s*\(\s*[\'"]' . preg_quote($tableName, '/') . '[\'"].*?\$table.*?[\'"]' . preg_quote($columnName, '/') . '[\'"\)]/is';
    $migrations = glob($directory . '/*_*.php');
    $matches = [];

    foreach ($migrations as $migration) {
        $content = file_get_contents($migration);
        if (preg_match($pattern, $content)) {
            $matches[] = $migration;
        }
    }

    return $matches;
}

// Verificar si se proporcionaron los argumentos necesarios
if ($argc < 3) {
    echo "Uso: php " . $argv[0] . " <nombre_tabla> <nombre_columna>\n";
    echo "Ejemplo: php " . $argv[0] . " pedidos user_id\n";
    exit(1);
}

$migrationsDir = __DIR__ . '/database/migrations';
$tableName = $argv[1];
$columnName = $argv[2];

echo "Buscando migraciones para la tabla '$tableName' y la columna '$columnName'...\n\n";

$results = searchMigrations($migrationsDir, $tableName, $columnName);

if (empty($results)) {
    echo "No se encontraron migraciones que coincidan con los criterios.\n";
} else {
    foreach ($results as $file) {
        echo realpath($file) . "\n";
    }

    echo "\nTotal de migraciones encontradas: " . count($results) . "\n";
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)