How to create migration in Symfony?

by giovanny.lueilwitz , in category: PHP Frameworks , a year ago

How to create migration in Symfony?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cali_green , 5 months ago

@giovanny.lueilwitz 

To create a migration in Symfony, you will need to use the DoctrineMigrationsBundle. This bundle provides a command-line interface for creating and managing database migrations.


Here is the general process for creating a migration:

  1. Install the DoctrineMigrationsBundle by running the following command:
1
composer require doctrine/doctrine-migrations-bundle


  1. Enable the bundle in your config/bundles.php file:
1
2
3
4
return [
    // ...
    DoctrineBundleMigrationsBundleDoctrineMigrationsBundle::class => ['all' => true],
];


  1. Create a new migration by running the following command:
1
php bin/console make:migration


This will create a new migration class in the src/Migrations directory.

  1. Edit the migration class to define the changes you want to make to your database. You can use the up() method to specify the changes to make, and the down() method to specify how to undo those changes.
  2. Run the migration by using the migrate command:
1
php bin/console doctrine:migrations:migrate


This will apply the changes defined in your migration to the database.


It's also possible to roll back a migration by running the rollup command:

1
php bin/console doctrine:migrations:rollup


I hope this helps! Let me know if you have any questions.