I'm using Deployer to deploy my Symfony application to production. All is working fine, however, my deploy.php file has the command for database migrations:
/**
* Run a diff to generate migrations,
*/
task('database:diff', function () {
run('{{bin/php}} {{release_path}}/' . trim(get('bin_dir'), '/') . '/console doctrine:migrations:diff --env={{env}} --no-debug --no-interaction');
})->desc('Generate migrations database');
...
after('deploy:symlink','database:diff');
after('database:diff','database:migrate');
But if there are no changes to the database then this step causes an error.
Error message:
[RuntimeException]
[Doctrine\DBAL\Migrations\MigrationException]
Could not find any migrations to execute.
I'm looking for some code that can check for any database change and only conditionally run the migrations tasks if there are changes.
Well, this is strange approach to automate migrations and database versioning.
That's how we do that:
Every developer generates their migrations;
$ php app/console doctrine:migrations:diff
migration is created locally.During deployment, there is no need to run diff, because all diffs are in git repository, so you just run database:migrate
to update you prod database to a latest version.
Hint: When generating migrations, it is good practice to generate one migration per entity/database table, as if some sql queries would fail in your big migration, it would be difficult to rollback these who have been executed already.