Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sgupta1979's avatar

How we can convert Symfony migration scripts to laravel

Hello

I am having web application build in Symfony 3.4. I am migrating application to Laravel 10.

I am having more than 100 Symfony migration scripts which executes thorugh backend process on an event. I would like to convert all those migration scripts into Laravel 10 migration scripts.

Converting all those manually is a tidious job. Is there any simplest way?

Thanks in advance

0 likes
1 reply
LaryAI's avatar
Level 58

Converting Symfony migration scripts to Laravel migration scripts is not a straightforward task because the two frameworks use different formats and conventions for migrations. However, I can provide you with a general approach to automate the process as much as possible.

  1. Understand the Differences: First, you need to understand the differences between Symfony's Doctrine migrations and Laravel's migrations. Laravel migrations are typically more declarative, using a fluent syntax to define table structures, while Doctrine migrations might contain more imperative code.

  2. Extract SQL Statements: If your Symfony migrations are mostly executing raw SQL statements, you can extract these statements and use them directly within Laravel's DB::statement() method inside your Laravel migration files.

  3. Create a Conversion Script: You can write a PHP script that reads the Symfony migration files, extracts the relevant parts, and generates Laravel migration files. This script would:

    • Read the contents of each Symfony migration file.
    • Parse the file to extract the SQL or the operations being performed.
    • Translate these operations into Laravel's migration syntax.
    • Write the output to a new Laravel migration file.

Here's a very simplified example of what such a script might look like:

<?php

$symfonyMigrationsDirectory = '/path/to/symfony/migrations';
$laravelMigrationsDirectory = '/path/to/laravel/database/migrations';

foreach (new DirectoryIterator($symfonyMigrationsDirectory) as $file) {
    if ($file->isDot() || !$file->isFile()) {
        continue;
    }

    $symfonyMigrationContent = file_get_contents($file->getPathname());
    
    // This is where you would parse the Symfony migration content and convert it
    // For the sake of this example, let's assume you just want to extract SQL statements
    preg_match_all('/$this->addSql\((.*?)\);/s', $symfonyMigrationContent, $matches);
    
    $laravelMigrationContent = "<?php\n\nuse Illuminate\Database\Migrations\Migration;\nuse Illuminate\Database\Schema\Blueprint;\nuse Illuminate\Support\Facades\Schema;\nuse Illuminate\Support\Facades\DB;\n\nclass ConvertedMigration extends Migration\n{\n    public function up()\n    {\n";

    foreach ($matches[1] as $sql) {
        // Clean up the SQL statement
        $sql = str_replace(["\n", "'"], ["\n", "\'"], $sql);
        $laravelMigrationContent .= "        DB::statement('$sql');\n";
    }

    $laravelMigrationContent .= "    }\n\n    public function down()\n    {\n        // You should also handle the reverse operations if possible\n    }\n}\n";

    // Generate a unique filename for the Laravel migration
    $timestamp = date('Y_m_d_His');
    $laravelFilename = $timestamp . '_converted_migration.php';

    file_put_contents($laravelMigrationsDirectory . '/' . $laravelFilename, $laravelMigrationContent);
    
    // Update the timestamp to avoid filename collision
    sleep(1);
}
  1. Manual Review and Adjustment: After running your conversion script, you will need to manually review and adjust the generated Laravel migrations. This is necessary because some operations may not convert cleanly, and you need to ensure that the migrations are idempotent and reversible if possible.

  2. Testing: Test your new Laravel migrations in a safe environment to ensure they perform the expected database schema changes without issues.

Remember, this is a high-level approach and the actual implementation can be quite complex depending on the specifics of your Symfony migrations. You may need to handle different types of operations (e.g., creating tables, adding indexes, foreign keys, etc.) and convert them to the equivalent Laravel operations. The script provided is a starting point and will likely require significant expansion to handle all the cases in your migrations.

Please or to participate in this conversation.