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