Database
Migrations
in PHP
Posobota #66
Vladimír Kriška
@ujovlado
Slide 2
Slide 2 text
My experience
• Paradox: first company I worked with
was the closest to migrations
• Then it went only worse
• no migrations
• single file migrations
• Yii framework database migrations
Slide 3
Slide 3 text
Pros
• Exact version of schema with
creation and application date
• CLI interface (automation)
• Easy rollback, going through history
Slide 4
Slide 4 text
Cons
• Need to regenerate/change model
Slide 5
Slide 5 text
Other options/ideas
• Generate migrations directly from
model (no SQL writing)
• With each change create DB dump with
file per table, than version in Git
Slide 6
Slide 6 text
Other tools
• Phinx
• Sqitch (Perl)
• Doctrine, Phalcon, Propel, CakePHP
• and many others …
Slide 7
Slide 7 text
Migrations in Yii
• Two main commands
• yiic migrate up
• yiic migrate down
• And also: history, new, to, mark
Slide 8
Slide 8 text
Example
• let’s do some shit …
Slide 9
Slide 9 text
Tips/tricks
• DB schema has to be versioned - no
matter how
• If you don’t know how to do it,
choose solution from some framework
• Don’t write own tool for migrations
• Start with migrations ASAP
Slide 10
Slide 10 text
• Always write down migrations
• Try to avoid shortcut methods (they
disallow c&p from migration)
• Don’t mix schema and data migrations
• Write short migrations
• Don’t change applied migrations