After you have set up migrations for your database you can start using the migration tools to generate diffs of your current and your changed schema or create your own migration classes to achieve smooth updates between your schema versions. This chapter will show you how to use migrations in your everyday development cycle.
Imagine you have set up a database with a User
table which holds a fullname
column.
User:
actAs:
Timestampable: ~
columns:
full_name:
type: string(255)
notnull: true
email:
type: string(255)
notnull: true
After some time you decide that using fullname
is not the best solution, since you want to greet users by just their first names and not the full ones. Changing from a single fullname
column to separate first_name
and last_name
columns is needed. To achieve this first you change your schema.yml
file to the following as you would normally do.
User:
actAs:
Timestampable: ~
columns:
first_name:
type: string(255)
notnull: true
last_name:
type: string(255)
notnull: true
email:
type: string(255)
notnull: true
If you now want to apply these changes to your database you can use the doctrine-migrations-diff
task to generate the differences between the current and the changed schema. These changes are converted by Doctrine into a migrations file which will be applied to your database the next time you migrate it to a higher version.
$ php symfony doctrine:generate-migrations-diff
To apply the migrations you have just generated you will have to use the doctrine:migrate
task. When called without any parameters this task will scan all available migration classes and update your database to the latest version of the schema.
$ php symfony doctrine:migrate
$ php symfony doctrine:build --all-classes
Note that you still have to run the doctrine:build
task with the --all-classes
switch. This is because your model files are not updated by the doctrine:migrate
task. But, as always in symfony, there is a shorthand way to do this.
$ php symfony doctrine:build --all-classes --and-migrate
When the doctrine:build
task is given the switch --and-migrate
it migrates your database to the latest version along with any other operations performed by the task. In this way you can update both your model files and the tables in your database.