rails 4.1.14.2 → 5.2.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (286) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +33 -19
  3. metadata +65 -313
  4. data/guides/CHANGELOG.md +0 -106
  5. data/guides/Rakefile +0 -77
  6. data/guides/assets/images/akshaysurve.jpg +0 -0
  7. data/guides/assets/images/belongs_to.png +0 -0
  8. data/guides/assets/images/book_icon.gif +0 -0
  9. data/guides/assets/images/bullet.gif +0 -0
  10. data/guides/assets/images/chapters_icon.gif +0 -0
  11. data/guides/assets/images/check_bullet.gif +0 -0
  12. data/guides/assets/images/credits_pic_blank.gif +0 -0
  13. data/guides/assets/images/csrf.png +0 -0
  14. data/guides/assets/images/edge_badge.png +0 -0
  15. data/guides/assets/images/favicon.ico +0 -0
  16. data/guides/assets/images/feature_tile.gif +0 -0
  17. data/guides/assets/images/footer_tile.gif +0 -0
  18. data/guides/assets/images/fxn.png +0 -0
  19. data/guides/assets/images/getting_started/article_with_comments.png +0 -0
  20. data/guides/assets/images/getting_started/challenge.png +0 -0
  21. data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
  22. data/guides/assets/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
  23. data/guides/assets/images/getting_started/form_with_errors.png +0 -0
  24. data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
  25. data/guides/assets/images/getting_started/new_article.png +0 -0
  26. data/guides/assets/images/getting_started/rails_welcome.png +0 -0
  27. data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
  28. data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
  29. data/guides/assets/images/getting_started/show_action_for_articles.png +0 -0
  30. data/guides/assets/images/getting_started/template_is_missing_articles_new.png +0 -0
  31. data/guides/assets/images/getting_started/unknown_action_create_for_articles.png +0 -0
  32. data/guides/assets/images/getting_started/unknown_action_new_for_articles.png +0 -0
  33. data/guides/assets/images/grey_bullet.gif +0 -0
  34. data/guides/assets/images/habtm.png +0 -0
  35. data/guides/assets/images/has_many.png +0 -0
  36. data/guides/assets/images/has_many_through.png +0 -0
  37. data/guides/assets/images/has_one.png +0 -0
  38. data/guides/assets/images/has_one_through.png +0 -0
  39. data/guides/assets/images/header_backdrop.png +0 -0
  40. data/guides/assets/images/header_tile.gif +0 -0
  41. data/guides/assets/images/i18n/demo_html_safe.png +0 -0
  42. data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
  43. data/guides/assets/images/i18n/demo_translated_en.png +0 -0
  44. data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
  45. data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
  46. data/guides/assets/images/i18n/demo_untranslated.png +0 -0
  47. data/guides/assets/images/icons/README +0 -5
  48. data/guides/assets/images/icons/callouts/1.png +0 -0
  49. data/guides/assets/images/icons/callouts/10.png +0 -0
  50. data/guides/assets/images/icons/callouts/11.png +0 -0
  51. data/guides/assets/images/icons/callouts/12.png +0 -0
  52. data/guides/assets/images/icons/callouts/13.png +0 -0
  53. data/guides/assets/images/icons/callouts/14.png +0 -0
  54. data/guides/assets/images/icons/callouts/15.png +0 -0
  55. data/guides/assets/images/icons/callouts/2.png +0 -0
  56. data/guides/assets/images/icons/callouts/3.png +0 -0
  57. data/guides/assets/images/icons/callouts/4.png +0 -0
  58. data/guides/assets/images/icons/callouts/5.png +0 -0
  59. data/guides/assets/images/icons/callouts/6.png +0 -0
  60. data/guides/assets/images/icons/callouts/7.png +0 -0
  61. data/guides/assets/images/icons/callouts/8.png +0 -0
  62. data/guides/assets/images/icons/callouts/9.png +0 -0
  63. data/guides/assets/images/icons/caution.png +0 -0
  64. data/guides/assets/images/icons/example.png +0 -0
  65. data/guides/assets/images/icons/home.png +0 -0
  66. data/guides/assets/images/icons/important.png +0 -0
  67. data/guides/assets/images/icons/next.png +0 -0
  68. data/guides/assets/images/icons/note.png +0 -0
  69. data/guides/assets/images/icons/prev.png +0 -0
  70. data/guides/assets/images/icons/tip.png +0 -0
  71. data/guides/assets/images/icons/up.png +0 -0
  72. data/guides/assets/images/icons/warning.png +0 -0
  73. data/guides/assets/images/nav_arrow.gif +0 -0
  74. data/guides/assets/images/oscardelben.jpg +0 -0
  75. data/guides/assets/images/polymorphic.png +0 -0
  76. data/guides/assets/images/radar.png +0 -0
  77. data/guides/assets/images/rails4_features.png +0 -0
  78. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  79. data/guides/assets/images/rails_guides_logo.gif +0 -0
  80. data/guides/assets/images/rails_logo_remix.gif +0 -0
  81. data/guides/assets/images/session_fixation.png +0 -0
  82. data/guides/assets/images/tab_grey.gif +0 -0
  83. data/guides/assets/images/tab_info.gif +0 -0
  84. data/guides/assets/images/tab_note.gif +0 -0
  85. data/guides/assets/images/tab_red.gif +0 -0
  86. data/guides/assets/images/tab_yellow.gif +0 -0
  87. data/guides/assets/images/tab_yellow.png +0 -0
  88. data/guides/assets/images/vijaydev.jpg +0 -0
  89. data/guides/assets/javascripts/guides.js +0 -53
  90. data/guides/assets/javascripts/jquery.min.js +0 -4
  91. data/guides/assets/javascripts/responsive-tables.js +0 -43
  92. data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  93. data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  94. data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  95. data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  96. data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  97. data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  98. data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  99. data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  100. data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  101. data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  102. data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  103. data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  104. data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  105. data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  106. data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  107. data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  108. data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  109. data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  110. data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  111. data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  112. data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  113. data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  114. data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  115. data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  116. data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  117. data/guides/assets/javascripts/syntaxhighlighter/shCore.js +0 -17
  118. data/guides/assets/stylesheets/fixes.css +0 -16
  119. data/guides/assets/stylesheets/kindle.css +0 -11
  120. data/guides/assets/stylesheets/main.css +0 -710
  121. data/guides/assets/stylesheets/print.css +0 -52
  122. data/guides/assets/stylesheets/reset.css +0 -43
  123. data/guides/assets/stylesheets/responsive-tables.css +0 -50
  124. data/guides/assets/stylesheets/style.css +0 -13
  125. data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +0 -226
  126. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  127. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  128. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  129. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  130. data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  131. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  132. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  133. data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  134. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  135. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  136. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  137. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  138. data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  139. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  140. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  141. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  142. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  143. data/guides/bug_report_templates/action_controller_gem.rb +0 -47
  144. data/guides/bug_report_templates/action_controller_master.rb +0 -53
  145. data/guides/bug_report_templates/active_record_gem.rb +0 -40
  146. data/guides/bug_report_templates/active_record_master.rb +0 -49
  147. data/guides/bug_report_templates/generic_gem.rb +0 -15
  148. data/guides/bug_report_templates/generic_master.rb +0 -26
  149. data/guides/code/getting_started/Gemfile +0 -40
  150. data/guides/code/getting_started/Gemfile.lock +0 -125
  151. data/guides/code/getting_started/README.rdoc +0 -28
  152. data/guides/code/getting_started/Rakefile +0 -6
  153. data/guides/code/getting_started/app/assets/javascripts/application.js +0 -15
  154. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
  155. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
  156. data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +0 -3
  157. data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
  158. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
  159. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
  160. data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
  161. data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
  162. data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -23
  163. data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -53
  164. data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
  165. data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
  166. data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
  167. data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -2
  168. data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
  169. data/guides/code/getting_started/app/models/comment.rb +0 -3
  170. data/guides/code/getting_started/app/models/post.rb +0 -7
  171. data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
  172. data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
  173. data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
  174. data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
  175. data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
  176. data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
  177. data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
  178. data/guides/code/getting_started/app/views/posts/show.html.erb +0 -18
  179. data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -4
  180. data/guides/code/getting_started/bin/bundle +0 -4
  181. data/guides/code/getting_started/bin/rails +0 -4
  182. data/guides/code/getting_started/bin/rake +0 -4
  183. data/guides/code/getting_started/config.ru +0 -4
  184. data/guides/code/getting_started/config/application.rb +0 -18
  185. data/guides/code/getting_started/config/boot.rb +0 -4
  186. data/guides/code/getting_started/config/database.yml +0 -25
  187. data/guides/code/getting_started/config/environment.rb +0 -5
  188. data/guides/code/getting_started/config/environments/development.rb +0 -30
  189. data/guides/code/getting_started/config/environments/production.rb +0 -80
  190. data/guides/code/getting_started/config/environments/test.rb +0 -36
  191. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
  192. data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
  193. data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
  194. data/guides/code/getting_started/config/initializers/locale.rb +0 -9
  195. data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
  196. data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
  197. data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
  198. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
  199. data/guides/code/getting_started/config/locales/en.yml +0 -23
  200. data/guides/code/getting_started/config/routes.rb +0 -7
  201. data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
  202. data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
  203. data/guides/code/getting_started/db/schema.rb +0 -33
  204. data/guides/code/getting_started/db/seeds.rb +0 -7
  205. data/guides/code/getting_started/public/404.html +0 -60
  206. data/guides/code/getting_started/public/422.html +0 -60
  207. data/guides/code/getting_started/public/500.html +0 -59
  208. data/guides/code/getting_started/public/favicon.ico +0 -0
  209. data/guides/code/getting_started/public/robots.txt +0 -5
  210. data/guides/code/getting_started/test/controllers/comments_controller_test.rb +0 -7
  211. data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
  212. data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
  213. data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
  214. data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
  215. data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
  216. data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
  217. data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
  218. data/guides/code/getting_started/test/models/comment_test.rb +0 -7
  219. data/guides/code/getting_started/test/models/post_test.rb +0 -7
  220. data/guides/code/getting_started/test/test_helper.rb +0 -12
  221. data/guides/rails_guides.rb +0 -63
  222. data/guides/rails_guides/generator.rb +0 -248
  223. data/guides/rails_guides/helpers.rb +0 -53
  224. data/guides/rails_guides/indexer.rb +0 -68
  225. data/guides/rails_guides/kindle.rb +0 -119
  226. data/guides/rails_guides/levenshtein.rb +0 -31
  227. data/guides/rails_guides/markdown.rb +0 -163
  228. data/guides/rails_guides/markdown/renderer.rb +0 -82
  229. data/guides/source/2_2_release_notes.md +0 -435
  230. data/guides/source/2_3_release_notes.md +0 -621
  231. data/guides/source/3_0_release_notes.md +0 -611
  232. data/guides/source/3_1_release_notes.md +0 -556
  233. data/guides/source/3_2_release_notes.md +0 -565
  234. data/guides/source/4_0_release_notes.md +0 -276
  235. data/guides/source/4_1_release_notes.md +0 -731
  236. data/guides/source/_license.html.erb +0 -2
  237. data/guides/source/_welcome.html.erb +0 -25
  238. data/guides/source/action_controller_overview.md +0 -1175
  239. data/guides/source/action_mailer_basics.md +0 -689
  240. data/guides/source/action_view_overview.md +0 -1602
  241. data/guides/source/active_model_basics.md +0 -200
  242. data/guides/source/active_record_basics.md +0 -373
  243. data/guides/source/active_record_callbacks.md +0 -410
  244. data/guides/source/active_record_querying.md +0 -1785
  245. data/guides/source/active_record_validations.md +0 -1169
  246. data/guides/source/active_support_core_extensions.md +0 -3817
  247. data/guides/source/active_support_instrumentation.md +0 -496
  248. data/guides/source/api_documentation_guidelines.md +0 -315
  249. data/guides/source/asset_pipeline.md +0 -1165
  250. data/guides/source/association_basics.md +0 -2229
  251. data/guides/source/caching_with_rails.md +0 -354
  252. data/guides/source/command_line.md +0 -603
  253. data/guides/source/configuring.md +0 -942
  254. data/guides/source/contributing_to_ruby_on_rails.md +0 -544
  255. data/guides/source/credits.html.erb +0 -80
  256. data/guides/source/debugging_rails_applications.md +0 -707
  257. data/guides/source/development_dependencies_install.md +0 -278
  258. data/guides/source/documents.yaml +0 -193
  259. data/guides/source/engines.md +0 -1412
  260. data/guides/source/form_helpers.md +0 -1001
  261. data/guides/source/generators.md +0 -663
  262. data/guides/source/getting_started.md +0 -1946
  263. data/guides/source/i18n.md +0 -1043
  264. data/guides/source/index.html.erb +0 -27
  265. data/guides/source/initialization.md +0 -657
  266. data/guides/source/kindle/copyright.html.erb +0 -1
  267. data/guides/source/kindle/layout.html.erb +0 -27
  268. data/guides/source/kindle/rails_guides.opf.erb +0 -52
  269. data/guides/source/kindle/toc.html.erb +0 -24
  270. data/guides/source/kindle/toc.ncx.erb +0 -64
  271. data/guides/source/kindle/welcome.html.erb +0 -5
  272. data/guides/source/layout.html.erb +0 -146
  273. data/guides/source/layouts_and_rendering.md +0 -1226
  274. data/guides/source/maintenance_policy.md +0 -75
  275. data/guides/source/migrations.md +0 -1143
  276. data/guides/source/nested_model_forms.md +0 -225
  277. data/guides/source/plugins.md +0 -444
  278. data/guides/source/rails_application_templates.md +0 -248
  279. data/guides/source/rails_on_rack.md +0 -332
  280. data/guides/source/routing.md +0 -1115
  281. data/guides/source/ruby_on_rails_guides_guidelines.md +0 -128
  282. data/guides/source/security.md +0 -1018
  283. data/guides/source/testing.md +0 -1052
  284. data/guides/source/upgrading_ruby_on_rails.md +0 -928
  285. data/guides/source/working_with_javascript_in_rails.md +0 -405
  286. data/guides/w3c_validator.rb +0 -95
