Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Database Migrations in PHP (Posobota #66)

Database Migrations in PHP (Posobota #66)

Vladimír Kriška

October 25, 2014
Tweet

More Decks by Vladimír Kriška

Other Decks in Programming

Transcript

  1. Database
    Migrations
    in PHP
    Posobota #66
    Vladimír Kriška
    @ujovlado

    View full-size slide

  2. 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

    View full-size slide

  3. Pros
    • Exact version of schema with
    creation and application date
    • CLI interface (automation)
    • Easy rollback, going through history

    View full-size slide

  4. Cons
    • Need to regenerate/change model

    View full-size slide

  5. 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

    View full-size slide

  6. Other tools
    • Phinx
    • Sqitch (Perl)
    • Doctrine, Phalcon, Propel, CakePHP
    • and many others …

    View full-size slide

  7. Migrations in Yii
    • Two main commands
    • yiic migrate up
    • yiic migrate down
    • And also: history, new, to, mark

    View full-size slide

  8. Example
    • let’s do some shit …

    View full-size slide

  9. 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

    View full-size slide

  10. • 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

    View full-size slide

  11. THE END
    Thanks!

    View full-size slide