How to perform database migrations in Symfony?

Database migrations in Symfony can be performed using Doctrine Migrations Bundle. Here is a step-by-step guide on how to perform database migrations in Symfony:

  1. Install Doctrine Migrations Bundle by running the following command in your Symfony project directory:

    composer require doctrine/doctrine-migrations-bundle
  2. Enable the bundle in your config/bundles.php file by adding the following line:

    Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
  3. Create a new migration class by running the following command:

    php bin/console make:migration
  4. Update the generated migration class in the src/Migrations/VersionXXXXXXXXXXXXXX.php file. Add your schema changes in the up and down methods of the migration class.

  5. Run the migration to apply the changes to the database by executing the following command:

    php bin/console doctrine:migrations:migrate
  6. To roll back a migration, you can run the following command:

    php bin/console doctrine:migrations:migrate prev
  7. You can view the status of migrations by running the following command:

    php bin/console doctrine:migrations:status

By following these steps, you can easily perform database migrations in Symfony using Doctrine Migrations Bundle.