I am setting up a test environment for unit tests, following Symfony's Test-Guide. However each time I deploy the test-DB the final tables look slightly different than the originals from the production db.
I use migration files to build up the production-db. Maybe I am doing the test-db setup in wrong order regarding the production db setup?
Step | Action | Command |
---|---|---|
1 | Remove all databases | mysql -e "DROP DATABASE prod_db" //... |
2 | Add production db | mysql -e "CREATE DATABASE prod_db" |
3 | Execute migrations for production db | php bin/console doctrine:migrations:migrate |
4 | Add test-db | php bin/console --env=test doctrine:database:create |
5 | Create test-db schema | php bin/console --env=test doctrine:schema:create |
Q: What is the cause of the differences?
The simplest answer is that the schemas do, in fact, differ: While doctrine:schema:create
generates the required DDL on the fly against your entities, the doctrine:migrations:migrate
executes a series of SQL sentences that you (probably with the help of the make:migration
command) previously defined.
It's all to easy to do a small change to your entities and forget to create the corresponding migration.
To check what's different in production (i.e. what you forgot to add to the migrations) you can either:
bin/console make:migration
. This will create a new migration with the missing sentences.bin/console doctrine:schema:update --dump-sql
.TL;DR To just make your test database match the production one, use the following command in place of your 5th command:
bin/console doctrine:migrations:migrate -e test
This will load the same SQL, making them match.
Bonus: you can also truncate your databases using symfony commands: in the event that you change your database engine, doctrine will take care of it (add the -e
flag as needed):
bin/console doctrine:database:drop --force && bin/console doctrine:database:create