I have the following migrations structure within the Doctrine package:
- Version 1
- Version 2
- Version 3 (custom)
- Version 4
Version 3 is not a regular migration because it requires some code calculations and depends on the previous migrations. So it should be executed separately after version 2 and before version 4.
I tried to do the following in my CI/CD script:
bin/console doctrine:migrations:migrate Version2 # prepare
bin/console doctrine:migrations:migrate Version3 # run custom migration
bin/console doctrine:migrations:migrate # run the rest
This works well on the first run. But on the second run, the command doctrine:migrations:migrate Version2
rolls back everything to the version 2 which is definitely not what I need. What I need is to make commands doctrine:migrations:migrate Version2
and doctrine:migrations:migrate Version3
do nothing if newer migrations are already applied. How can I achieve that?
My Answer:
One way to prevent the `doctrine:migrations:migrate` command from rolling back if an older version was specified is to use the `--dry-run` option. This option will show you the SQL queries that would be executed without actually applying them.
You can modify your CI/CD script to first check if the migration version has already been applied before running the migration. Here's an example of how you can achieve this:
bash
# Check if Version2 migration needs to be applied
if ! bin/console doctrine:migrations:status --show-versions | grep -q Version2; then
bin/console doctrine:migrations:migrate Version2
fi
# Check if Version3 migration needs to be applied
if ! bin/console doctrine:migrations:status --show-versions | grep -q Version3; then
bin/console doctrine:migrations:migrate Version3
fi
# Run the rest of the migrations
bin/console doctrine:migrations:migrate
This script will only run the Version2 and Version3 migrations if they have not already been applied. This way, you can prevent the `doctrine:migrations:migrate` command from rolling back if an older version was specified.
Rate this post
4 of 5 based on 6089 votesComments