Guest User

Untitled

a guest
Feb 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. # NOTE, all names, syntax, etc... are just off the top of my head to illustrate
  2. # the general idea. Don't take anything too literally.
  3.  
  4. # I just started a blank project with DataMapper. DM Migrations knows
  5. # that the current production database is empty because no schema has been "committed".
  6. #
  7. # I then created a new model:
  8.  
  9. class User
  10. include DataMapper::Resource
  11.  
  12. property :id, Serial
  13. end
  14.  
  15. # DataMapper.auto_migrate! can run and creates a new table users with the ID property.
  16. # A new migration file is also created named work_in_progress.rb with the following
  17. # content:
  18.  
  19. migrate(
  20. :from => table(:users).missing,
  21. :to => table(:users).with(:id => Serial)
  22. ) do
  23. create_table(:users) do |u|
  24. u.column :id, Serial
  25. end
  26. end
  27.  
  28. # I now decided that I want a username property and change the file:
  29.  
  30. class User
  31. include DataMapper::Resource
  32.  
  33. property :id, Serial
  34. property :username, String
  35. end
  36.  
  37. # I run DataMapper.auto_migrate and the new column is created in development mode.
  38. # DM still knows that I haven't committed so it goes ahead and modifies the current
  39. # workign migration file to:
  40.  
  41. migrate(
  42. :from => table(:users).missing,
  43. :to => table(:users).with(:id => Serial, :username => String)
  44. ) do
  45. create_table(:users) do |u|
  46. u.column :id, Serial
  47. u.column :username, String
  48. end
  49. end
  50.  
  51. # So, I decide I want to deploy my awesome application and type a command rake dm:commit
  52. # and datamapper "freezes" the current work_in_progress.rb migration file for deployment.
  53. # So, I deploy my app and decide that it sucks and I need to improve it. I also get John
  54. # to help me. So, now there are two developers working on this awesome app.
  55.  
  56. # Ok, I decided that I needed to add a password field:
  57.  
  58. class User
  59. include DataMapper::Resource
  60.  
  61. property :id, Serial
  62. property :username, String
  63. property :password, String
  64. end
  65.  
  66. # I run auto_migrate. The old committed migration file is stashed somewhere and a new
  67. # work_in_progress.rb migration is created with the following:
  68.  
  69. migrate(
  70. # As you can see, this migration only requires a users table (with anything in it)
  71. # as long as it can successfully add the password column
  72. :from => table(:users),
  73. :to => table(:users).with(:password => String)
  74. ) do
  75. add_column :users, :password, String
  76. end
  77.  
  78. # Ok, now John decides that he wants to remove :username (wtf??):
  79.  
  80. class User
  81. include DataMapper::Resource
  82.  
  83. property :id, Serial
  84. property :password, String
  85. end
  86.  
  87. # And he commits and gets the following migrate file:
  88.  
  89. migrate(
  90. # As you can see, this migration only requires a users table (with anything in it)
  91. # as long as it can successfully add the password column
  92. :from => table(:users).with(:username => String),
  93. :to => table(:users)
  94. ) do
  95. remove_column :users, :username
  96. end
  97.  
  98. # Now here's a cool bit. What if I decide to change :password to an Integer and John
  99. # decides to change :password to a Boolean? My migrate file would look like:
  100.  
  101. migrate(
  102. :from => table(:users).with(:password => String),
  103. :to => table(:users).with(:password => Integer)
  104. ) do
  105. create_column :users, :new_password, Integer
  106. execute "update users set new_password = integer(password);"
  107. remove_column :users, :password
  108. rename_column :users, :new_password, :password
  109. end
  110.  
  111. # And John's migration file would look like:
  112.  
  113. migrate(
  114. :from => table(:users).with(:password => String),
  115. :to => table(:users).with(:password => Boolean)
  116. ) do
  117. remove_column :users, :password
  118. create_column :users, :password, Boolean
  119. end
  120.  
  121. # DM Migrator would know that there is a conflict here and something would happen.
  122.  
  123. # CONCLUSION:
  124.  
  125. # The whole point is to make development easy with auto_migrate and then being able
  126. # to safely get changes to production with some kind of smart migration files. This
  127. # is still a really rough and crazy idea.
Add Comment
Please, Sign In to add comment