Using Doctrine Migrations

                            by Dennis Benkert
Dennis Who?!




               2
Dennis Who?!




               2
Dennis Who?!




               2
Dennis Who?!




               2
Dennis Who?!




               2
Agenda




         3
Agenda


         Migrating by Hand




                             3
Agenda


         Migrating by Hand
         Doctrine Migrations




                               3
Agenda


          Migrating by Hand
         Doctrine Migrations
         Migrations and sf CLI




                     ...
Agenda


           Migrating by Hand
          Doctrine Migrations
         Migrations and sf CLI
         Things to keep...
So, you want to change your live
   servers DB schema, right?!



                                   4
But migrating your database by hand is hard work
                                                   5
Migrating by Hand


                    6
Migrating by Hand


schema.yml




                    Live DB



  Dev DB
                              7
Migrating by Hand


schema.yml




                    Live DB



  Dev DB
                              7
Migrating by Hand


schema.yml




                    Live DB



  Dev DB
                              7
Migrating by Hand


schema.yml




                    Live DB



  Dev DB
                              7
Migrating by Hand


schema.yml




                    Live DB



  Dev DB
                              7
Migrating by Hand


schema.yml




                    Live DB



  Dev DB
                              7
What‘s so bad about it?




                          8
What‘s so bad about it?


            Error Prone




                          8
What‘s so bad about it?


            Error Prone
            No Rollback




                          8
What‘s so bad about it?


            Error Prone
           No Rollback
          Not Comfortable



                    ...
Wouldn‘t it be cool if database changes were a bit
                               like SCM revisions?




                ...
Doctrine Migrations


                      10
Migration Process




schema.yml

                    Migration File
                                     11
Migration Process




schema.yml

                    Migration File
                                     11
Migration Process




schema.yml

                    Migration File
                                     11
Migration Process




             Compare



schema.yml

                       Migration File
                          ...
Migration Process




             Compare



schema.yml

                       Migration File
                          ...
Migration Process




             Compare

                   Generate
schema.yml

                              Migratio...
Migration Process




             Compare

                   Generate
schema.yml

                              Migratio...
Migration Process




             Compare

                   Generate
schema.yml

                              Migratio...
Migration Process



                          Migrate

             Compare

                   Generate
schema.yml

    ...
