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

Database Schema Migrations with Doctrine

Database Schema Migrations with Doctrine

Diego Sapriza

April 29, 2014
Tweet

More Decks by Diego Sapriza

Other Decks in Technology

Transcript

  1. Database Schema Migrations with Doctrine #NomadPHP #lightningTalk Doctrine 2

  2. Diego Sapriza Associate Director of Web Applications PHPer ~ DevOps

  3. http://AV4TAr.com PHP.meetup.uy   DevOps.meetup.uy . .uy @AV4TAr

  4. Database Schema Migrations

  5. code versioning? and your schema?

  6. Classic workflow 1. production dump 2. change code & schema

    3. overwrite production
  7. ¿how about intermediate changes? in the database

  8. schema synchronization

  9. MySQL Workbench

  10. scripting undo changes from latest script? ¿rollback? IF  NOT  EXISTS

      …   …   …
  11. …  count(devs)  >  1

  12. centralized dev DB

  13. …  count(devs)  >  1   AND   env=‘distributed’

  14. different DB versions

  15. continuous Deployment code and schema not in sync

  16. management of incremental, reversible changes to relational database schemas. http://en.wikipedia.org/wiki/Schema_migration

  17. None
  18. create migration file edit - up() & down() execute migration

    commit & push
  19. pull your code execute migrations continue coding…

  20. Doctrine Migrations http://bit.ly/doctrine-migrations

  21. download “.phar” https://github.com/doctrine/migrations

  22. migrations.yml http://doctrine-migrations.readthedocs.org/en/latest/reference/introduction.html#configuration

  23. migrations-db.php http://doctrine-migrations.readthedocs.org/en/latest/reference/introduction.html#php-binary-phar

  24. None
  25. $ php doctrine-migrations.phar --configuration="configuration.yml"

  26. new migration migrations:generate

  27. None
  28. None
  29. None
  30. migration status migrations:status

  31. test a migration migrations:migrate —dry-run

  32. execute migration migrations:migrate

  33. migrations:migrate

  34. how doctrine knows?

  35. state

  36. state

  37. 0 1 2 3 4 rollback migrations:migrate #version

  38. rollback migrations:migrate #version

  39. Doctrine Migrations http://bit.ly/doctrine-migrations

  40. Tips

  41. avoid complex rollbacks … its easier to restore a backup

  42. “rollbacks does not exist, we always go forward” - @nachexnachex

  43. 0 1 2 3 4 rollback migrations:migrate 0

  44. decouple code deploys from database migrations from to deploy &

    migrate deploy 1 deploy 2 migrate https://speakerdeck.com/nduthoit/the-path-to-smoother-database-migrations
  45. deploy 1 deploy 2 migrate code works before and after

    migration code depends on migration being completed https://speakerdeck.com/nduthoit/the-path-to-smoother-database-migrations decouple code deploys from database migrations
  46. + tips migration-agnostic code add model - don’t use it-

    migrate schema - use model.
  47. + tips • nullable FK -> migrate data -> migrate

    code • test migrations in production backups
  48. backup your data before schema migration ;)

  49. @AV4TAr http://AV4TAr.com irc freenode #AV4TAr https://joind.in/13400

  50. Resources • http://en.wikipedia.org/wiki/Schema_migration • http://phinx.org • https://github.com/doctrine/migrations • https://speakerdeck.com/austinsmorris/doctrine-migrations •

    https://speakerdeck.com/nduthoit/the-path-to-smoother- database-migrations • https://speakerdeck.com/trumpi/database-migrations • https://speakerdeck.com/austinsmorris/doctrine-migrations