Import database from dump with Doctrine and Symfony

Import database from dump with Doctrine and Symfony

There are tickets where you have to import a database over and over again. For this it's awesome if you have some kind of automation going. Turns out there is one so easy in Symfony that you don't even think of it.

A simple Symfony console command provided with the doctrine bundle:

php bin/console doctrine:database:import dump.sql

If you've got a migration or some other process which creates new tables and you also want them removed, just kill the database and create it again:

php bin/console doctrine:database:drop --force
php bin/console doctrine:database:create
php bin/console doctrine:database:import dump.sql

And of course you can shorten them and concat them to a one-liner as with all Symfony commands to for example:

php bin/console doc:dat:dro --force && php bin/console doc:dat:cre && php bin/console doc:dat:imp dump.sql

If you don't always have a current dump, just add the migrations command to the list:

php bin/console doctrine:migrations:migrate --no-interaction