Migration Files


 class VersionXX extends Doctrine_Migration_Base
 {
     public function up()
     {
         $this->add...
Migration Files


 class VersionXX extends Doctrine_Migration_Base
 {
     public function up()
     {
         $this->add...
Migration Files
     Used for upgrading

 class VersionXX extends Doctrine_Migration_Base
 {
     public function up()
   ...
Migration Files
     Used for upgrading

 class VersionXX extends Doctrine_Migration_Base
 {
     public function up()
   ...
Migration Files
     Used for upgrading

 class VersionXX extends Doctrine_Migration_Base
 {
     public function up()
   ...
Migration Files
     Used for upgrading

 class VersionXX extends Doctrine_Migration_Base
 {
     public function up()
   ...
Migration Files
     Used for upgrading
                                         Table / Data Manipulation
 class VersionX...
How Migrations work

          migration_version

             version (int)




                              13
How Migrations work

          migration_version

             version (int)




                              13
How Migrations work

          migration_version

             version (int)




              Current version stored
    ...
How Migrations work


    4


   3


       2

           1          14
How Migrations work


    4


   3


       2

           1          14
How Migrations work


    4          getVersion



   3


       2

           1                14
How Migrations work


    4          getVersion



   3


       2

           1                14
How Migrations work


    4          getVersion

                  Version =1

   3


       2

           1              ...
How Migrations work


    4          getVersion

                  Version =1

   3


       2

           1              ...
How Migrations work


    4             getVersion

                     Version =1

   3
           up()

       2

     ...
How Migrations work


    4             getVersion

                     Version =1

   3
           up()

       2

     ...
How Migrations work


    4             getVersion

           up()      Version =1

   3
           up()

       2

     ...
How Migrations work


    4             getVersion

           up()      Version =1

   3
           up()

       2

     ...
How Migrations work

             up()   getVersion
    4

           up()        Version =1

   3
           up()

      ...
How Migrations work

             up()   getVersion
    4

           up()        Version =1

   3
           up()

      ...
How Migrations work

             up()   getVersion
    4

           up()        Version =1

   3                   setVe...
Migrations and sf CLI


                        15
Migrations and sf CLI

        symfony   doctrine:generate-migrations-models
        symfony   doctrine:generate-migration...
Migrations and sf CLI

        symfony   doctrine:generate-migrations-models
        symfony   doctrine:generate-migration...
Migrations and sf CLI

        symfony   doctrine:generate-migrations-models
        symfony   doctrine:generate-migration...
Migrations and sf CLI

        symfony   doctrine:generate-migrations-models
        symfony   doctrine:generate-migration...
Migrations and sf CLI


        symfony doctrine:generate-migrations-models




                                          ...
Migrations and sf CLI


        symfony doctrine:generate-migrations-models




     Compares Model files and schema.yml


...
Migrations and sf CLI


        symfony doctrine:generate-migrations-models




     Compares Model files and schema.yml
  ...
Migrations and sf CLI


        symfony doctrine:generate-migrations-db




                                              ...
Migrations and sf CLI


          symfony doctrine:generate-migrations-db




      Compares DB tables and schema.yml




...
Migrations and sf CLI


          symfony doctrine:generate-migrations-db




      Compares DB tables and schema.yml
    ...
Creating a Migration Diff


                        Before
 BlogPost:
   # [...]
   columns:
     title:    { type: string...
Creating a Migration Diff


                         After
 BlogPost:
   # [...]
   columns:
     title:    { type: string...
Migrations and sf CLI


        symfony doctrine:generate-migrations-diff




                                            ...
Migrations and sf CLI


           symfony doctrine:generate-migrations-diff




   Compares changed schema.yml and Model ...
Migrations and sf CLI


           symfony doctrine:generate-migrations-diff




   Compares changed schema.yml and Model ...
Migrating using sf CLI

           symfony   doctrine:migrate
           symfony   doctrine:build-model
           symfony...
Migrating using sf CLI

           symfony   doctrine:migrate
           symfony   doctrine:build-model
           symfony...
Migrating using sf CLI


          symfony doctrine:migrate
          symfony doctrine:build --all-classes




           ...
Migrating using sf CLI


          symfony doctrine:migrate
          symfony doctrine:build --all-classes




           ...
Migrating using sf CLI


       symfony doctrine:build --all-classes --and-migrate




                                   ...
Migrating using sf CLI


       symfony doctrine:build --all-classes --and-migrate




             That‘s the cool sf 1.3...
Things to keep in mind


                         25
Big vs. small Migrations



          aka. „Complete Migrations“
   vs. „Wild West Cowboy Style Migrations“




          ...
Big vs. small Migrations




                           27
Big vs. small Migrations


         Small Migrations more Agile




                                       27
Big vs. small Migrations


         Small Migrations more Agile
          Really need to be Agile?!




                  ...
Big vs. small Migrations


         Small Migrations more Agile
          Really need to be Agile?!
         Small Migrati...
Big vs. small Migrations


         Small Migrations more Agile
           Really need to be Agile?!
         Small Migrat...
Keep in mind


     Run a diff before building the model




                                            28
Keep in mind


      Run a diff before building the model



    After migrating you can‘t generate the diff!




        ...
Keep in mind


       Keep Data Migrations simple




                                     29
Keep in mind


         Keep Data Migrations simple



      Migrate the Data as easy as possible!




                   ...
Keep in mind


  Version get‘s updated after all Migrations are run




                                                  ...
Keep in mind


  Version get‘s updated after all Migrations are run



    You have to fix problems by hand if they fail.

...
Thank you!


             31
Credits
         Thanks to:
         - Ryan Weaver for his presentation about Doctrine
         ...Migrations
         - S...
Upcoming SlideShare
Loading in...5
×

symfony Live 2010 - Using Doctrine Migrations

20,670
-1

Published on

Published in: Technology
4 Comments
39 Likes
Statistics
Notes
No Downloads
Views
Total Views
20,670
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
503
Comments
4
Likes
39
Embeds 0
No embeds

No notes for slide
  • symfony Live 2010 - Using Doctrine Migrations

    1. 1. Using Doctrine Migrations by Dennis Benkert
    2. 2. Dennis Who?! 2
    3. 3. Dennis Who?! 2
    4. 4. Dennis Who?! 2
    5. 5. Dennis Who?! 2
    6. 6. Dennis Who?! 2
    7. 7. Agenda 3
    8. 8. Agenda Migrating by Hand 3
    9. 9. Agenda Migrating by Hand Doctrine Migrations 3
    10. 10. Agenda Migrating by Hand Doctrine Migrations Migrations and sf CLI 3
    11. 11. Agenda Migrating by Hand Doctrine Migrations Migrations and sf CLI Things to keep in mind 3
    12. 12. So, you want to change your live servers DB schema, right?! 4
    13. 13. But migrating your database by hand is hard work 5
    14. 14. Migrating by Hand 6
    15. 15. Migrating by Hand schema.yml Live DB Dev DB 7
    16. 16. Migrating by Hand schema.yml Live DB Dev DB 7
    17. 17. Migrating by Hand schema.yml Live DB Dev DB 7
    18. 18. Migrating by Hand schema.yml Live DB Dev DB 7
    19. 19. Migrating by Hand schema.yml Live DB Dev DB 7
    20. 20. Migrating by Hand schema.yml Live DB Dev DB 7
    21. 21. What‘s so bad about it? 8
    22. 22. What‘s so bad about it? Error Prone 8
    23. 23. What‘s so bad about it? Error Prone No Rollback 8
    24. 24. What‘s so bad about it? Error Prone No Rollback Not Comfortable 8
    25. 25. Wouldn‘t it be cool if database changes were a bit like SCM revisions? 9
    26. 26. Doctrine Migrations 10
    27. 27. Migration Process schema.yml Migration File 11
    28. 28. Migration Process schema.yml Migration File 11
    29. 29. Migration Process schema.yml Migration File 11
    30. 30. Migration Process Compare schema.yml Migration File 11
    31. 31. Migration Process Compare schema.yml Migration File 11
    32. 32. Migration Process Compare Generate schema.yml Migration File 11
    33. 33. Migration Process Compare Generate schema.yml Migration File 11
    34. 34. Migration Process Compare Generate schema.yml Migration File 11
    35. 35. Migration Process Migrate Compare Generate schema.yml Migration File 11
    36. 36. Migration Files class VersionXX extends Doctrine_Migration_Base { public function up() { $this->addColumn('blog_post', 'tags', 'string', '255', array( )); } public function down() { $this->removeColumn('blog_post', 'tags'); } } 12
    37. 37. Migration Files class VersionXX extends Doctrine_Migration_Base { public function up() { $this->addColumn('blog_post', 'tags', 'string', '255', array( )); } public function down() { $this->removeColumn('blog_post', 'tags'); } } 12
    38. 38. Migration Files Used for upgrading class VersionXX extends Doctrine_Migration_Base { public function up() { $this->addColumn('blog_post', 'tags', 'string', '255', array( )); } public function down() { $this->removeColumn('blog_post', 'tags'); } } 12
    39. 39. Migration Files Used for upgrading class VersionXX extends Doctrine_Migration_Base { public function up() { $this->addColumn('blog_post', 'tags', 'string', '255', array( )); } public function down() { $this->removeColumn('blog_post', 'tags'); } } 12
    40. 40. Migration Files Used for upgrading class VersionXX extends Doctrine_Migration_Base { public function up() { $this->addColumn('blog_post', 'tags', 'string', '255', array( )); } public function down() { $this->removeColumn('blog_post', 'tags'); } } Used for downgrading 12
    41. 41. Migration Files Used for upgrading class VersionXX extends Doctrine_Migration_Base { public function up() { $this->addColumn('blog_post', 'tags', 'string', '255', array( )); } public function down() { $this->removeColumn('blog_post', 'tags'); } } Used for downgrading 12
    42. 42. Migration Files Used for upgrading Table / Data Manipulation class VersionXX extends Doctrine_Migration_Base { public function up() { $this->addColumn('blog_post', 'tags', 'string', '255', array( )); } public function down() { $this->removeColumn('blog_post', 'tags'); } } Used for downgrading 12
    43. 43. How Migrations work migration_version version (int) 13
    44. 44. How Migrations work migration_version version (int) 13
    45. 45. How Migrations work migration_version version (int) Current version stored in your database 13
    46. 46. How Migrations work 4 3 2 1 14
    47. 47. How Migrations work 4 3 2 1 14
    48. 48. How Migrations work 4 getVersion 3 2 1 14
    49. 49. How Migrations work 4 getVersion 3 2 1 14
    50. 50. How Migrations work 4 getVersion Version =1 3 2 1 14
    51. 51. How Migrations work 4 getVersion Version =1 3 2 1 14
    52. 52. How Migrations work 4 getVersion Version =1 3 up() 2 1 14
    53. 53. How Migrations work 4 getVersion Version =1 3 up() 2 1 14
    54. 54. How Migrations work 4 getVersion up() Version =1 3 up() 2 1 14
    55. 55. How Migrations work 4 getVersion up() Version =1 3 up() 2 1 14
    56. 56. How Migrations work up() getVersion 4 up() Version =1 3 up() 2 1 14
    57. 57. How Migrations work up() getVersion 4 up() Version =1 3 up() 2 1 14
    58. 58. How Migrations work up() getVersion 4 up() Version =1 3 setVersion(4) up() 2 1 14
    59. 59. Migrations and sf CLI 15
    60. 60. Migrations and sf CLI symfony doctrine:generate-migrations-models symfony doctrine:generate-migrations-db symfony doctrine:generate-migrations-diff symfony doctrine:migrate 16
    61. 61. Migrations and sf CLI symfony doctrine:generate-migrations-models symfony doctrine:generate-migrations-db symfony doctrine:generate-migrations-diff symfony doctrine:migrate Available in other tasks, too 16
    62. 62. Migrations and sf CLI symfony doctrine:generate-migrations-models symfony doctrine:generate-migrations-db symfony doctrine:generate-migrations-diff symfony doctrine:migrate Available in other tasks, too Even works with plugins 16
    63. 63. Migrations and sf CLI symfony doctrine:generate-migrations-models symfony doctrine:generate-migrations-db symfony doctrine:generate-migrations-diff symfony doctrine:migrate Available in other tasks, too Even works with plugins (only in sf, sorry Matthew :P) 16
    64. 64. Migrations and sf CLI symfony doctrine:generate-migrations-models 17
    65. 65. Migrations and sf CLI symfony doctrine:generate-migrations-models Compares Model files and schema.yml 17
    66. 66. Migrations and sf CLI symfony doctrine:generate-migrations-models Compares Model files and schema.yml Good when starting without schema 17
    67. 67. Migrations and sf CLI symfony doctrine:generate-migrations-db 18
    68. 68. Migrations and sf CLI symfony doctrine:generate-migrations-db Compares DB tables and schema.yml 18
    69. 69. Migrations and sf CLI symfony doctrine:generate-migrations-db Compares DB tables and schema.yml Good when starting with schema 18
    70. 70. Creating a Migration Diff Before BlogPost: # [...] columns: title: { type: string(255), notnull: true } # [...] 19
    71. 71. Creating a Migration Diff After BlogPost: # [...] columns: title: { type: string(255), notnull: true } # [...] tags: { type: string(255) } 20
    72. 72. Migrations and sf CLI symfony doctrine:generate-migrations-diff 21
    73. 73. Migrations and sf CLI symfony doctrine:generate-migrations-diff Compares changed schema.yml and Model files 21
    74. 74. Migrations and sf CLI symfony doctrine:generate-migrations-diff Compares changed schema.yml and Model files Good when already using Migrations 21
    75. 75. Migrating using sf CLI symfony doctrine:migrate symfony doctrine:build-model symfony doctrine:build-filters symfony doctrine:build-forms 22
    76. 76. Migrating using sf CLI symfony doctrine:migrate symfony doctrine:build-model symfony doctrine:build-filters symfony doctrine:build-forms That‘s the obvious way 22
    77. 77. Migrating using sf CLI symfony doctrine:migrate symfony doctrine:build --all-classes 23
    78. 78. Migrating using sf CLI symfony doctrine:migrate symfony doctrine:build --all-classes That‘s the nice way 23
    79. 79. Migrating using sf CLI symfony doctrine:build --all-classes --and-migrate 24
    80. 80. Migrating using sf CLI symfony doctrine:build --all-classes --and-migrate That‘s the cool sf 1.3 way 24
    81. 81. Things to keep in mind 25
    82. 82. Big vs. small Migrations aka. „Complete Migrations“ vs. „Wild West Cowboy Style Migrations“ 26
    83. 83. Big vs. small Migrations 27
    84. 84. Big vs. small Migrations Small Migrations more Agile 27
    85. 85. Big vs. small Migrations Small Migrations more Agile Really need to be Agile?! 27
    86. 86. Big vs. small Migrations Small Migrations more Agile Really need to be Agile?! Small Migrations more dirty 27
    87. 87. Big vs. small Migrations Small Migrations more Agile Really need to be Agile?! Small Migrations more dirty Big Migrations more complicated 27
    88. 88. Keep in mind Run a diff before building the model 28
    89. 89. Keep in mind Run a diff before building the model After migrating you can‘t generate the diff! 28
    90. 90. Keep in mind Keep Data Migrations simple 29
    91. 91. Keep in mind Keep Data Migrations simple Migrate the Data as easy as possible! 29
    92. 92. Keep in mind Version get‘s updated after all Migrations are run 30
    93. 93. Keep in mind Version get‘s updated after all Migrations are run You have to fix problems by hand if they fail. 30
    94. 94. Thank you! 31
    95. 95. Credits Thanks to: - Ryan Weaver for his presentation about Doctrine ...Migrations - Samim for the designs and the artworks. Picture „Von A nach B“ - photocase.com © Anna-Lena Thamm : cydonna Creative Commons stuff used: - „Application, Mime, Vnd.sun.xml.draw“ icon by „New Mooon“ - http://www.iconfinder.net/ icondetails/28957/128/ - „Application, Sqlite2, X“ icon by „New Mooon“ - http://www.iconfinder.net/icondetails/ 28899/128/ - „Application, Php, X “ icon by „New Mooon“ - http://www.iconfinder.net/icondetails/28894/128/ 32
    1. ¿Le ha llamado la atención una diapositiva en particular?

      Recortar diapositivas es una manera útil de recopilar información importante para consultarla más tarde.

    ×