@@ -1,75 +0,0 @@
1
- Maintenance Policy for Ruby on Rails
2
- ====================================
3
-
4
- Support of the Rails framework is divided into four groups: New features, bug
5
- fixes, security issues, and severe security issues. They are handled as
6
- follows, all versions in `X.Y.Z` format.
7
-
8
- --------------------------------------------------------------------------------
9
-
10
- Rails follows a shifted version of [semver](http://semver.org/):
11
-
12
- **Patch `Z`**
13
-
14
- Only bug fixes, no API changes, no new features.
15
- Except as necessary for security fixes.
16
-
17
- **Minor `Y`**
18
-
19
- New features, may contain API changes (Serve as major versions of Semver).
20
- Breaking changes are paired with deprecation notices in the previous minor
21
- or major release.
22
-
23
- **Major `X`**
24
-
25
- New features, will likely contain API changes. The difference between Rails'
26
- minor and major releases is the magnitude of breaking changes, and usually
27
- reserved for special occasions.
28
-
29
- New Features
30
- ------------
31
-
32
- New features are only added to the master branch and will not be made available
33
- in point releases.
34
-
35
- Bug Fixes
36
- ---------
37
-
38
- Only the latest release series will receive bug fixes. When enough bugs are
39
- fixed and its deemed worthy to release a new gem, this is the branch it happens
40
- from.
41
-
42
- **Currently included series:** `4.1.Z`, `4.0.Z`.
43
-
44
- Security Issues
45
- ---------------
46
-
47
- The current release series and the next most recent one will receive patches
48
- and new versions in case of a security issue.
49
-
50
- These releases are created by taking the last released version, applying the
51
- security patches, and releasing. Those patches are then applied to the end of
52
- the x-y-stable branch. For example, a theoretical 1.2.3 security release would
53
- be built from 1.2.2, and then added to the end of 1-2-stable. This means that
54
- security releases are easy to upgrade to if you're running the latest version
55
- of Rails.
56
-
57
- **Currently included series:** `4.1.Z`, `4.0.Z`.
58
-
59
- Severe Security Issues
60
- ----------------------
61
-
62
- For severe security issues we will provide new versions as above, and also the
63
- last major release series will receive patches and new versions. The
64
- classification of the security issue is judged by the core team.
65
-
66
- **Currently included series:** `4.1.Z`, `4.0.Z`, `3.2.Z`.
67
-
68
- Unsupported Release Series
69
- --------------------------
70
-
71
- When a release series is no longer supported, it's your own responsibility to
72
- deal with bugs and security issues. We may provide backports of the fixes and
73
- publish them to git, however there will be no new versions released. If you are
74
- not comfortable maintaining your own versions, you should upgrade to a
75
- supported version.
@@ -1,1143 +0,0 @@
1
- Active Record Migrations
2
- ========================
3
-
4
- Migrations are a feature of Active Record that allows you to evolve your
5
- database schema over time. Rather than write schema modifications in pure SQL,
6
- migrations allow you to use an easy Ruby DSL to describe changes to your
7
- tables.
8
-
9
- After reading this guide, you will know:
10
-
11
- * The generators you can use to create them.
12
- * The methods Active Record provides to manipulate your database.
13
- * The Rake tasks that manipulate migrations and your schema.
14
- * How migrations relate to `schema.rb`.
15
-
16
- --------------------------------------------------------------------------------
17
-
18
- Migration Overview
19
- ------------------
20
-
21
- Migrations are a convenient way to alter your database schema over time in a
22
- consistent and easy way. They use a Ruby DSL so that you don't have to write
23
- SQL by hand, allowing your schema and changes to be database independent.
24
-
25
- You can think of each migration as being a new 'version' of the database. A
26
- schema starts off with nothing in it, and each migration modifies it to add or
27
- remove tables, columns, or entries. Active Record knows how to update your
28
- schema along this timeline, bringing it from whatever point it is in the
29
- history to the latest version. Active Record will also update your
30
- `db/schema.rb` file to match the up-to-date structure of your database.
31
-
32
- Here's an example of a migration:
33
-
34
- ```ruby
35
- class CreateProducts < ActiveRecord::Migration
36
- def change
37
- create_table :products do |t|
38
- t.string :name
39
- t.text :description
40
-
41
- t.timestamps
42
- end
43
- end
44
- end
45
- ```
46
-
47
- This migration adds a table called `products` with a string column called
48
- `name` and a text column called `description`. A primary key column called `id`
49
- will also be added implicitly, as it's the default primary key for all Active
50
- Record models. The `timestamps` macro adds two columns, `created_at` and
51
- `updated_at`. These special columns are automatically managed by Active Record
52
- if they exist.
53
-
54
- Note that we define the change that we want to happen moving forward in time.
55
- Before this migration is run, there will be no table. After, the table will
56
- exist. Active Record knows how to reverse this migration as well: if we roll
57
- this migration back, it will remove the table.
58
-
59
- On databases that support transactions with statements that change the schema,
60
- migrations are wrapped in a transaction. If the database does not support this
61
- then when a migration fails the parts of it that succeeded will not be rolled
62
- back. You will have to rollback the changes that were made by hand.
63
-
64
- NOTE: There are certain queries that can't run inside a transaction. If your
65
- adapter supports DDL transactions you can use `disable_ddl_transaction!` to
66
- disable them for a single migration.
67
-
68
- If you wish for a migration to do something that Active Record doesn't know how
69
- to reverse, you can use `reversible`:
70
-
71
- ```ruby
72
- class ChangeProductsPrice < ActiveRecord::Migration
73
- def change
74
- reversible do |dir|
75
- change_table :products do |t|
76
- dir.up { t.change :price, :string }
77
- dir.down { t.change :price, :integer }
78
- end
79
- end
80
- end
81
- end
82
- ```
83
-
84
- Alternatively, you can use `up` and `down` instead of `change`:
85
-
86
- ```ruby
87
- class ChangeProductsPrice < ActiveRecord::Migration
88
- def up
89
- change_table :products do |t|
90
- t.change :price, :string
91
- end
92
- end
93
-
94
- def down
95
- change_table :products do |t|
96
- t.change :price, :integer
97
- end
98
- end
99
- end
100
- ```
101
-
102
- Creating a Migration
103
- --------------------
104
-
105
- ### Creating a Standalone Migration
106
-
107
- Migrations are stored as files in the `db/migrate` directory, one for each
108
- migration class. The name of the file is of the form
109
- `YYYYMMDDHHMMSS_create_products.rb`, that is to say a UTC timestamp
110
- identifying the migration followed by an underscore followed by the name
111
- of the migration. The name of the migration class (CamelCased version)
112
- should match the latter part of the file name. For example
113
- `20080906120000_create_products.rb` should define class `CreateProducts` and
114
- `20080906120001_add_details_to_products.rb` should define
115
- `AddDetailsToProducts`. Rails uses this timestamp to determine which migration
116
- should be run and in what order, so if you're copying a migration from another
117
- application or generate a file yourself, be aware of its position in the order.
118
-
119
- Of course, calculating timestamps is no fun, so Active Record provides a
120
- generator to handle making it for you:
121
-
122
- ```bash
123
- $ bin/rails generate migration AddPartNumberToProducts
124
- ```
125
-
126
- This will create an empty but appropriately named migration:
127
-
128
- ```ruby
129
- class AddPartNumberToProducts < ActiveRecord::Migration
130
- def change
131
- end
132
- end
133
- ```
134
-
135
- If the migration name is of the form "AddXXXToYYY" or "RemoveXXXFromYYY" and is
136
- followed by a list of column names and types then a migration containing the
137
- appropriate `add_column` and `remove_column` statements will be created.
138
-
139
- ```bash
140
- $ bin/rails generate migration AddPartNumberToProducts part_number:string
141
- ```
142
-
143
- will generate
144
-
145
- ```ruby
146
- class AddPartNumberToProducts < ActiveRecord::Migration
147
- def change
148
- add_column :products, :part_number, :string
149
- end
150
- end
151
- ```
152
-
153
- If you'd like to add an index on the new column, you can do that as well:
154
-
155
- ```bash
156
- $ bin/rails generate migration AddPartNumberToProducts part_number:string:index
157
- ```
158
-
159
- will generate
160
-
161
- ```ruby
162
- class AddPartNumberToProducts < ActiveRecord::Migration
163
- def change
164
- add_column :products, :part_number, :string
165
- add_index :products, :part_number
166
- end
167
- end
168
- ```
169
-
170
-
171
- Similarly, you can generate a migration to remove a column from the command line:
172
-
173
- ```bash
174
- $ bin/rails generate migration RemovePartNumberFromProducts part_number:string
175
- ```
176
-
177
- generates
178
-
179
- ```ruby
180
- class RemovePartNumberFromProducts < ActiveRecord::Migration
181
- def change
182
- remove_column :products, :part_number, :string
183
- end
184
- end
185
- ```
186
-
187
- You are not limited to one magically generated column. For example:
188
-
189
- ```bash
190
- $ bin/rails generate migration AddDetailsToProducts part_number:string price:decimal
191
- ```
192
-
193
- generates
194
-
195
- ```ruby
196
- class AddDetailsToProducts < ActiveRecord::Migration
197
- def change
198
- add_column :products, :part_number, :string
199
- add_column :products, :price, :decimal
200
- end
201
- end
202
- ```
203
-
204
- If the migration name is of the form "CreateXXX" and is
205
- followed by a list of column names and types then a migration creating the table
206
- XXX with the columns listed will be generated. For example:
207
-
208
- ```bash
209
- $ bin/rails generate migration CreateProducts name:string part_number:string
210
- ```
211
-
212
- generates
213
-
214
- ```ruby
215
- class CreateProducts < ActiveRecord::Migration
216
- def change
217
- create_table :products do |t|
218
- t.string :name
219
- t.string :part_number
220
- end
221
- end
222
- end
223
- ```
224
-
225
- As always, what has been generated for you is just a starting point. You can add
226
- or remove from it as you see fit by editing the
227
- `db/migrate/YYYYMMDDHHMMSS_add_details_to_products.rb` file.
228
-
229
- Also, the generator accepts column type as `references`(also available as
230
- `belongs_to`). For instance:
231
-
232
- ```bash
233
- $ bin/rails generate migration AddUserRefToProducts user:references
234
- ```
235
-
236
- generates
237
-
238
- ```ruby
239
- class AddUserRefToProducts < ActiveRecord::Migration
240
- def change
241
- add_reference :products, :user, index: true
242
- end
243
- end
244
- ```
245
-
246
- This migration will create a `user_id` column and appropriate index.
247
-
248
- There is also a generator which will produce join tables if `JoinTable` is part of the name:
249
-
250
- ```bash
251
- $ bin/rails g migration CreateJoinTableCustomerProduct customer product
252
- ```
253
-
254
- will produce the following migration:
255
-
256
- ```ruby
257
- class CreateJoinTableCustomerProduct < ActiveRecord::Migration
258
- def change
259
- create_join_table :customers, :products do |t|
260
- # t.index [:customer_id, :product_id]
261
- # t.index [:product_id, :customer_id]
262
- end
263
- end
264
- end
265
- ```
266
-
267
- ### Model Generators
268
-
269
- The model and scaffold generators will create migrations appropriate for adding
270
- a new model. This migration will already contain instructions for creating the
271
- relevant table. If you tell Rails what columns you want, then statements for
272
- adding these columns will also be created. For example, running:
273
-
274
- ```bash
275
- $ bin/rails generate model Product name:string description:text
276
- ```
277
-
278
- will create a migration that looks like this
279
-
280
- ```ruby
281
- class CreateProducts < ActiveRecord::Migration
282
- def change
283
- create_table :products do |t|
284
- t.string :name
285
- t.text :description
286
-
287
- t.timestamps
288
- end
289
- end
290
- end
291
- ```
292
-
293
- You can append as many column name/type pairs as you want.
294
-
295
- ### Passing Modifiers
296
-
297
- Some commonly used [type modifiers](#column-modifiers) can be passed directly on
298
- the command line. They are enclosed by curly braces and follow the field type:
299
-
300
- For instance, running:
301
-
302
- ```bash
303
- $ bin/rails generate migration AddDetailsToProducts 'price:decimal{5,2}' supplier:references{polymorphic}
304
- ```
305
-
306
- will produce a migration that looks like this
307
-
308
- ```ruby
309
- class AddDetailsToProducts < ActiveRecord::Migration
310
- def change
311
- add_column :products, :price, :decimal, precision: 5, scale: 2
312
- add_reference :products, :supplier, polymorphic: true, index: true
313
- end
314
- end
315
- ```
316
-
317
- TIP: Have a look at the generators help output for further details.
318
-
319
- Writing a Migration
320
- -------------------
321
-
322
- Once you have created your migration using one of the generators it's time to
323
- get to work!
324
-
325
- ### Creating a Table
326
-
327
- The `create_table` method is one of the most fundamental, but most of the time,
328
- will be generated for you from using a model or scaffold generator. A typical
329
- use would be
330
-
331
- ```ruby
332
- create_table :products do |t|
333
- t.string :name
334
- end
335
- ```
336
-
337
- which creates a `products` table with a column called `name` (and as discussed
338
- below, an implicit `id` column).
339
-
340
- By default, `create_table` will create a primary key called `id`. You can change
341
- the name of the primary key with the `:primary_key` option (don't forget to
342
- update the corresponding model) or, if you don't want a primary key at all, you
343
- can pass the option `id: false`. If you need to pass database specific options
344
- you can place an SQL fragment in the `:options` option. For example:
345
-
346
- ```ruby
347
- create_table :products, options: "ENGINE=BLACKHOLE" do |t|
348
- t.string :name, null: false
349
- end
350
- ```
351
-
352
- will append `ENGINE=BLACKHOLE` to the SQL statement used to create the table
353
- (when using MySQL, the default is `ENGINE=InnoDB`).
354
-
355
- ### Creating a Join Table
356
-
357
- Migration method `create_join_table` creates a HABTM join table. A typical use
358
- would be:
359
-
360
- ```ruby
361
- create_join_table :products, :categories
362
- ```
363
-
364
- which creates a `categories_products` table with two columns called
365
- `category_id` and `product_id`. These columns have the option `:null` set to
366
- `false` by default. This can be overridden by specifying the `:column_options`
367
- option.
368
-
369
- ```ruby
370
- create_join_table :products, :categories, column_options: {null: true}
371
- ```
372
-
373
- will create the `product_id` and `category_id` with the `:null` option as
374
- `true`.
375
-
376
- You can pass the option `:table_name` when you want to customize the table
377
- name. For example:
378
-
379
- ```ruby
380
- create_join_table :products, :categories, table_name: :categorization
381
- ```
382
-
383
- will create a `categorization` table.
384
-
385
- `create_join_table` also accepts a block, which you can use to add indices
386
- (which are not created by default) or additional columns:
387
-
388
- ```ruby
389
- create_join_table :products, :categories do |t|
390
- t.index :product_id
391
- t.index :category_id
392
- end
393
- ```
394
-
395
- ### Changing Tables
396
-
397
- A close cousin of `create_table` is `change_table`, used for changing existing
398
- tables. It is used in a similar fashion to `create_table` but the object
399
- yielded to the block knows more tricks. For example:
400
-
401
- ```ruby
402
- change_table :products do |t|
403
- t.remove :description, :name
404
- t.string :part_number
405
- t.index :part_number
406
- t.rename :upccode, :upc_code
407
- end
408
- ```
409
-
410
- removes the `description` and `name` columns, creates a `part_number` string
411
- column and adds an index on it. Finally it renames the `upccode` column.
412
-
413
- ### Changing Columns
414
-
415
- Like the `remove_column` and `add_column` Rails provides the `change_column`
416
- migration method.
417
-
418
- ```ruby
419
- change_column :products, :part_number, :text
420
- ```
421
-
422
- This changes the column `part_number` on products table to be a `:text` field.
423
-
424
- Besides `change_column`, the `change_column_null` and `change_column_default`
425
- methods are used specifically to change the null and default values of a
426
- column.
427
-
428
- ```ruby
429
- change_column_null :products, :name, false
430
- change_column_default :products, :approved, false
431
- ```
432
-
433
- This sets `:name` field on products to a `NOT NULL` column and the default
434
- value of the `:approved` field to false.
435
-
436
- ### Column Modifiers
437
-
438
- Column modifiers can be applied when creating or changing a column:
439
-
440
- * `limit` Sets the maximum size of the `string/text/binary/integer` fields.
441
- * `precision` Defines the precision for the `decimal` fields, representing the total number of digits in the number.
442
- * `scale` Defines the scale for the `decimal` fields, representing the number of digits after the decimal point.
443
- * `polymorphic` Adds a `type` column for `belongs_to` associations.
444
- * `null` Allows or disallows `NULL` values in the column.
445
- * `default` Allows to set a default value on the column. NOTE: If using a dynamic value (such as date), the default will only be calculated the first time (e.g. on the date the migration is applied.)
446
- * `index` Adds an index for the column.
447
-
448
- Some adapters may support additional options; see the adapter specific API docs
449
- for further information.
450
-
451
- ### When Helpers aren't Enough
452
-
453
- If the helpers provided by Active Record aren't enough you can use the `execute`
454
- method to execute arbitrary SQL:
455
-
456
- ```ruby
457
- Product.connection.execute('UPDATE `products` SET `price`=`free` WHERE 1')
458
- ```
459
-
460
- For more details and examples of individual methods, check the API documentation.
461
- In particular the documentation for
462
- [`ActiveRecord::ConnectionAdapters::SchemaStatements`](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html)
463
- (which provides the methods available in the `change`, `up` and `down` methods),
464
- [`ActiveRecord::ConnectionAdapters::TableDefinition`](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html)
465
- (which provides the methods available on the object yielded by `create_table`)
466
- and
467
- [`ActiveRecord::ConnectionAdapters::Table`](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html)
468
- (which provides the methods available on the object yielded by `change_table`).
469
-
470
- ### Using the `change` Method
471
-
472
- The `change` method is the primary way of writing migrations. It works for the
473
- majority of cases, where Active Record knows how to reverse the migration
474
- automatically. Currently, the `change` method supports only these migration
475
- definitions:
476
-
477
- * `add_column`
478
- * `add_index`
479
- * `add_reference`
480
- * `add_timestamps`
481
- * `create_table`
482
- * `create_join_table`
483
- * `drop_table` (must supply a block)
484
- * `drop_join_table` (must supply a block)
485
- * `remove_timestamps`
486
- * `rename_column`
487
- * `rename_index`
488
- * `remove_reference`
489
- * `rename_table`
490
-
491
- `change_table` is also reversible, as long as the block does not call `change`,
492
- `change_default` or `remove`.
493
-
494
- If you're going to need to use any other methods, you should use `reversible`
495
- or write the `up` and `down` methods instead of using the `change` method.
496
-
497
- ### Using `reversible`
498
-
499
- Complex migrations may require processing that Active Record doesn't know how
500
- to reverse. You can use `reversible` to specify what to do when running a
501
- migration what else to do when reverting it. For example:
502
-
503
- ```ruby
504
- class ExampleMigration < ActiveRecord::Migration
505
- def change
506
- create_table :products do |t|
507
- t.references :category
508
- end
509
-
510
- reversible do |dir|
511
- dir.up do
512
- #add a foreign key
513
- execute <<-SQL
514
- ALTER TABLE products
515
- ADD CONSTRAINT fk_products_categories
516
- FOREIGN KEY (category_id)
517
- REFERENCES categories(id)
518
- SQL
519
- end
520
- dir.down do
521
- execute <<-SQL
522
- ALTER TABLE products
523
- DROP FOREIGN KEY fk_products_categories
524
- SQL
525
- end
526
- end
527
-
528
- add_column :users, :home_page_url, :string
529
- rename_column :users, :email, :email_address
530
- end
531
- end
532
- ```
533
-
534
- Using `reversible` will ensure that the instructions are executed in the
535
- right order too. If the previous example migration is reverted,
536
- the `down` block will be run after the `home_page_url` column is removed and
537
- right before the table `products` is dropped.
538
-
539
- Sometimes your migration will do something which is just plain irreversible; for
540
- example, it might destroy some data. In such cases, you can raise
541
- `ActiveRecord::IrreversibleMigration` in your `down` block. If someone tries
542
- to revert your migration, an error message will be displayed saying that it
543
- can't be done.
544
-
545
- ### Using the `up`/`down` Methods
546
-
547
- You can also use the old style of migration using `up` and `down` methods
548
- instead of the `change` method.
549
- The `up` method should describe the transformation you'd like to make to your
550
- schema, and the `down` method of your migration should revert the
551
- transformations done by the `up` method. In other words, the database schema
552
- should be unchanged if you do an `up` followed by a `down`. For example, if you
553
- create a table in the `up` method, you should drop it in the `down` method. It
554
- is wise to reverse the transformations in precisely the reverse order they were
555
- made in the `up` method. The example in the `reversible` section is equivalent to:
556
-
557
- ```ruby
558
- class ExampleMigration < ActiveRecord::Migration
559
- def up
560
- create_table :products do |t|
561
- t.references :category
562
- end
563
-
564
- # add a foreign key
565
- execute <<-SQL
566
- ALTER TABLE products
567
- ADD CONSTRAINT fk_products_categories
568
- FOREIGN KEY (category_id)
569
- REFERENCES categories(id)
570
- SQL
571
-
572
- add_column :users, :home_page_url, :string
573
- rename_column :users, :email, :email_address
574
- end
575
-
576
- def down
577
- rename_column :users, :email_address, :email
578
- remove_column :users, :home_page_url
579
-
580
- execute <<-SQL
581
- ALTER TABLE products
582
- DROP FOREIGN KEY fk_products_categories
583
- SQL
584
-
585
- drop_table :products
586
- end
587
- end
588
- ```
589
-
590
- If your migration is irreversible, you should raise
591
- `ActiveRecord::IrreversibleMigration` from your `down` method. If someone tries
592
- to revert your migration, an error message will be displayed saying that it
593
- can't be done.
594
-
595
- ### Reverting Previous Migrations
596
-
597
- You can use Active Record's ability to rollback migrations using the `revert` method:
598
-
599
- ```ruby
600
- require_relative '2012121212_example_migration'
601
-
602
- class FixupExampleMigration < ActiveRecord::Migration
603
- def change
604
- revert ExampleMigration
605
-
606
- create_table(:apples) do |t|
607
- t.string :variety
608
- end
609
- end
610
- end
611
- ```
612
-
613
- The `revert` method also accepts a block of instructions to reverse.
614
- This could be useful to revert selected parts of previous migrations.
615
- For example, let's imagine that `ExampleMigration` is committed and it
616
- is later decided it would be best to serialize the product list instead.
617
- One could write:
618
-
619
- ```ruby
620
- class SerializeProductListMigration < ActiveRecord::Migration
621
- def change
622
- add_column :categories, :product_list
623
-
624
- reversible do |dir|
625
- dir.up do
626
- # transfer data from Products to Category#product_list
627
- end
628
- dir.down do
629
- # create Products from Category#product_list
630
- end
631
- end
632
-
633
- revert do
634
- # copy-pasted code from ExampleMigration
635
- create_table :products do |t|
636
- t.references :category
637
- end
638
-
639
- reversible do |dir|
640
- dir.up do
641
- #add a foreign key
642
- execute <<-SQL
643
- ALTER TABLE products
644
- ADD CONSTRAINT fk_products_categories
645
- FOREIGN KEY (category_id)
646
- REFERENCES categories(id)
647
- SQL
648
- end
649
- dir.down do
650
- execute <<-SQL
651
- ALTER TABLE products
652
- DROP FOREIGN KEY fk_products_categories
653
- SQL
654
- end
655
- end
656
-
657
- # The rest of the migration was ok
658
- end
659
- end
660
- end
661
- ```
662
-
663
- The same migration could also have been written without using `revert`
664
- but this would have involved a few more steps: reversing the order
665
- of `create_table` and `reversible`, replacing `create_table`
666
- by `drop_table`, and finally replacing `up` by `down` and vice-versa.
667
- This is all taken care of by `revert`.
668
-
669
- Running Migrations
670
- ------------------
671
-
672
- Rails provides a set of Rake tasks to run certain sets of migrations.
673
-
674
- The very first migration related Rake task you will use will probably be
675
- `rake db:migrate`. In its most basic form it just runs the `change` or `up`
676
- method for all the migrations that have not yet been run. If there are
677
- no such migrations, it exits. It will run these migrations in order based
678
- on the date of the migration.
679
-
680
- Note that running the `db:migrate` task also invokes the `db:schema:dump` task, which
681
- will update your `db/schema.rb` file to match the structure of your database.
682
-
683
- If you specify a target version, Active Record will run the required migrations
684
- (change, up, down) until it has reached the specified version. The version
685
- is the numerical prefix on the migration's filename. For example, to migrate
686
- to version 20080906120000 run:
687
-
688
- ```bash
689
- $ bin/rake db:migrate VERSION=20080906120000
690
- ```
691
-
692
- If version 20080906120000 is greater than the current version (i.e., it is
693
- migrating upwards), this will run the `change` (or `up`) method
694
- on all migrations up to and
695
- including 20080906120000, and will not execute any later migrations. If
696
- migrating downwards, this will run the `down` method on all the migrations
697
- down to, but not including, 20080906120000.
698
-
699
- ### Rolling Back
700
-
701
- A common task is to rollback the last migration. For example, if you made a
702
- mistake in it and wish to correct it. Rather than tracking down the version
703
- number associated with the previous migration you can run:
704
-
705
- ```bash
706
- $ bin/rake db:rollback
707
- ```
708
-
709
- This will rollback the latest migration, either by reverting the `change`
710
- method or by running the `down` method. If you need to undo
711
- several migrations you can provide a `STEP` parameter:
712
-
713
- ```bash
714
- $ bin/rake db:rollback STEP=3
715
- ```
716
-
717
- will revert the last 3 migrations.
718
-
719
- The `db:migrate:redo` task is a shortcut for doing a rollback and then migrating
720
- back up again. As with the `db:rollback` task, you can use the `STEP` parameter
721
- if you need to go more than one version back, for example:
722
-
723
- ```bash
724
- $ bin/rake db:migrate:redo STEP=3
725
- ```
726
-
727
- Neither of these Rake tasks do anything you could not do with `db:migrate`. They
728
- are simply more convenient, since you do not need to explicitly specify the
729
- version to migrate to.
730
-
731
- ### Setup the Database
732
-
733
- The `rake db:setup` task will create the database, load the schema and initialize
734
- it with the seed data.
735
-
736
- ### Resetting the Database
737
-
738
- The `rake db:reset` task will drop the database and set it up again. This is
739
- functionally equivalent to `rake db:drop db:setup`.
740
-
741
- NOTE: This is not the same as running all the migrations. It will only use the
742
- contents of the current `schema.rb` file. If a migration can't be rolled back,
743
- `rake db:reset` may not help you. To find out more about dumping the schema see
744
- [Schema Dumping and You](#schema-dumping-and-you) section.
745
-
746
- ### Running Specific Migrations
747
-
748
- If you need to run a specific migration up or down, the `db:migrate:up` and
749
- `db:migrate:down` tasks will do that. Just specify the appropriate version and
750
- the corresponding migration will have its `change`, `up` or `down` method
751
- invoked, for example:
752
-
753
- ```bash
754
- $ bin/rake db:migrate:up VERSION=20080906120000
755
- ```
756
-
757
- will run the 20080906120000 migration by running the `change` method (or the
758
- `up` method). This task will
759
- first check whether the migration is already performed and will do nothing if
760
- Active Record believes that it has already been run.
761
-
762
- ### Running Migrations in Different Environments
763
-
764
- By default running `rake db:migrate` will run in the `development` environment.
765
- To run migrations against another environment you can specify it using the
766
- `RAILS_ENV` environment variable while running the command. For example to run
767
- migrations against the `test` environment you could run:
768
-
769
- ```bash
770
- $ bin/rake db:migrate RAILS_ENV=test
771
- ```
772
-
773
- ### Changing the Output of Running Migrations
774
-
775
- By default migrations tell you exactly what they're doing and how long it took.
776
- A migration creating a table and adding an index might produce output like this
777
-
778
- ```bash
779
- == CreateProducts: migrating =================================================
780
- -- create_table(:products)
781
- -> 0.0028s
782
- == CreateProducts: migrated (0.0028s) ========================================
783
- ```
784
-
785
- Several methods are provided in migrations that allow you to control all this:
786
-
787
- | Method | Purpose
788
- | -------------------- | -------
789
- | suppress_messages | Takes a block as an argument and suppresses any output generated by the block.
790
- | say | Takes a message argument and outputs it as is. A second boolean argument can be passed to specify whether to indent or not.
791
- | say_with_time | Outputs text along with how long it took to run its block. If the block returns an integer it assumes it is the number of rows affected.
792
-
793
- For example, this migration:
794
-
795
- ```ruby
796
- class CreateProducts < ActiveRecord::Migration
797
- def change
798
- suppress_messages do
799
- create_table :products do |t|
800
- t.string :name
801
- t.text :description
802
- t.timestamps
803
- end
804
- end
805
-
806
- say "Created a table"
807
-
808
- suppress_messages {add_index :products, :name}
809
- say "and an index!", true
810
-
811
- say_with_time 'Waiting for a while' do
812
- sleep 10
813
- 250
814
- end
815
- end
816
- end
817
- ```
818
-
819
- generates the following output
820
-
821
- ```bash
822
- == CreateProducts: migrating =================================================
823
- -- Created a table
824
- -> and an index!
825
- -- Waiting for a while
826
- -> 10.0013s
827
- -> 250 rows
828
- == CreateProducts: migrated (10.0054s) =======================================
829
- ```
830
-
831
- If you want Active Record to not output anything, then running `rake db:migrate
832
- VERBOSE=false` will suppress all output.
833
-
834
- Changing Existing Migrations
835
- ----------------------------
836
-
837
- Occasionally you will make a mistake when writing a migration. If you have
838
- already run the migration then you cannot just edit the migration and run the
839
- migration again: Rails thinks it has already run the migration and so will do
840
- nothing when you run `rake db:migrate`. You must rollback the migration (for
841
- example with `rake db:rollback`), edit your migration and then run
842
- `rake db:migrate` to run the corrected version.
843
-
844
- In general, editing existing migrations is not a good idea. You will be
845
- creating extra work for yourself and your co-workers and cause major headaches
846
- if the existing version of the migration has already been run on production
847
- machines. Instead, you should write a new migration that performs the changes
848
- you require. Editing a freshly generated migration that has not yet been
849
- committed to source control (or, more generally, which has not been propagated
850
- beyond your development machine) is relatively harmless.
851
-
852
- The `revert` method can be helpful when writing a new migration to undo
853
- previous migrations in whole or in part
854
- (see [Reverting Previous Migrations](#reverting-previous-migrations) above).
855
-
856
- Using Models in Your Migrations
857
- -------------------------------
858
-
859
- When creating or updating data in a migration it is often tempting to use one
860
- of your models. After all, they exist to provide easy access to the underlying
861
- data. This can be done, but some caution should be observed.
862
-
863
- For example, problems occur when the model uses database columns which are (1)
864
- not currently in the database and (2) will be created by this or a subsequent
865
- migration.
866
-
867
- Consider this example, where Alice and Bob are working on the same code base
868
- which contains a `Product` model:
869
-
870
- Bob goes on vacation.
871
-
872
- Alice creates a migration for the `products` table which adds a new column and
873
- initializes it:
874
-
875
- ```ruby
876
- # db/migrate/20100513121110_add_flag_to_product.rb
877
-
878
- class AddFlagToProduct < ActiveRecord::Migration
879
- def change
880
- add_column :products, :flag, :boolean
881
- reversible do |dir|
882
- dir.up { Product.update_all flag: false }
883
- end
884
- end
885
- end
886
- ```
887
-
888
- She also adds a validation to the `Product` model for the new column:
889
-
890
- ```ruby
891
- # app/models/product.rb
892
-
893
- class Product < ActiveRecord::Base
894
- validates :flag, inclusion: { in: [true, false] }
895
- end
896
- ```
897
-
898
- Alice adds a second migration which adds another column to the `products`
899
- table and initializes it:
900
-
901
- ```ruby
902
- # db/migrate/20100515121110_add_fuzz_to_product.rb
903
-
904
- class AddFuzzToProduct < ActiveRecord::Migration
905
- def change
906
- add_column :products, :fuzz, :string
907
- reversible do |dir|
908
- dir.up { Product.update_all fuzz: 'fuzzy' }
909
- end
910
- end
911
- end
912
- ```
913
-
914
- She also adds a validation to the `Product` model for the new column:
915
-
916
- ```ruby
917
- # app/models/product.rb
918
-
919
- class Product < ActiveRecord::Base
920
- validates :flag, inclusion: { in: [true, false] }
921
- validates :fuzz, presence: true
922
- end
923
- ```
924
-
925
- Both migrations work for Alice.
926
-
927
- Bob comes back from vacation and:
928
-
929
- * Updates the source - which contains both migrations and the latest version
930
- of the Product model.
931
- * Runs outstanding migrations with `rake db:migrate`, which
932
- includes the one that updates the `Product` model.
933
-
934
- The migration crashes because when the model attempts to save, it tries to
935
- validate the second added column, which is not in the database when the _first_
936
- migration runs:
937
-
938
- ```
939
- rake aborted!
940
- An error has occurred, this and all later migrations canceled:
941
-
942
- undefined method `fuzz' for #<Product:0x000001049b14a0>
943
- ```
944
-
945
- A fix for this is to create a local model within the migration. This keeps
946
- Rails from running the validations, so that the migrations run to completion.
947
-
948
- When using a local model, it's a good idea to call
949
- `Product.reset_column_information` to refresh the Active Record cache for the
950
- `Product` model prior to updating data in the database.
951
-
952
- If Alice had done this instead, there would have been no problem:
953
-
954
- ```ruby
955
- # db/migrate/20100513121110_add_flag_to_product.rb
956
-
957
- class AddFlagToProduct < ActiveRecord::Migration
958
- class Product < ActiveRecord::Base
959
- end
960
-
961
- def change
962
- add_column :products, :flag, :boolean
963
- Product.reset_column_information
964
- reversible do |dir|
965
- dir.up { Product.update_all flag: false }
966
- end
967
- end
968
- end
969
- ```
970
-
971
- ```ruby
972
- # db/migrate/20100515121110_add_fuzz_to_product.rb
973
-
974
- class AddFuzzToProduct < ActiveRecord::Migration
975
- class Product < ActiveRecord::Base
976
- end
977
-
978
- def change
979
- add_column :products, :fuzz, :string
980
- Product.reset_column_information
981
- reversible do |dir|
982
- dir.up { Product.update_all fuzz: 'fuzzy' }
983
- end
984
- end
985
- end
986
- ```
987
-
988
- There are other ways in which the above example could have gone badly.
989
-
990
- For example, imagine that Alice creates a migration that selectively
991
- updates the `description` field on certain products. She runs the
992
- migration, commits the code, and then begins working on the next feature,
993
- which is to add a new column `fuzz` to the products table.
994
-
995
- She creates two migrations for this new feature, one which adds the new
996
- column, and a second which selectively updates the `fuzz` column based on
997
- other product attributes.
998
-
999
- These migrations run just fine, but when Bob comes back from his vacation
1000
- and calls `rake db:migrate` to run all the outstanding migrations, he gets a
1001
- subtle bug: The descriptions have defaults, and the `fuzz` column is present,
1002
- but `fuzz` is `nil` on all products.
1003
-
1004
- The solution is again to use `Product.reset_column_information` before
1005
- referencing the Product model in a migration, ensuring the Active Record's
1006
- knowledge of the table structure is current before manipulating data in those
1007
- records.
1008
-
1009
- Schema Dumping and You
1010
- ----------------------
1011
-
1012
- ### What are Schema Files for?
1013
-
1014
- Migrations, mighty as they may be, are not the authoritative source for your
1015
- database schema. That role falls to either `db/schema.rb` or an SQL file which
1016
- Active Record generates by examining the database. They are not designed to be
1017
- edited, they just represent the current state of the database.
1018
-
1019
- There is no need (and it is error prone) to deploy a new instance of an app by
1020
- replaying the entire migration history. It is much simpler and faster to just
1021
- load into the database a description of the current schema.
1022
-
1023
- For example, this is how the test database is created: the current development
1024
- database is dumped (either to `db/schema.rb` or `db/structure.sql`) and then
1025
- loaded into the test database.
1026
-
1027
- Schema files are also useful if you want a quick look at what attributes an
1028
- Active Record object has. This information is not in the model's code and is
1029
- frequently spread across several migrations, but the information is nicely
1030
- summed up in the schema file. The
1031
- [annotate_models](https://github.com/ctran/annotate_models) gem automatically
1032
- adds and updates comments at the top of each model summarizing the schema if
1033
- you desire that functionality.
1034
-
1035
- ### Types of Schema Dumps
1036
-
1037
- There are two ways to dump the schema. This is set in `config/application.rb`
1038
- by the `config.active_record.schema_format` setting, which may be either `:sql`
1039
- or `:ruby`.
1040
-
1041
- If `:ruby` is selected then the schema is stored in `db/schema.rb`. If you look
1042
- at this file you'll find that it looks an awful lot like one very big
1043
- migration:
1044
-
1045
- ```ruby
1046
- ActiveRecord::Schema.define(version: 20080906171750) do
1047
- create_table "authors", force: true do |t|
1048
- t.string "name"
1049
- t.datetime "created_at"
1050
- t.datetime "updated_at"
1051
- end
1052
-
1053
- create_table "products", force: true do |t|
1054
- t.string "name"
1055
- t.text "description"
1056
- t.datetime "created_at"
1057
- t.datetime "updated_at"
1058
- t.string "part_number"
1059
- end
1060
- end
1061
- ```
1062
-
1063
- In many ways this is exactly what it is. This file is created by inspecting the
1064
- database and expressing its structure using `create_table`, `add_index`, and so
1065
- on. Because this is database-independent, it could be loaded into any database
1066
- that Active Record supports. This could be very useful if you were to
1067
- distribute an application that is able to run against multiple databases.
1068
-
1069
- There is however a trade-off: `db/schema.rb` cannot express database specific
1070
- items such as foreign key constraints, triggers, or stored procedures. While in
1071
- a migration you can execute custom SQL statements, the schema dumper cannot
1072
- reconstitute those statements from the database. If you are using features like
1073
- this, then you should set the schema format to `:sql`.
1074
-
1075
- Instead of using Active Record's schema dumper, the database's structure will
1076
- be dumped using a tool specific to the database (via the `db:structure:dump`
1077
- Rake task) into `db/structure.sql`. For example, for PostgreSQL, the `pg_dump`
1078
- utility is used. For MySQL, this file will contain the output of
1079
- `SHOW CREATE TABLE` for the various tables.
1080
-
1081
- Loading these schemas is simply a question of executing the SQL statements they
1082
- contain. By definition, this will create a perfect copy of the database's
1083
- structure. Using the `:sql` schema format will, however, prevent loading the
1084
- schema into a RDBMS other than the one used to create it.
1085
-
1086
- ### Schema Dumps and Source Control
1087
-
1088
- Because schema dumps are the authoritative source for your database schema, it
1089
- is strongly recommended that you check them into source control.
1090
-
1091
- Active Record and Referential Integrity
1092
- ---------------------------------------
1093
-
1094
- The Active Record way claims that intelligence belongs in your models, not in
1095
- the database. As such, features such as triggers or foreign key constraints,
1096
- which push some of that intelligence back into the database, are not heavily
1097
- used.
1098
-
1099
- Validations such as `validates :foreign_key, uniqueness: true` are one way in
1100
- which models can enforce data integrity. The `:dependent` option on
1101
- associations allows models to automatically destroy child objects when the
1102
- parent is destroyed. Like anything which operates at the application level,
1103
- these cannot guarantee referential integrity and so some people augment them
1104
- with foreign key constraints in the database.
1105
-
1106
- Although Active Record does not provide any tools for working directly with
1107
- such features, the `execute` method can be used to execute arbitrary SQL. You
1108
- can also use a gem like
1109
- [foreigner](https://github.com/matthuhiggins/foreigner) which adds foreign key
1110
- support to Active Record (including support for dumping foreign keys in
1111
- `db/schema.rb`).
1112
-
1113
- Migrations and Seed Data
1114
- ------------------------
1115
-
1116
- Some people use migrations to add data to the database:
1117
-
1118
- ```ruby
1119
- class AddInitialProducts < ActiveRecord::Migration
1120
- def up
1121
- 5.times do |i|
1122
- Product.create(name: "Product ##{i}", description: "A product.")
1123
- end
1124
- end
1125
-
1126
- def down
1127
- Product.delete_all
1128
- end
1129
- end
1130
- ```
1131
-
1132
- However, Rails has a 'seeds' feature that should be used for seeding a database
1133
- with initial data. It's a really simple feature: just fill up `db/seeds.rb`
1134
- with some Ruby code, and run `rake db:seed`:
1135
-
1136
- ```ruby
1137
- 5.times do |i|
1138
- Product.create(name: "Product ##{i}", description: "A product.")
1139
- end
1140
- ```
1141
-
1142
- This is generally a much cleaner way to set up the database of a blank
1143
- application.