• info@bizmate.biz

Checking and rolling back migrations with doctrine.

Checking and rolling back migrations with doctrine.

This is a quick note on how to work with doctrine migrations

When a change in the mapping is done you can run

I have no name!@7efd2339724b:/var/www/html$ bin/console doctrine:schema:validate

Mapping
-------


[OK] The mapping files are correct. 


Database
--------


[ERROR] The database schema is not in sync with the current mapping file.

This will show if the mapping files are correct and if indeed the mapping matches the current schema status.

If the status is not in sync then you might need to update, run or generate a migration.

To generate a migration run…

I have no name!@7efd2339724b:/var/www/html$ bin/console doctrine:migrations:diff 
Generated new migration class to "/var/www/html/src/Infrastructure/Persistence/Doctrine/Migrations/Version20231127094706.php"

This new migration will have the new definitions for the schema to upgrate the state to the current state. It is generally a good practice to review it.

However if you want to rollback and run a specific migration this can be done by first checking the migrations status …

I have no name!@7efd2339724b:/var/www/html$ bin/console doctrine:migrations:status
+----------------------+------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------+
| Configuration |
+----------------------+------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------+
| Storage | Type | Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration |
| | Table Name | doctrine_migration_versions |
| | Column Name | version |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Database | Driver | Symfony\Bridge\Doctrine\Middleware\Debug\Driver |
| | Name | devdb |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Versions | Previous | 0 |
| | Current | MyApp\YelpBundle\Infrastructure\Persistence\Doctrine\Migrations\Version20230605164531 |
| | Next | MyRApp\ReviewsConfigBundle\Infrastructure\Persistence\Doctrine\Migrations\Version20230118085703 |
| | Latest | MyApp\YelpBundle\Infrastructure\Persistence\Doctrine\Migrations\Version20230605164531 |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Migrations | Executed | 1 |
| | Executed Unavailable | 0 |
| | Available | 2 |
| | New | 1 |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Migration Namespaces | MyApp\ReviewsConfigBundle\Infrastructure\Persistence\Doctrine\Migrations | /var/www/html/src/Infrastructure/Persistence/Doctrine/Migrations |
| | MyApp\YelpBundle\Infrastructure\Persistence\Doctrine\Migrations | /var/www/html/vendor/bizmate/myreviews-yelp-reviews/src/Infrastructure/Persistence/Doctrine/Migrations |

Then you can rollback the migrations by notice in the command line you will need to escape the backward slashes or the migration will not be identified.

I have no name!@7efd2339724b:/var/www/html$ bin/console doctrine:migrations:execute MyApp\\ReviewsConfigBundle\\Infrastructure\\Persistence\\Doctrine\\Migrations\\Version20230118085703 --down

WARNING! You are about to execute a migration in database "devdb" that could result in schema changes and data loss. Are you sure you wish to continue? (yes/no) [yes]:
> yes

[notice] Executing MyApp\ReviewsConfigBundle\Infrastructure\Persistence\Doctrine\Migrations\Version20230118085703 down
[notice] finished in 46.2ms, used 18M memory, 1 migrations executed, 23 sql queries

[OK] Successfully migrated version(s): 
MyApp\ReviewsConfigBundle\Infrastructure\Persistence\Doctrine\Migrations\Version20230118085703: [DOWN]

NOTICE if multiple bundles are registered, to keep track of the other bundles migrations status.

 

Bizmate