What is “database migration”
I meet migrations first time in Ruby on Rail framework. The idea is simple. Programmers can write scripts with the database schema. Each migration can update the database, for example, create the new table or remove a column in the table. I use in “Image optimizer API” library called “Phinx”, but you can find another solution.
Why you should use PHP database migration tool?
1. Easy deploy. You can run migration in deploy process. You can select databank (develop/test/production) too.
2. You can quick remove your changes.
3. Better teamwork. Each programmer has identical database.
4. Save your time. You don’t have to import and export database schema never again.
“Phinx” library can you install by composer:
php composer.phar require robmorgan/phinx
Next must you create folder “db/migrations”. In this folder can you find migration scripts.
To init “Phinx” you must run in console:
Before you create your first migration, you must add database access data to “phinx.xml” file.
And now can you create you first migration:
php vendor/bin/phinx create MyNewMigration
You can see how to look my migration to create user table in “Image optimizer API”:
To execute your code and migrate to developments database you should use the command:
phinx migrate -e development
That’s it! Your table was created.
If you want to remove your changes, should you use rollback command:
phinx rollback -e development
More command can you find in the documentation: