From d7c2fcd856e7a592955598d5ae3f2216cd5abcff Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Sun, 4 Dec 2011 08:49:26 +1100 Subject: Update layouts and rendering guide javascript_include_tag to bring it in line with Rails 3.1 --- .../guides/source/layouts_and_rendering.textile | 47 +++++++++++++++++++--- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/railties/guides/source/layouts_and_rendering.textile b/railties/guides/source/layouts_and_rendering.textile index df7b9b364c..6c050e27d6 100644 --- a/railties/guides/source/layouts_and_rendering.textile +++ b/railties/guides/source/layouts_and_rendering.textile @@ -671,19 +671,33 @@ There are three tag options available for the +auto_discovery_link_tag+: h5. Linking to JavaScript Files with the +javascript_include_tag+ -The +javascript_include_tag+ helper returns an HTML +script+ tag for each source provided. Rails looks in +public/javascripts+ for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include +public/javascripts/main.js+: +The +javascript_include_tag+ helper returns an HTML +script+ tag for each source provided. + +If you are using Rails with the "Asset Pipeline":http://guides.rubyonrails.org/asset_pipeline.html enabled, this helper will generate a link to +/assets/javascripts/+ rather than +public/javascripts+ which was used in earlier versions of Rails. This link is then served by the Sprockets gem, which was introduced in Rails 3.1. + +A JavaScript file within a Rails application or Rails engine goes in one of three locations: +app/assets+, +lib/assets+ or +vendor/assets+. These locations are explained in detail in the "Asset Organisation section in the Asset Pipeline Guide":http://guides.rubyonrails.org/asset_pipeline.html#asset-organization + +You can specify a full path relative to the document root, or a URL, if you prefer. For example, to link to a JavaScript file that is inside a directory called +javascripts+ inside of one of +app/assets+, +lib/assets+ or +vendor/assets+, you would do this: <%= javascript_include_tag "main" %> -To include +public/javascripts/main.js+ and +public/javascripts/columns.js+: +Rails will then output a +script+ tag such as this: + + + + + +The request to this asset is then served by the Sprockets gem. + +To include multiple files such as +app/assets/javascripts/main.js+ and +app/assets/javascripts/columns.js+ at the same time: <%= javascript_include_tag "main", "columns" %> -To include +public/javascripts/main.js+ and +public/photos/columns.js+: +To include +app/assets/javascripts/main.js+ and +app/assets/javascripts/photos/columns.js+: <%= javascript_include_tag "main", "/photos/columns" %> @@ -701,15 +715,38 @@ If the application does not use the asset pipeline, the +:defaults+ option loads <%= javascript_include_tag :defaults %> -And you can in any case override the expansion in config/application.rb: +Outputting +script+ tags such as this: + + + + + + +These two files for jQuery, +jquery.js+ and +jquery_ujs.js+ must be placed inside +public/javascripts+ if the application doesn't use the asset pipeline. These files can be downloaded from the "jquery-rails repository on GitHub":https://github.com/indirect/jquery-rails/tree/master/vendor/assets/javascripts + +WARNING: If you are using the Asset Pipeline, this tag will render a +script+ tag for an asset called +defaults.js+, which would not exist in your application unless you've explicitly defined it to be. + +And you can in any case override the +:defaults+ expansion in config/application.rb: config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js) +You can also define new defaults: + + +config.action_view.javascript_expansions[:projects] = %w(projects.js tickets.js) + + +And use them by referencing them exactly like +:defaults+: + + +<%= javascript_include_tag :projects %> + + When using :defaults, if an application.js file exists in public/javascripts it will be included as well at then end. -Also, the +:all+ option loads every JavaScript file in +public/javascripts+: +Also, if the Asset Pipeline is disabled, the +:all+ expansion loads every JavaScript file in +public/javascripts+: <%= javascript_include_tag :all %> -- cgit v1.2.3 From 9d87fb8f684698cae7ee281bc5cf17db907d8cdd Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 16:48:41 -0700 Subject: Constrained to 80 chars per line --- railties/guides/source/migrations.textile | 540 ++++++++++++++++++++---------- 1 file changed, 367 insertions(+), 173 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index c63f2aa119..13fc76359f 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -1,28 +1,40 @@ h2. Migrations -Migrations are a convenient way for you to alter your database in a structured and organized manner. You could edit fragments of SQL by hand but you would then be responsible for telling other developers that they need to go and run them. You'd also have to keep track of which changes need to be run against the production machines next time you deploy. - -Active Record tracks which migrations have already been run so all you have to do is update your source and run +rake db:migrate+. Active Record will work out which migrations should be run. It will also update your +db/schema.rb+ file to match the structure of your database. - -Migrations also allow you to describe these transformations using Ruby. The great thing about this is that (like most of Active Record's functionality) it is database independent: you don't need to worry about the precise syntax of +CREATE TABLE+ any more than you worry about variations on +SELECT *+ (you can drop down to raw SQL for database specific features). For example you could use SQLite3 in development, but MySQL in production. +Migrations are a convenient way for you to alter your database in a structured +and organized manner. You could edit fragments of SQL by hand but you would then +be responsible for telling other developers that they need to go and run them. +You'd also have to keep track of which changes need to be run against the +production machines next time you deploy. + +Active Record tracks which migrations have already been run so all you have to +do is update your source and run +rake db:migrate+. Active Record will work out +which migrations should be run. It will also update your +db/schema.rb+ file to +match the structure of your database. + +Migrations also allow you to describe these transformations using Ruby. The +great thing about this is that (like most of Active Record's functionality) it +is database independent: you don't need to worry about the precise syntax of ++CREATE TABLE+ any more than you worry about variations on +SELECT *+ (you can +drop down to raw SQL for database specific features). For example you could use +SQLite3 in development, but MySQL in production. You'll learn all about migrations including: -* The generators you can use to create them -* The methods Active Record provides to manipulate your database -* The Rake tasks that manipulate them -* How they relate to +schema.rb+ +* The generators you can use to create them The methods Active Record provides +* to manipulate your database The Rake tasks that manipulate them How they +* relate to +schema.rb+ endprologue. h3. Anatomy of a Migration -Before we dive into the details of a migration, here are a few examples of the sorts of things you can do: +Before we dive into the details of a migration, here are a few examples of the +sorts of things you can do: class CreateProducts < ActiveRecord::Migration def up - create_table :products do |t| + create_table :products do |t| t.string :name t.text :description @@ -36,14 +48,20 @@ class CreateProducts < ActiveRecord::Migration end -This migration adds a table called +products+ with a string column called +name+ and a text column called +description+. A primary key column called +id+ will also be added, however since this is the default we do not need to ask for this. The timestamp columns +created_at+ and +updated_at+ which Active Record populates automatically will also be added. Reversing this migration is as simple as dropping the table. +This migration adds a table called +products+ with a string column called +name+ +and a text column called +description+. A primary key column called +id+ will +also be added, however since this is the default we do not need to ask for this. +The timestamp columns +created_at+ and +updated_at+ which Active Record +populates automatically will also be added. Reversing this migration is as +simple as dropping the table. -Migrations are not limited to changing the schema. You can also use them to fix bad data in the database or populate new fields: +Migrations are not limited to changing the schema. You can also use them to fix +bad data in the database or populate new fields: class AddReceiveNewsletterToUsers < ActiveRecord::Migration def up - change_table :users do |t| + change_table :users do |t| t.boolean :receive_newsletter, :default => false end User.update_all ["receive_newsletter = ?", true] @@ -55,17 +73,23 @@ class AddReceiveNewsletterToUsers < ActiveRecord::Migration end -NOTE: Some "caveats":#using-models-in-your-migrations apply to using models in your migrations. +NOTE: Some "caveats":#using-models-in-your-migrations apply to using models in +your migrations. -This migration adds a +receive_newsletter+ column to the +users+ table. We want it to default to +false+ for new users, but existing users are considered -to have already opted in, so we use the User model to set the flag to +true+ for existing users. +This migration adds a +receive_newsletter+ column to the +users+ table. We want +it to default to +false+ for new users, but existing users are considered to +have already opted in, so we use the User model to set the flag to +true+ for +existing users. -Rails 3.1 makes migrations smarter by providing a new change method. This method is preferred for writing constructive migrations (adding columns or tables). The migration knows how to migrate your database and reverse it when the migration is rolled back without the need to write a separate +down+ method. +Rails 3.1 makes migrations smarter by providing a new change method. +This method is preferred for writing constructive migrations (adding columns or +tables). The migration knows how to migrate your database and reverse it when +the migration is rolled back without the need to write a separate +down+ method. class CreateProducts < ActiveRecord::Migration def change - create_table :products do |t| + create_table :products do |t| t.string :name t.text :description @@ -77,64 +101,97 @@ end h4. Migrations are Classes -A migration is a subclass of ActiveRecord::Migration that implements two methods: +up+ (perform the required transformations) and +down+ (revert them). +A migration is a subclass of ActiveRecord::Migration that implements +two methods: +up+ (perform the required transformations) and +down+ (revert +them). -Active Record provides methods that perform common data definition tasks in a database independent way (you'll read about them in detail later): +Active Record provides methods that perform common data definition tasks in a +database independent way (you'll read about them in detail later): -* +create_table+ -* +change_table+ -* +drop_table+ -* +add_column+ -* +change_column+ -* +rename_column+ -* +remove_column+ -* +add_index+ -* +remove_index+ +* +create_table+ +change_table+ +drop_table+ +add_column+ +change_column+ +* +rename_column+ +remove_column+ +add_index+ +remove_index+ -If you need to perform tasks specific to your database (for example create a "foreign key":#active-record-and-referential-integrity constraint) then the +execute+ method allows you to execute arbitrary SQL. A migration is just a regular Ruby class so you're not limited to these functions. For example after adding a column you could write code to set the value of that column for existing records (if necessary using your models). +If you need to perform tasks specific to your database (for example create a +"foreign key":#active-record-and-referential-integrity constraint) then the ++execute+ method allows you to execute arbitrary SQL. A migration is just a +regular Ruby class so you're not limited to these functions. For example after +adding a column you could write code to set the value of that column for +existing records (if necessary using your models). -On databases that support transactions with statements that change the schema (such as PostgreSQL or SQLite3), migrations are wrapped in a transaction. If the database does not support this (for example MySQL) then when a migration fails the parts of it that succeeded will not be rolled back. You will have to unpick the changes that were made by hand. +On databases that support transactions with statements that change the schema +(such as PostgreSQL or SQLite3), migrations are wrapped in a transaction. If the +database does not support this (for example MySQL) then when a migration fails +the parts of it that succeeded will not be rolled back. You will have to unpick +the changes that were made by hand. h4. What's in a Name -Migrations are stored in files in +db/migrate+, one for each migration class. The name of the file is of the form +YYYYMMDDHHMMSS_create_products.rb+, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration. The name of the migration class (CamelCased version) should match the latter part of the file name. For example +20080906120000_create_products.rb+ should define class +CreateProducts+ and +20080906120001_add_details_to_products.rb+ should define +AddDetailsToProducts+. If you do feel the need to change the file name then you have to update the name of the class inside or Rails will complain about a missing class. - -Internally Rails only uses the migration's number (the timestamp) to identify them. Prior to Rails 2.1 the migration number started at 1 and was incremented each time a migration was generated. With multiple developers it was easy for these to clash requiring you to rollback migrations and renumber them. With Rails 2.1 this is largely avoided by using the creation time of the migration to identify them. You can revert to the old numbering scheme by adding the following line to +config/application.rb+. +Migrations are stored as files in the +db/migrate+ directory, one for each +migration class. The name of the file is of the form ++YYYYMMDDHHMMSS_create_products.rb+, that is to say a UTC timestamp +identifying the migration followed by an underscore followed by the name +of the migration. The name of the migration class (CamelCased version) +should match the latter part of the file name. For example ++20080906120000_create_products.rb+ should define class +CreateProducts+ and ++20080906120001_add_details_to_products.rb+ should define ++AddDetailsToProducts+. If you do feel the need to change the file name then you +have to update the name of the class inside or Rails will complain +about a missing class. + +Internally Rails only uses the migration's number (the timestamp) to identify +them. Prior to Rails 2.1 the migration number started at 1 and was incremented +each time a migration was generated. With multiple developers it was easy for +these to clash requiring you to rollback migrations and renumber them. With +Rails 2.1+ this is largely avoided by using the creation time of the migration +to identify them. You can revert to the old numbering scheme by adding the +following line to +config/application.rb+. config.active_record.timestamped_migrations = false -The combination of timestamps and recording which migrations have been run allows Rails to handle common situations that occur with multiple developers. +The combination of timestamps and recording which migrations have been run +allows Rails to handle common situations that occur with multiple developers. -For example Alice adds migrations +20080906120000+ and +20080906123000+ and Bob adds +20080906124500+ and runs it. Alice finishes her changes and checks in her migrations and Bob pulls down the latest changes. Rails knows that it has not run Alice's two migrations so +rake db:migrate+ would run them (even though Bob's migration with a later timestamp has been run), and similarly migrating down would not run their +down+ methods. +For example Alice adds migrations +20080906120000+ and +20080906123000+ and Bob +adds +20080906124500+ and runs it. Alice finishes her changes and checks in her +migrations and Bob pulls down the latest changes. Rails knows that it has not +run Alice's two migrations so +rake db:migrate+ would run them (even though +Bob's migration with a later timestamp has been run), and similarly migrating +down would not run their +down+ methods. -Of course this is no substitution for communication within the team. For example, if Alice's migration removed a table that Bob's migration assumed to exist, then trouble would certainly strike. +Of course this is no substitution for communication within the team. For +example, if Alice's migration removed a table that Bob's migration assumed to +exist, then trouble would certainly strike. h4. Changing Migrations -Occasionally you will make a mistake when writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run +rake db:migrate+. You must rollback the migration (for example with +rake db:rollback+), edit your migration and then run +rake db:migrate+ to run the corrected version. - -In general editing existing migrations is not a good idea: you will be creating extra work for yourself and your co-workers and cause major headaches if the existing version of the migration has already been run on production machines. Instead, you should write a new migration that performs the changes you require. Editing a freshly generated migration that has not yet been committed to source control (or, more generally, which has not been propagated beyond your development machine) is relatively harmless. +Occasionally you will make a mistake when writing a migration. If you have +already run the migration then you cannot just edit the migration and run the +migration again: Rails thinks it has already run the migration and so will do +nothing when you run +rake db:migrate+. You must rollback the migration (for +example with +rake db:rollback+), edit your migration and then run +rake +db:migrate+ to run the corrected version. + +In general editing existing migrations is not a good idea: you will be creating +extra work for yourself and your co-workers and cause major headaches if the +existing version of the migration has already been run on production machines. +Instead, you should write a new migration that performs the changes you require. +Editing a freshly generated migration that has not yet been committed to source +control (or, more generally, which has not been propagated beyond your +development machine) is relatively harmless. h4. Supported Types Active Record supports the following types: -* +:primary_key+ -* +:string+ -* +:text+ -* +:integer+ -* +:float+ -* +:decimal+ -* +:datetime+ -* +:timestamp+ -* +:time+ -* +:date+ -* +:binary+ -* +:boolean+ - -These will be mapped onto an appropriate underlying database type. For example, with MySQL the type +:string+ is mapped to +VARCHAR(255)+. You can create columns of types not supported by Active Record when using the non-sexy syntax, for example +* +:primary_key+ +:string+ +:text+ +:integer+ +:float+ +:decimal+ +:datetime+ +* +:timestamp+ +:time+ +:date+ +:binary+ +:boolean+ + +These will be mapped onto an appropriate underlying database type. For example, +with MySQL the type +:string+ is mapped to +VARCHAR(255)+. You can create +columns of types not supported by Active Record when using the non-sexy syntax, +for example create_table :products do |t| @@ -148,7 +205,10 @@ h3. Creating a Migration h4. Creating a Model -The model and scaffold generators will create migrations appropriate for adding a new model. This migration will already contain instructions for creating the relevant table. If you tell Rails what columns you want, then statements for adding these columns will also be created. For example, running +The model and scaffold generators will create migrations appropriate for adding +a new model. This migration will already contain instructions for creating the +relevant table. If you tell Rails what columns you want, then statements for +adding these columns will also be created. For example, running $ rails generate model Product name:string description:text @@ -169,14 +229,16 @@ class CreateProducts < ActiveRecord::Migration end -You can append as many column name/type pairs as you want. By default +t.timestamps+ (which creates the +updated_at+ and +created_at+ columns that -are automatically populated by Active Record) will be added for you. +You can append as many column name/type pairs as you want. By default ++t.timestamps+ (which creates the +updated_at+ and +created_at+ columns that are +automatically populated by Active Record) will be added for you. h4. Creating a Standalone Migration -If you are creating migrations for other purposes (for example to add a column to an existing table) then you can use the migration generator: +If you are creating migrations for other purposes (for example to add a column +to an existing table) then you can use the migration generator: - + $ rails generate migration AddPartNumberToProducts @@ -189,7 +251,9 @@ class AddPartNumberToProducts < ActiveRecord::Migration end -If the migration name is of the form "AddXXXToYYY" or "RemoveXXXFromYYY" and is followed by a list of column names and types then a migration containing the appropriate +add_column+ and +remove_column+ statements will be created. +If the migration name is of the form "AddXXXToYYY" or "RemoveXXXFromYYY" and is +followed by a list of column names and types then a migration containing the +appropriate +add_column+ and +remove_column+ statements will be created. $ rails generate migration AddPartNumberToProducts part_number:string @@ -223,7 +287,6 @@ class RemovePartNumberFromProducts < ActiveRecord::Migration add_column :products, :part_number, :string end end - You are not limited to one magically generated column, for example @@ -242,17 +305,22 @@ class AddDetailsToProducts < ActiveRecord::Migration end -As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit. +As always, what has been generated for you is just a starting point. You can add +or remove from it as you see fit. -NOTE: The generated migration file for destructive migrations will still be old-style using the +up+ and +down+ methods. This is because Rails doesn't know the original data types defined when you made the original changes. +NOTE: The generated migration file for destructive migrations will still be +old-style using the +up+ and +down+ methods. This is because Rails doesn't know +the original data types defined when you made the original changes. h3. Writing a Migration -Once you have created your migration using one of the generators it's time to get to work! +Once you have created your migration using one of the generators it's time to +get to work! h4. Creating a Table -Migration method +create_table+ will be one of your workhorses. A typical use would be +Migration method +create_table+ will be one of your workhorses. A typical use +would be create_table :products do |t| @@ -260,9 +328,11 @@ create_table :products do |t| end -which creates a +products+ table with a column called +name+ (and as discussed below, an implicit +id+ column). +which creates a +products+ table with a column called +name+ (and as discussed +below, an implicit +id+ column). -The object yielded to the block allows you to create columns on the table. There are two ways of doing it. The first (traditional) form looks like +The object yielded to the block allows you to create columns on the table. There +are two ways of doing it. The first (traditional) form looks like create_table :products do |t| @@ -270,7 +340,9 @@ create_table :products do |t| end -The second form, the so called "sexy" migration, drops the somewhat redundant +column+ method. Instead, the +string+, +integer+, etc. methods create a column of that type. Subsequent parameters are the same. +The second form, the so called "sexy" migration, drops the somewhat redundant ++column+ method. Instead, the +string+, +integer+, etc. methods create a column +of that type. Subsequent parameters are the same. create_table :products do |t| @@ -278,7 +350,12 @@ create_table :products do |t| end -By default, +create_table+ will create a primary key called +id+. You can change the name of the primary key with the +:primary_key+ option (don't forget to update the corresponding model) or, if you don't want a primary key at all (for example for a HABTM join table), you can pass the option +:id => false+. If you need to pass database specific options you can place an SQL fragment in the +:options+ option. For example, +By default, +create_table+ will create a primary key called +id+. You can change +the name of the primary key with the +:primary_key+ option (don't forget to +update the corresponding model) or, if you don't want a primary key at all (for +example for a HABTM join table), you can pass the option +:id => false+. If you +need to pass database specific options you can place an SQL fragment in the ++:options+ option. For example, create_table :products, :options => "ENGINE=BLACKHOLE" do |t| @@ -286,11 +363,14 @@ create_table :products, :options => "ENGINE=BLACKHOLE" do |t| end -will append +ENGINE=BLACKHOLE+ to the SQL statement used to create the table (when using MySQL, the default is +ENGINE=InnoDB+). +will append +ENGINE=BLACKHOLE+ to the SQL statement used to create the table +(when using MySQL, the default is +ENGINE=InnoDB+). h4. Changing Tables -A close cousin of +create_table+ is +change_table+, used for changing existing tables. It is used in a similar fashion to +create_table+ but the object yielded to the block knows more tricks. For example +A close cousin of +create_table+ is +change_table+, used for changing existing +tables. It is used in a similar fashion to +create_table+ but the object yielded +to the block knows more tricks. For example change_table :products do |t| @@ -301,29 +381,27 @@ change_table :products do |t| end -removes the +description+ and +name+ columns, creates a +part_number+ column and adds an index on it. Finally it renames the +upccode+ column. This is the same as doing - - -remove_column :products, :description -remove_column :products, :name -add_column :products, :part_number, :string -add_index :products, :part_number -rename_column :products, :upccode, :upc_code - - -You don't have to keep repeating the table name and it groups all the statements related to modifying one particular table. The individual transformation names are also shorter, for example +remove_column+ becomes just +remove+ and +add_index+ becomes just +index+. +You don't have to keep repeating the table name and it groups all the statements +related to modifying one particular table. The individual transformation names +are also shorter, for example +remove_column+ becomes just +remove+ and ++add_index+ becomes just +index+. h4. Special Helpers -Active Record provides some shortcuts for common functionality. It is for example very common to add both the +created_at+ and +updated_at+ columns and so there is a method that does exactly that: +Active Record provides some shortcuts for common functionality. It is for +example very common to add both the +created_at+ and +updated_at+ columns and so +there is a method that does exactly that: create_table :products do |t| t.timestamps end -will create a new products table with those two columns (plus the +id+ column) whereas +will create a new products table with those two columns (plus the +id+ column) +whereas + +The other helper is called +references+ (also available as +belongs_to+). In its change_table :products do |t| t.timestamps @@ -331,7 +409,7 @@ end adds those columns to an existing table. -The other helper is called +references+ (also available as +belongs_to+). In its simplest form it just adds some readability +simplest form it just adds some readability create_table :products do |t| @@ -339,43 +417,60 @@ create_table :products do |t| end -will create a +category_id+ column of the appropriate type. Note that you pass the model name, not the column name. Active Record adds the +_id+ for you. If you have polymorphic +belongs_to+ associations then +references+ will add both of the columns required: +will create a +category_id+ column of the appropriate type. Note that you pass +the model name, not the column name. Active Record adds the +_id+ for you. If +you have polymorphic +belongs_to+ associations then +references+ will add both +of the columns required: create_table :products do |t| t.references :attachment, :polymorphic => {:default => 'Photo'} end -will add an +attachment_id+ column and a string +attachment_type+ column with a default value of 'Photo'. -NOTE: The +references+ helper does not actually create foreign key constraints for you. You will need to use +execute+ or a plugin that adds "foreign key support":#active-record-and-referential-integrity. +will add an +attachment_id+ column and a string +attachment_type+ column with +a default value of 'Photo'. -If the helpers provided by Active Record aren't enough you can use the +execute+ method to execute arbitrary SQL. +NOTE: The +references+ helper does not actually create foreign key constraints +for you. You will need to use +execute+ or a plugin that adds "foreign key +support":#active-record-and-referential-integrity. -For more details and examples of individual methods, check the API documentation, in particular the documentation for "ActiveRecord::ConnectionAdapters::SchemaStatements":http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html (which provides the methods available in the +up+ and +down+ methods), "ActiveRecord::ConnectionAdapters::TableDefinition":http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html (which provides the methods available on the object yielded by +create_table+) and "ActiveRecord::ConnectionAdapters::Table":http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html (which provides the methods available on the object yielded by +change_table+). +If the helpers provided by Active Record aren't enough you can use the +execute+ +method to execute arbitrary SQL. + +For more details and examples of individual methods, check the API documentation, +in particular the documentation for +"ActiveRecord::ConnectionAdapters::SchemaStatements":http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html +(which provides the methods available in the +up+ and +down+ methods), +"ActiveRecord::ConnectionAdapters::TableDefinition":http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html +(which provides the methods available on the object yielded by +create_table+) +and +"ActiveRecord::ConnectionAdapters::Table":http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html +(which provides the methods available on the object yielded by +change_table+). h4. Writing Your +change+ Method -The +change+ method removes the need to write both +up+ and +down+ methods in those cases that Rails know how to revert the changes automatically. Currently, the +change+ method supports only these migration definitions: +The +change+ method removes the need to write both +up+ and +down+ methods in +those cases that Rails know how to revert the changes automatically. Currently, +the +change+ method supports only these migration definitions: -* +add_column+ -* +add_index+ -* +add_timestamps+ -* +create_table+ -* +remove_timestamps+ -* +rename_column+ -* +rename_index+ -* +rename_table+ +* +add_column+ +add_index+ +add_timestamps+ +create_table+ +remove_timestamps+ +* +rename_column+ +rename_index+ +rename_table+ -If you're going to use other methods, you'll have to write the +up+ and +down+ methods normally. +If you're going to use other methods, you'll have to write the +up+ and +down+ +methods normally. h4. Writing Your +down+ Method -The +down+ method of your migration should revert the transformations done by the +up+ method. In other words, the database schema should be unchanged if you do an +up+ followed by a +down+. For example, if you create a table in the +up+ method, you should drop it in the +down+ method. It is wise to reverse the transformations in precisely the reverse order they were made in the +up+ method. For example, +The +down+ method of your migration should revert the transformations done by +the +up+ method. In other words, the database schema should be unchanged if you +do an +up+ followed by a +down+. For example, if you create a table in the +up+ +method, you should drop it in the +down+ method. It is wise to reverse the +transformations in precisely the reverse order they were made in the +up+ +method. For example, class ExampleMigration < ActiveRecord::Migration - def up create_table :products do |t| t.references :category @@ -387,47 +482,65 @@ class ExampleMigration < ActiveRecord::Migration FOREIGN KEY (category_id) REFERENCES categories(id) SQL - add_column :users, :home_page_url, :string - rename_column :users, :email, :email_address end def down rename_column :users, :email_address, :email remove_column :users, :home_page_url - execute "ALTER TABLE products DROP FOREIGN KEY fk_products_categories" + execute <<-SQL + ALTER TABLE products + DROP FOREIGN KEY fk_products_categories + SQL drop_table :products end end -Sometimes your migration will do something which is just plain irreversible; for example, it might destroy some data. In such cases, you can raise +ActiveRecord::IrreversibleMigration+ from your +down+ method. If someone tries to revert your migration, an error message will be displayed saying that it can't be done. +Sometimes your migration will do something which is just plain irreversible; for +example, it might destroy some data. In such cases, you can raise ++ActiveRecord::IrreversibleMigration+ from your +down+ method. If someone tries +to revert your migration, an error message will be displayed saying that it +can't be done. h3. Running Migrations -Rails provides a set of rake tasks to work with migrations which boil down to running certain sets of migrations. The very first migration related rake task you will use will probably be +db:migrate+. In its most basic form it just runs the +up+ method for all the migrations that have not yet been run. If there are no such migrations, it exits. +Rails provides a set of rake tasks to work with migrations which boil down to +running certain sets of migrations. The very first migration related rake task +you will use will probably be +db:migrate+. In its most basic form it just runs +the +up+ method for all the migrations that have not yet been run. If there are +no such migrations, it exits. -Note that running the +db:migrate+ also invokes the +db:schema:dump+ task, which will update your db/schema.rb file to match the structure of your database. +Note that running the +db:migrate+ also invokes the +db:schema:dump+ task, which +will update your db/schema.rb file to match the structure of your database. -If you specify a target version, Active Record will run the required migrations (up or down) until it has reached the specified version. The -version is the numerical prefix on the migration's filename. For example, to migrate to version 20080906120000 run +If you specify a target version, Active Record will run the required migrations +(up or down) until it has reached the specified version. The version is the +numerical prefix on the migration's filename. For example, to migrate to version +20080906120000 run $ rake db:migrate VERSION=20080906120000 -If version 20080906120000 is greater than the current version (i.e., it is migrating upwards), this will run the +up+ method on all migrations up to and including 20080906120000. If migrating downwards, this will run the +down+ method on all the migrations down to, but not including, 20080906120000. +If version 20080906120000 is greater than the current version (i.e., it is +migrating upwards), this will run the +up+ method on all migrations up to and +including 20080906120000. If migrating downwards, this will run the +down+ +method on all the migrations down to, but not including, 20080906120000. h4. Rolling Back -A common task is to rollback the last migration, for example if you made a mistake in it and wish to correct it. Rather than tracking down the version number associated with the previous migration you can run +A common task is to rollback the last migration, for example if you made a +mistake in it and wish to correct it. Rather than tracking down the version +number associated with the previous migration you can run $ rake db:rollback -This will run the +down+ method from the latest migration. If you need to undo several migrations you can provide a +STEP+ parameter: +This will run the +down+ method from the latest migration. If you need to undo +several migrations you can provide a +STEP+ parameter: $ rake db:rollback STEP=3 @@ -435,46 +548,59 @@ $ rake db:rollback STEP=3 will run the +down+ method from the last 3 migrations. -The +db:migrate:redo+ task is a shortcut for doing a rollback and then migrating back up again. As with the +db:rollback+ task, you can use the +STEP+ parameter if you need to go more than one version back, for example +The +db:migrate:redo+ task is a shortcut for doing a rollback and then migrating +back up again. As with the +db:rollback+ task, you can use the +STEP+ parameter +if you need to go more than one version back, for example $ rake db:migrate:redo STEP=3 -Neither of these Rake tasks do anything you could not do with +db:migrate+. They are simply more convenient, since you do not need to explicitly specify the version to migrate to. +Neither of these Rake tasks do anything you could not do with +db:migrate+. They +are simply more convenient, since you do not need to explicitly specify the +version to migrate to. -Lastly, the +db:reset+ task will drop the database, recreate it and load the current schema into it. +Lastly, the +db:reset+ task will drop the database, recreate it and load the +current schema into it. -NOTE: This is not the same as running all the migrations - see the section on "schema.rb":#schema-dumping-and-you. +NOTE: This is not the same as running all the migrations - see the section on +"schema.rb":#schema-dumping-and-you. h4. Being Specific -If you need to run a specific migration up or down, the +db:migrate:up+ and +db:migrate:down+ tasks will do that. Just specify the appropriate version and the corresponding migration will have its +up+ or +down+ method invoked, for example, +If you need to run a specific migration up or down, the +db:migrate:up+ and ++db:migrate:down+ tasks will do that. Just specify the appropriate version and +the corresponding migration will have its +up+ or +down+ method invoked, for +example, $ rake db:migrate:up VERSION=20080906120000 -will run the +up+ method from the 20080906120000 migration. These tasks check whether the migration has already run, so for example +db:migrate:up VERSION=20080906120000+ will do nothing if Active Record believes that 20080906120000 has already been run. +will run the +up+ method from the 20080906120000 migration. These tasks check +whether the migration has already run, so for example +db:migrate:up +VERSION=20080906120000+ will do nothing if Active Record believes that +20080906120000 has already been run. h4. Being Talkative -By default migrations tell you exactly what they're doing and how long it took. A migration creating a table and adding an index might produce output like this +By default migrations tell you exactly what they're doing and how long it took. +A migration creating a table and adding an index might produce output like this -20080906170109 CreateProducts: migrating +== CreateProducts: migrating ================================================= -- create_table(:products) - -> 0.0021s --- add_index(:products, :name) - -> 0.0026s -20080906170109 CreateProducts: migrated (0.0059s) + -> 0.0028s +== CreateProducts: migrated (0.0028s) ======================================== Several methods are provided that allow you to control all this: -* +suppress_messages+ takes a block as an argument and suppresses any output generated by the block. -* +say+ takes a message argument and outputs it as is. A second boolean argument can be passed to specify whether to indent or not. -* +say_with_time+ outputs text along with how long it took to run its block. If the block returns an integer it assumes it is the number of rows affected. +* +suppress_messages+ takes a block as an argument and suppresses any output +* generated by the block. +say+ takes a message argument and outputs it as is. +* A second boolean argument can be passed to specify whether to indent or not. +* +say_with_time+ outputs text along with how long it took to run its block. If +* the block returns an integer it assumes it is the number of rows affected. For example, this migration @@ -502,37 +628,46 @@ end generates the following output -20080906170109 CreateProducts: migrating - Created a table +== CreateProducts: migrating ================================================= +-- Created a table -> and an index! - Waiting for a while - -> 10.0001s +-- Waiting for a while + -> 10.0013s -> 250 rows -20080906170109 CreateProducts: migrated (10.0097s) +== CreateProducts: migrated (10.0054s) ======================================= -If you just want Active Record to shut up, then running +rake db:migrate VERBOSE=false+ will suppress all output. +If you just want Active Record to shut up, then running +rake db:migrate +VERBOSE=false+ will suppress all output. h3. Using Models in Your Migrations -When creating or updating data in a migration it is often tempting to use one of your models. After all, they exist to provide easy access to the underlying data. This can be done, but some caution should be observed. +When creating or updating data in a migration it is often tempting to use one of +your models. After all, they exist to provide easy access to the underlying +data. This can be done, but some caution should be observed. -For example, problems occur when the model uses database columns which are (1) not currently in the database and (2) will be created by this or a subsequent migration. +For example, problems occur when the model uses database columns which are (1) +not currently in the database and (2) will be created by this or a subsequent +migration. -Consider this example, where Alice and Bob are working on the same code base which contains a +Product+ model: +Consider this example, where Alice and Bob are working on the same code base +which contains a +Product+ model: Bob goes on vacation. -Alice creates a migration for the +products+ table which adds a new column and initializes it. -She also adds a validation to the +Product+ model for the new column. +Alice creates a migration for the +products+ table which adds a new column and +initializes it. She also adds a validation to the +Product+ model for the new +column. # db/migrate/20100513121110_add_flag_to_product.rb class AddFlagToProduct < ActiveRecord::Migration def change - add_column :products, :flag, :int - Product.all.each { |f| f.update_attributes!(:flag => 'false') } + add_column :products, :flag, :boolean + Product.all.each do |product| + product.update_attributes!(:flag => 'false') + end end end @@ -545,7 +680,9 @@ class Product < ActiveRecord::Base end -Alice adds a second migration which adds and initializes another column to the +products+ table and also adds a validation to the +Product+ model for the new column. +Alice adds a second migration which adds and initializes another column to the ++products+ table and also adds a validation to the +Product+ model for the new +column. # db/migrate/20100515121110_add_fuzz_to_product.rb @@ -553,7 +690,9 @@ Alice adds a second migration which adds and initializes another column to the + class AddFuzzToProduct < ActiveRecord::Migration def change add_column :products, :fuzz, :string - Product.all.each { |f| f.update_attributes! :fuzz => 'fuzzy' } + Product.all.each do |product| + product.update_attributes! :fuzz => 'fuzzy' + end end end @@ -570,10 +709,14 @@ Both migrations work for Alice. Bob comes back from vacation and: -# updates the source - which contains both migrations and the latests version of the Product model. -# runs outstanding migrations with +rake db:migrate+, which includes the one that updates the +Product+ model. +# Updates the source - which contains both migrations and the latests version of +the Product model. +# Runs outstanding migrations with +rake db:migrate+, which +includes the one that updates the +Product+ model. -The migration crashes because when the model attempts to save, it tries to validate the second added column, which is not in the database when the _first_ migration runs: +The migration crashes because when the model attempts to save, it tries to +validate the second added column, which is not in the database when the _first_ +migration runs: rake aborted! @@ -582,23 +725,28 @@ An error has occurred, this and all later migrations canceled: undefined method `fuzz' for # -A fix for this is to create a local model within the migration. This keeps rails from running the validations, so that the migrations run to completion. +A fix for this is to create a local model within the migration. This keeps rails +from running the validations, so that the migrations run to completion. -When using a faux model, it's a good idea to call +Product.reset_column_information+ to refresh the +ActiveRecord+ cache for the +Product+ model prior to updating data in the database. +When using a faux model, it's a good idea to call ++Product.reset_column_information+ to refresh the +ActiveRecord+ cache for the ++Product+ model prior to updating data in the database. If Alice had done this instead, there would have been no problem: # db/migrate/20100513121110_add_flag_to_product.rb -class AddFlagToProduct < ActiveRecord::Migration +class AddFlagToProduct < ActiveRecord::Migration class Product < ActiveRecord::Base end def change - add_column :products, :flag, :int + add_column :products, :flag, :integer Product.reset_column_information - Product.all.each { |f| f.update_attributes!(:flag => false) } + Product.all.each do |product| + product.update_attributes!(:flag => false) + end end end @@ -609,32 +757,49 @@ end class AddFuzzToProduct < ActiveRecord::Migration class Product < ActiveRecord::Base end + def change add_column :products, :fuzz, :string Product.reset_column_information - Product.all.each { |f| f.update_attributes! :fuzz => 'fuzzy' } + Product.all.each do |product| + product.update_attributes!(:fuzz => 'fuzzy') + end end end - h3. Schema Dumping and You h4. What are Schema Files for? -Migrations, mighty as they may be, are not the authoritative source for your database schema. That role falls to either +db/schema.rb+ or an SQL file which Active Record generates by examining the database. They are not designed to be edited, they just represent the current state of the database. +Migrations, mighty as they may be, are not the authoritative source for your +database schema. That role falls to either +db/schema.rb+ or an SQL file which +Active Record generates by examining the database. They are not designed to be +edited, they just represent the current state of the database. -There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire migration history. It is much simpler and faster to just load into the database a description of the current schema. +There is no need (and it is error prone) to deploy a new instance of an app by +replaying the entire migration history. It is much simpler and faster to just +load into the database a description of the current schema. -For example, this is how the test database is created: the current development database is dumped (either to +db/schema.rb+ or +db/development.sql+) and then loaded into the test database. +For example, this is how the test database is created: the current development +database is dumped (either to +db/schema.rb+ or +db/development.sql+) and then +loaded into the test database. -Schema files are also useful if you want a quick look at what attributes an Active Record object has. This information is not in the model's code and is frequently spread across several migrations, but is summed up in the schema file. The "annotate_models":https://github.com/ctran/annotate_models gem automatically adds and updates comments at the top of each model summarizing the schema if you desire that functionality. +Schema files are also useful if you want a quick look at what attributes an +Active Record object has. This information is not in the model's code and is +frequently spread across several migrations, but is summed up in the schema +file. The "annotate_models":https://github.com/ctran/annotate_models gem +automatically adds and updates comments at the top of each model summarizing the +schema if you desire that functionality. h4. Types of Schema Dumps -There are two ways to dump the schema. This is set in +config/application.rb+ by the +config.active_record.schema_format+ setting, which may be either +:sql+ or +:ruby+. +There are two ways to dump the schema. This is set in +config/application.rb+ by +the +config.active_record.schema_format+ setting, which may be either +:sql+ or ++:ruby+. -If +:ruby+ is selected then the schema is stored in +db/schema.rb+. If you look at this file you'll find that it looks an awful lot like one very big migration: +If +:ruby+ is selected then the schema is stored in +db/schema.rb+. If you look +at this file you'll find that it looks an awful lot like one very big migration: ActiveRecord::Schema.define(:version => 20080906171750) do @@ -646,28 +811,57 @@ ActiveRecord::Schema.define(:version => 20080906171750) do create_table "products", :force => true do |t| t.string "name" - t.text "description" + t.text "description" t.datetime "created_at" t.datetime "updated_at" - t.string "part_number" + t.string "part_number" end end -In many ways this is exactly what it is. This file is created by inspecting the database and expressing its structure using +create_table+, +add_index+, and so on. Because this is database-independent, it could be loaded into any database that Active Record supports. This could be very useful if you were to distribute an application that is able to run against multiple databases. - -There is however a trade-off: +db/schema.rb+ cannot express database specific items such as foreign key constraints, triggers, or stored procedures. While in a migration you can execute custom SQL statements, the schema dumper cannot reconstitute those statements from the database. If you are using features like this, then you should set the schema format to +:sql+. - -Instead of using Active Record's schema dumper, the database's structure will be dumped using a tool specific to the database (via the +db:structure:dump+ Rake task) into +db/structure.sql+. For example, for the PostgreSQL RDBMS, the +pg_dump+ utility is used. For MySQL, this file will contain the output of +SHOW CREATE TABLE+ for the various tables. Loading these schemas is simply a question of executing the SQL statements they contain. By definition, this will create a perfect copy of the database's structure. Using the +:sql+ schema format will, however, prevent loading the schema into a RDBMS other than the one used to create it. +In many ways this is exactly what it is. This file is created by inspecting the +database and expressing its structure using +create_table+, +add_index+, and so +on. Because this is database-independent, it could be loaded into any database +that Active Record supports. This could be very useful if you were to distribute +an application that is able to run against multiple databases. + +There is however a trade-off: +db/schema.rb+ cannot express database specific +items such as foreign key constraints, triggers, or stored procedures. While in +a migration you can execute custom SQL statements, the schema dumper cannot +reconstitute those statements from the database. If you are using features like +this, then you should set the schema format to +:sql+. + +Instead of using Active Record's schema dumper, the database's structure will be +dumped using a tool specific to the database (via the +db:structure:dump+ Rake +task) into +db/structure.sql+. For example, for the PostgreSQL RDBMS, the ++pg_dump+ utility is used. For MySQL, this file will contain the output of +SHOW +CREATE TABLE+ for the various tables. Loading these schemas is simply a question +of executing the SQL statements they contain. By definition, this will create a +perfect copy of the database's structure. Using the +:sql+ schema format will, +however, prevent loading the schema into a RDBMS other than the one used to +create it. h4. Schema Dumps and Source Control -Because schema dumps are the authoritative source for your database schema, it is strongly recommended that you check them into source control. +Because schema dumps are the authoritative source for your database schema, it +is strongly recommended that you check them into source control. h3. Active Record and Referential Integrity -The Active Record way claims that intelligence belongs in your models, not in the database. As such, features such as triggers or foreign key constraints, which push some of that intelligence back into the database, are not heavily used. - -Validations such as +validates :foreign_key, :uniqueness => true+ are one way in which models can enforce data integrity. The +:dependent+ option on associations allows models to automatically destroy child objects when the parent is destroyed. Like anything which operates at the application level, these cannot guarantee referential integrity and so some people augment them with foreign key constraints. - -Although Active Record does not provide any tools for working directly with such features, the +execute+ method can be used to execute arbitrary SQL. You could also use some plugin like "foreigner":https://github.com/matthuhiggins/foreigner which add foreign key support to Active Record (including support for dumping foreign keys in +db/schema.rb+). +The Active Record way claims that intelligence belongs in your models, not in +the database. As such, features such as triggers or foreign key constraints, +which push some of that intelligence back into the database, are not heavily +used. + +Validations such as +validates :foreign_key, :uniqueness => true+ are one way in +which models can enforce data integrity. The +:dependent+ option on associations +allows models to automatically destroy child objects when the parent is +destroyed. Like anything which operates at the application level, these cannot +guarantee referential integrity and so some people augment them with foreign key +constraints. + +Although Active Record does not provide any tools for working directly with such +features, the +execute+ method can be used to execute arbitrary SQL. You could +also use some plugin like "foreigner":https://github.com/matthuhiggins/foreigner +which add foreign key support to Active Record (including support for dumping +foreign keys in +db/schema.rb+). -- cgit v1.2.3 From f9cc3f0d8ea26de06f57ee33a575733a72dc7bd7 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:01:53 -0700 Subject: Improve wording, fix some formatting issues --- railties/guides/source/migrations.textile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 13fc76359f..4f52e3ee8d 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -18,11 +18,12 @@ is database independent: you don't need to worry about the precise syntax of drop down to raw SQL for database specific features). For example you could use SQLite3 in development, but MySQL in production. -You'll learn all about migrations including: +In this guide, you'll learn all about migrations including: -* The generators you can use to create them The methods Active Record provides -* to manipulate your database The Rake tasks that manipulate them How they -* relate to +schema.rb+ +* The generators you can use to create them +* The methods Active Record provides to manipulate your database +* The Rake tasks that manipulate them +* How they relate to +schema.rb+ endprologue. -- cgit v1.2.3 From 78f6672a43ac556c19f8d4c9a6645911daa3bd1c Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:03:01 -0700 Subject: Alphabetize fields to make reading easier --- railties/guides/source/migrations.textile | 39 ++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 4f52e3ee8d..e6401a4bb7 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -109,8 +109,15 @@ them). Active Record provides methods that perform common data definition tasks in a database independent way (you'll read about them in detail later): -* +create_table+ +change_table+ +drop_table+ +add_column+ +change_column+ -* +rename_column+ +remove_column+ +add_index+ +remove_index+ +* +add_column+ +* +add_index+ +* +change_column+ +* +change_table+ +* +create_table+ +* +drop_table+ +* +remove_column+ +* +remove_index+ +* +rename_column+ If you need to perform tasks specific to your database (for example create a "foreign key":#active-record-and-referential-integrity constraint) then the @@ -184,10 +191,20 @@ development machine) is relatively harmless. h4. Supported Types -Active Record supports the following types: - -* +:primary_key+ +:string+ +:text+ +:integer+ +:float+ +:decimal+ +:datetime+ -* +:timestamp+ +:time+ +:date+ +:binary+ +:boolean+ +Active Record supports the following database column types: + +* +:binary+ +* +:boolean+ +* +:date+ +* +:datetime+ +* +:decimal+ +* +:float+ +* +:integer+ +* +:primary_key+ +* +:string+ +* +:text+ +* +:time+ +* +:timestamp+ These will be mapped onto an appropriate underlying database type. For example, with MySQL the type +:string+ is mapped to +VARCHAR(255)+. You can create @@ -455,8 +472,14 @@ The +change+ method removes the need to write both +up+ and +down+ methods in those cases that Rails know how to revert the changes automatically. Currently, the +change+ method supports only these migration definitions: -* +add_column+ +add_index+ +add_timestamps+ +create_table+ +remove_timestamps+ -* +rename_column+ +rename_index+ +rename_table+ +* +add_column+ +* +add_index+ +* +add_timestamps+ +* +create_table+ +* +remove_timestamps+ +* +rename_column+ +* +rename_index+ +* +rename_table+ If you're going to use other methods, you'll have to write the +up+ and +down+ methods normally. -- cgit v1.2.3 From c8f8ecfd38e20ee936ea4ccbb879acf006fe3cb8 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:03:26 -0700 Subject: Rollback is used elsewhere in the tutorial --- railties/guides/source/migrations.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index e6401a4bb7..ee0aa1d0f1 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -129,7 +129,7 @@ existing records (if necessary using your models). On databases that support transactions with statements that change the schema (such as PostgreSQL or SQLite3), migrations are wrapped in a transaction. If the database does not support this (for example MySQL) then when a migration fails -the parts of it that succeeded will not be rolled back. You will have to unpick +the parts of it that succeeded will not be rolled back. You will have to rollback the changes that were made by hand. h4. What's in a Name -- cgit v1.2.3 From 1f765a2f720c63b8b0e937a100837d27731719aa Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:10:49 -0700 Subject: Re-word confusing section --- railties/guides/source/migrations.textile | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index ee0aa1d0f1..2654b43cad 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -163,10 +163,9 @@ allows Rails to handle common situations that occur with multiple developers. For example Alice adds migrations +20080906120000+ and +20080906123000+ and Bob adds +20080906124500+ and runs it. Alice finishes her changes and checks in her -migrations and Bob pulls down the latest changes. Rails knows that it has not -run Alice's two migrations so +rake db:migrate+ would run them (even though -Bob's migration with a later timestamp has been run), and similarly migrating -down would not run their +down+ methods. +migrations and Bob pulls down the latest changes. When Bob runs +rake +db:migrate+, Rails knows that it has not run Alice's two migrations so it +executes the +up+ method for each migration. Of course this is no substitution for communication within the team. For example, if Alice's migration removed a table that Bob's migration assumed to -- cgit v1.2.3 From b3f8cbd9a8d95cfcaf5ac632e83041cad9154dde Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:11:22 -0700 Subject: Explain the t.timestamps method a little better --- railties/guides/source/migrations.textile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 2654b43cad..f271f3f89c 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -246,9 +246,10 @@ class CreateProducts < ActiveRecord::Migration end -You can append as many column name/type pairs as you want. By default -+t.timestamps+ (which creates the +updated_at+ and +created_at+ columns that are -automatically populated by Active Record) will be added for you. +You can append as many column name/type pairs as you want. By default, the +generated migration will include +t.timestamps+ (which creates the ++updated_at+ and +created_at+ columns that are automatically populated +by Active Record). h4. Creating a Standalone Migration -- cgit v1.2.3 From ec2727281c2b90e0431e6ebb6297c1274fa521e1 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:11:51 -0700 Subject: Added "also" to read better --- railties/guides/source/migrations.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index f271f3f89c..e83742428e 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -254,7 +254,7 @@ by Active Record). h4. Creating a Standalone Migration If you are creating migrations for other purposes (for example to add a column -to an existing table) then you can use the migration generator: +to an existing table) then you can also use the migration generator: $ rails generate migration AddPartNumberToProducts -- cgit v1.2.3 From bc6d1ebc66dcb15afa491a935ed094aff5948db5 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:12:23 -0700 Subject: It's more that Rails needs to know how to recreate the columns --- railties/guides/source/migrations.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index e83742428e..74d1b2cd8b 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -327,7 +327,7 @@ As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit. NOTE: The generated migration file for destructive migrations will still be -old-style using the +up+ and +down+ methods. This is because Rails doesn't know +old-style using the +up+ and +down+ methods. This is because Rails needs to know the original data types defined when you made the original changes. h3. Writing a Migration -- cgit v1.2.3 From a41295fdaa1d2da3c8ca90b5ad7babbcc21aef14 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:13:34 -0700 Subject: Correct grammatical errors --- railties/guides/source/migrations.textile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 74d1b2cd8b..3595ed90f4 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -372,7 +372,7 @@ By default, +create_table+ will create a primary key called +id+. You can change the name of the primary key with the +:primary_key+ option (don't forget to update the corresponding model) or, if you don't want a primary key at all (for example for a HABTM join table), you can pass the option +:id => false+. If you -need to pass database specific options you can place an SQL fragment in the +need to pass database specific options you can place a SQL fragment in the +:options+ option. For example, @@ -797,7 +797,7 @@ h3. Schema Dumping and You h4. What are Schema Files for? Migrations, mighty as they may be, are not the authoritative source for your -database schema. That role falls to either +db/schema.rb+ or an SQL file which +database schema. That role falls to either +db/schema.rb+ or a SQL file which Active Record generates by examining the database. They are not designed to be edited, they just represent the current state of the database. -- cgit v1.2.3 From 74807383aab8ec42210153a81a36436eed70f656 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:15:52 -0700 Subject: Reword confusing section --- railties/guides/source/migrations.textile | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 3595ed90f4..cfd89721ad 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -399,10 +399,8 @@ change_table :products do |t| end -You don't have to keep repeating the table name and it groups all the statements -related to modifying one particular table. The individual transformation names -are also shorter, for example +remove_column+ becomes just +remove+ and -+add_index+ becomes just +index+. +removes the +description+ and +name+ columns, creates a +part_number+ string +column and adds an index on it. Finally it renames the +upccode+ column. h4. Special Helpers -- cgit v1.2.3 From eedb86367483845fd383c5d74b4e677ef4563f59 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:16:20 -0700 Subject: Change The other... to Another... --- railties/guides/source/migrations.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index cfd89721ad..2c98009841 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -417,7 +417,6 @@ end will create a new products table with those two columns (plus the +id+ column) whereas -The other helper is called +references+ (also available as +belongs_to+). In its change_table :products do |t| t.timestamps @@ -425,6 +424,7 @@ end adds those columns to an existing table. +Another helper is called +references+ (also available as +belongs_to+). In its simplest form it just adds some readability -- cgit v1.2.3 From f7740f20f2266be00d3e0a8dbb82a9bbb402de70 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:17:00 -0700 Subject: We're using the method, not writing it --- railties/guides/source/migrations.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 2c98009841..a159a72659 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -464,7 +464,7 @@ and "ActiveRecord::ConnectionAdapters::Table":http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html (which provides the methods available on the object yielded by +change_table+). -h4. Writing Your +change+ Method +h4. Using the +change+ Method The +change+ method removes the need to write both +up+ and +down+ methods in those cases that Rails know how to revert the changes automatically. Currently, -- cgit v1.2.3 From aca34b8e514a375bbcb9899fa598d0d97c662990 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:18:27 -0700 Subject: Change wording to be more understandable --- railties/guides/source/migrations.textile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index a159a72659..18920f1d2e 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -479,8 +479,8 @@ the +change+ method supports only these migration definitions: * +rename_index+ * +rename_table+ -If you're going to use other methods, you'll have to write the +up+ and +down+ -methods normally. +If you're going to need to use any other methods, you'll have to write the ++up+ and +down+ methods instead of using the change method. h4. Writing Your +down+ Method -- cgit v1.2.3 From f41d099720dd2031341ea355116a7d4e73bee11a Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:18:45 -0700 Subject: We're using methods, not writing them --- railties/guides/source/migrations.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 18920f1d2e..7186e9998c 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -482,7 +482,7 @@ the +change+ method supports only these migration definitions: If you're going to need to use any other methods, you'll have to write the +up+ and +down+ methods instead of using the change method. -h4. Writing Your +down+ Method +h4. Using the +up+/+down+ Methods The +down+ method of your migration should revert the transformations done by the +up+ method. In other words, the database schema should be unchanged if you -- cgit v1.2.3 From 4130fe3a5bb85053e4729a16c2aafdd50faa7817 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:19:17 -0700 Subject: Reword section to make it more readable. --- railties/guides/source/migrations.textile | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 7186e9998c..02f6557bb8 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -529,10 +529,13 @@ can't be done. h3. Running Migrations Rails provides a set of rake tasks to work with migrations which boil down to -running certain sets of migrations. The very first migration related rake task -you will use will probably be +db:migrate+. In its most basic form it just runs -the +up+ method for all the migrations that have not yet been run. If there are -no such migrations, it exits. +running certain sets of migrations. + +The very first migration related rake task you will use will probably be ++rake db:migrate+. In its most basic form it just runs the +up+ or +change+ +method for all the migrations that have not yet been run. If there are +no such migrations, it exits. It will run these migrations in order based +on the date of the migration. Note that running the +db:migrate+ also invokes the +db:schema:dump+ task, which will update your db/schema.rb file to match the structure of your database. -- cgit v1.2.3 From 93d9845c30c3b3080698ff72738feeaab1aceed4 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:19:59 -0700 Subject: Migrations can also run the "change" method --- railties/guides/source/migrations.textile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 02f6557bb8..381c1055d9 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -541,9 +541,9 @@ Note that running the +db:migrate+ also invokes the +db:schema:dump+ task, which will update your db/schema.rb file to match the structure of your database. If you specify a target version, Active Record will run the required migrations -(up or down) until it has reached the specified version. The version is the -numerical prefix on the migration's filename. For example, to migrate to version -20080906120000 run +(up or down or change) until it has reached the specified version. The version +is the numerical prefix on the migration's filename. For example, to migrate +to version 20080906120000 run $ rake db:migrate VERSION=20080906120000 -- cgit v1.2.3 From d8c8bf5f05f50ef5e4709941bcb7a7ab97e44cee Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:20:40 -0700 Subject: Change wording to be more explicit on what migrating with a version does --- railties/guides/source/migrations.textile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 381c1055d9..5e4d948b24 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -551,8 +551,9 @@ $ rake db:migrate VERSION=20080906120000 If version 20080906120000 is greater than the current version (i.e., it is migrating upwards), this will run the +up+ method on all migrations up to and -including 20080906120000. If migrating downwards, this will run the +down+ -method on all the migrations down to, but not including, 20080906120000. +including 20080906120000, and will not execute any later migrations. If +migrating downwards, this will run the +down+ method on all the migrations +down to, but not including, 20080906120000. h4. Rolling Back -- cgit v1.2.3 From 82ba3a79bf19d1d6d0a596914bbbcfaefa251874 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:21:08 -0700 Subject: Move this into a section so that "Resetting the database" will show up in TOC --- railties/guides/source/migrations.textile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 5e4d948b24..8df721782c 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -586,7 +586,9 @@ Neither of these Rake tasks do anything you could not do with +db:migrate+. They are simply more convenient, since you do not need to explicitly specify the version to migrate to. -Lastly, the +db:reset+ task will drop the database, recreate it and load the +h4. Resetting the database + +The +rake db:reset+ task will drop the database, recreate it and load the current schema into it. NOTE: This is not the same as running all the migrations - see the section on -- cgit v1.2.3 From 90f18cc85ec33332ee9caab2b5172b38f91432b1 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:21:43 -0700 Subject: Readability improvements --- railties/guides/source/migrations.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 8df721782c..ab7b8b5ae2 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -594,7 +594,7 @@ current schema into it. NOTE: This is not the same as running all the migrations - see the section on "schema.rb":#schema-dumping-and-you. -h4. Being Specific +h4. Running specific migrations If you need to run a specific migration up or down, the +db:migrate:up+ and +db:migrate:down+ tasks will do that. Just specify the appropriate version and -- cgit v1.2.3 From d1ef1b2220947d772c9b2b8e8e4bc735fc6bdb71 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:22:12 -0700 Subject: Improve readability --- railties/guides/source/migrations.textile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index ab7b8b5ae2..a1a4afc067 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -605,8 +605,8 @@ example, $ rake db:migrate:up VERSION=20080906120000 -will run the +up+ method from the 20080906120000 migration. These tasks check -whether the migration has already run, so for example +db:migrate:up +will run the +up+ method from the 20080906120000 migration. These tasks still +check whether the migration has already run, so for example +db:migrate:up VERSION=20080906120000+ will do nothing if Active Record believes that 20080906120000 has already been run. -- cgit v1.2.3 From e9b3b9e5d82d69cf15bd9ca2d4ecad348a76d2e4 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:22:31 -0700 Subject: This section is more about changing the output of running migrations --- railties/guides/source/migrations.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index a1a4afc067..0eebe742c9 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -610,7 +610,7 @@ check whether the migration has already run, so for example +db:migrate:up VERSION=20080906120000+ will do nothing if Active Record believes that 20080906120000 has already been run. -h4. Being Talkative +h4. Changing the output of running migrations By default migrations tell you exactly what they're doing and how long it took. A migration creating a table and adding an index might produce output like this -- cgit v1.2.3 From 6589359ed1bdb2906a09eed84f4c6271baa9bbad Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:22:55 -0700 Subject: A table format makes this section easier to read --- railties/guides/source/migrations.textile | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 0eebe742c9..4cb95d9c4d 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -622,13 +622,17 @@ A migration creating a table and adding an index might produce output like this == CreateProducts: migrated (0.0028s) ======================================== -Several methods are provided that allow you to control all this: - -* +suppress_messages+ takes a block as an argument and suppresses any output -* generated by the block. +say+ takes a message argument and outputs it as is. -* A second boolean argument can be passed to specify whether to indent or not. -* +say_with_time+ outputs text along with how long it took to run its block. If -* the block returns an integer it assumes it is the number of rows affected. +Several methods are provided in migrations that allow you to control all this: + +|_.Method |_.Purpose| +|suppress_messages |takes a block as an argument and suppresses any output + generated by the block.| +|say |Takes a message argument and outputs it as is. A second + boolean argument can be passed to specify whether to + indent or not.| +|say_with_time |Outputs text along with how long it took to run its + block. If the block returns an integer it assumes it + is the number of rows affected.| For example, this migration -- cgit v1.2.3 From 452b624e1e878c2c38ea699c9bd413c00b00bad3 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:23:20 -0700 Subject: Be more friendly than "shut up" --- railties/guides/source/migrations.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 4cb95d9c4d..feb02892cb 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -669,7 +669,7 @@ generates the following output == CreateProducts: migrated (10.0054s) ======================================= -If you just want Active Record to shut up, then running +rake db:migrate +If you want Active Record to not output anything, then running +rake db:migrate VERBOSE=false+ will suppress all output. h3. Using Models in Your Migrations -- cgit v1.2.3 From 914b85f12492adad35108405d498da314584b9bf Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:24:28 -0700 Subject: Improve readability --- railties/guides/source/migrations.textile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index feb02892cb..bb5b769f79 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -819,10 +819,11 @@ loaded into the test database. Schema files are also useful if you want a quick look at what attributes an Active Record object has. This information is not in the model's code and is -frequently spread across several migrations, but is summed up in the schema -file. The "annotate_models":https://github.com/ctran/annotate_models gem -automatically adds and updates comments at the top of each model summarizing the -schema if you desire that functionality. +frequently spread across several migrations, but the information is nicely +summed up in the schema file. The +"annotate_models":https://github.com/ctran/annotate_models gem automatically +adds and updates comments at the top of each model summarizing the schema if +you desire that functionality. h4. Types of Schema Dumps -- cgit v1.2.3 From 718d0ea985e0c552fa9d2312c53e308cbfea440b Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:24:43 -0700 Subject: Be explicit of where the constraints are. --- railties/guides/source/migrations.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index bb5b769f79..2290b26162 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -891,7 +891,7 @@ which models can enforce data integrity. The +:dependent+ option on associations allows models to automatically destroy child objects when the parent is destroyed. Like anything which operates at the application level, these cannot guarantee referential integrity and so some people augment them with foreign key -constraints. +constraints in the database. Although Active Record does not provide any tools for working directly with such features, the +execute+ method can be used to execute arbitrary SQL. You could -- cgit v1.2.3 From b6d7ce9f2376138f17e41f293add29aab334a358 Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:30:43 -0700 Subject: Fix missing /ruby block --- railties/guides/source/migrations.textile | 1 + 1 file changed, 1 insertion(+) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 2290b26162..7dd9e718f8 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -305,6 +305,7 @@ class RemovePartNumberFromProducts < ActiveRecord::Migration add_column :products, :part_number, :string end end + You are not limited to one magically generated column, for example -- cgit v1.2.3 From 95213ac47721efb0d4ceec4fb919c8208389db6e Mon Sep 17 00:00:00 2001 From: Jason Noble Date: Sat, 3 Dec 2011 19:31:04 -0700 Subject: Be explicit about where to add/remove stuff to the migration --- railties/guides/source/migrations.textile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index 7dd9e718f8..f458f1bff9 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -325,7 +325,8 @@ end As always, what has been generated for you is just a starting point. You can add -or remove from it as you see fit. +or remove from it as you see fit by editing the +db/migrate/YYMMDDHHMMSS_add_details_to_products.rb file. NOTE: The generated migration file for destructive migrations will still be old-style using the +up+ and +down+ methods. This is because Rails needs to know -- cgit v1.2.3 From 69341d3686c3ae3f29c8a8b74ec027d29e101e79 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Sun, 4 Dec 2011 16:29:11 +0530 Subject: copy edits in the migrations guide --- railties/guides/source/migrations.textile | 94 +++++++++++++++---------------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/railties/guides/source/migrations.textile b/railties/guides/source/migrations.textile index f458f1bff9..e67be0ae9f 100644 --- a/railties/guides/source/migrations.textile +++ b/railties/guides/source/migrations.textile @@ -20,9 +20,9 @@ SQLite3 in development, but MySQL in production. In this guide, you'll learn all about migrations including: -* The generators you can use to create them -* The methods Active Record provides to manipulate your database -* The Rake tasks that manipulate them +* The generators you can use to create them +* The methods Active Record provides to manipulate your database +* The Rake tasks that manipulate them * How they relate to +schema.rb+ endprologue. @@ -35,7 +35,7 @@ sorts of things you can do: class CreateProducts < ActiveRecord::Migration def up - create_table :products do |t| + create_table :products do |t| t.string :name t.text :description @@ -62,7 +62,7 @@ bad data in the database or populate new fields: class AddReceiveNewsletterToUsers < ActiveRecord::Migration def up - change_table :users do |t| + change_table :users do |t| t.boolean :receive_newsletter, :default => false end User.update_all ["receive_newsletter = ?", true] @@ -90,7 +90,7 @@ the migration is rolled back without the need to write a separate +down+ method. class CreateProducts < ActiveRecord::Migration def change - create_table :products do |t| + create_table :products do |t| t.string :name t.text :description @@ -113,7 +113,7 @@ database independent way (you'll read about them in detail later): * +add_index+ * +change_column+ * +change_table+ -* +create_table+ +* +create_table+ * +drop_table+ * +remove_column+ * +remove_index+ @@ -134,11 +134,11 @@ the changes that were made by hand. h4. What's in a Name -Migrations are stored as files in the +db/migrate+ directory, one for each -migration class. The name of the file is of the form -+YYYYMMDDHHMMSS_create_products.rb+, that is to say a UTC timestamp -identifying the migration followed by an underscore followed by the name -of the migration. The name of the migration class (CamelCased version) +Migrations are stored as files in the +db/migrate+ directory, one for each +migration class. The name of the file is of the form ++YYYYMMDDHHMMSS_create_products.rb+, that is to say a UTC timestamp +identifying the migration followed by an underscore followed by the name +of the migration. The name of the migration class (CamelCased version) should match the latter part of the file name. For example +20080906120000_create_products.rb+ should define class +CreateProducts+ and +20080906120001_add_details_to_products.rb+ should define @@ -150,7 +150,7 @@ Internally Rails only uses the migration's number (the timestamp) to identify them. Prior to Rails 2.1 the migration number started at 1 and was incremented each time a migration was generated. With multiple developers it was easy for these to clash requiring you to rollback migrations and renumber them. With -Rails 2.1+ this is largely avoided by using the creation time of the migration +Rails 2.1+ this is largely avoided by using the creation time of the migration to identify them. You can revert to the old numbering scheme by adding the following line to +config/application.rb+. @@ -163,9 +163,8 @@ allows Rails to handle common situations that occur with multiple developers. For example Alice adds migrations +20080906120000+ and +20080906123000+ and Bob adds +20080906124500+ and runs it. Alice finishes her changes and checks in her -migrations and Bob pulls down the latest changes. When Bob runs +rake -db:migrate+, Rails knows that it has not run Alice's two migrations so it -executes the +up+ method for each migration. +migrations and Bob pulls down the latest changes. When Bob runs +rake db:migrate+, +Rails knows that it has not run Alice's two migrations so it executes the +up+ method for each migration. Of course this is no substitution for communication within the team. For example, if Alice's migration removed a table that Bob's migration assumed to @@ -177,8 +176,7 @@ Occasionally you will make a mistake when writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run +rake db:migrate+. You must rollback the migration (for -example with +rake db:rollback+), edit your migration and then run +rake -db:migrate+ to run the corrected version. +example with +rake db:rollback+), edit your migration and then run +rake db:migrate+ to run the corrected version. In general editing existing migrations is not a good idea: you will be creating extra work for yourself and your co-workers and cause major headaches if the @@ -246,9 +244,9 @@ class CreateProducts < ActiveRecord::Migration end -You can append as many column name/type pairs as you want. By default, the -generated migration will include +t.timestamps+ (which creates the -+updated_at+ and +created_at+ columns that are automatically populated +You can append as many column name/type pairs as you want. By default, the +generated migration will include +t.timestamps+ (which creates the ++updated_at+ and +created_at+ columns that are automatically populated by Active Record). h4. Creating a Standalone Migration @@ -256,7 +254,7 @@ h4. Creating a Standalone Migration If you are creating migrations for other purposes (for example to add a column to an existing table) then you can also use the migration generator: - + $ rails generate migration AddPartNumberToProducts @@ -325,8 +323,8 @@ end As always, what has been generated for you is just a starting point. You can add -or remove from it as you see fit by editing the -db/migrate/YYMMDDHHMMSS_add_details_to_products.rb file. +or remove from it as you see fit by editing the +db/migrate/YYYYMMDDHHMMSS_add_details_to_products.rb file. NOTE: The generated migration file for destructive migrations will still be old-style using the +up+ and +down+ methods. This is because Rails needs to know @@ -374,7 +372,7 @@ By default, +create_table+ will create a primary key called +id+. You can change the name of the primary key with the +:primary_key+ option (don't forget to update the corresponding model) or, if you don't want a primary key at all (for example for a HABTM join table), you can pass the option +:id => false+. If you -need to pass database specific options you can place a SQL fragment in the +need to pass database specific options you can place an SQL fragment in the +:options+ option. For example, @@ -401,8 +399,8 @@ change_table :products do |t| end -removes the +description+ and +name+ columns, creates a +part_number+ string -column and adds an index on it. Finally it renames the +upccode+ column. +removes the +description+ and +name+ columns, creates a +part_number+ string +column and adds an index on it. Finally it renames the +upccode+ column. h4. Special Helpers @@ -427,7 +425,7 @@ end adds those columns to an existing table. Another helper is called +references+ (also available as +belongs_to+). In its -simplest form it just adds some readability +simplest form it just adds some readability. create_table :products do |t| @@ -446,7 +444,7 @@ create_table :products do |t| end -will add an +attachment_id+ column and a string +attachment_type+ column with +will add an +attachment_id+ column and a string +attachment_type+ column with a default value of 'Photo'. NOTE: The +references+ helper does not actually create foreign key constraints @@ -481,8 +479,8 @@ the +change+ method supports only these migration definitions: * +rename_index+ * +rename_table+ -If you're going to need to use any other methods, you'll have to write the -+up+ and +down+ methods instead of using the change method. +If you're going to need to use any other methods, you'll have to write the ++up+ and +down+ methods instead of using the +change+ method. h4. Using the +up+/+down+ Methods @@ -531,9 +529,9 @@ can't be done. h3. Running Migrations Rails provides a set of rake tasks to work with migrations which boil down to -running certain sets of migrations. +running certain sets of migrations. -The very first migration related rake task you will use will probably be +The very first migration related rake task you will use will probably be +rake db:migrate+. In its most basic form it just runs the +up+ or +change+ method for all the migrations that have not yet been run. If there are no such migrations, it exits. It will run these migrations in order based @@ -543,8 +541,8 @@ Note that running the +db:migrate+ also invokes the +db:schema:dump+ task, which will update your db/schema.rb file to match the structure of your database. If you specify a target version, Active Record will run the required migrations -(up or down or change) until it has reached the specified version. The version -is the numerical prefix on the migration's filename. For example, to migrate +(up or down or change) until it has reached the specified version. The version +is the numerical prefix on the migration's filename. For example, to migrate to version 20080906120000 run @@ -553,8 +551,8 @@ $ rake db:migrate VERSION=20080906120000 If version 20080906120000 is greater than the current version (i.e., it is migrating upwards), this will run the +up+ method on all migrations up to and -including 20080906120000, and will not execute any later migrations. If -migrating downwards, this will run the +down+ method on all the migrations +including 20080906120000, and will not execute any later migrations. If +migrating downwards, this will run the +down+ method on all the migrations down to, but not including, 20080906120000. h4. Rolling Back @@ -627,13 +625,13 @@ A migration creating a table and adding an index might produce output like this Several methods are provided in migrations that allow you to control all this: |_.Method |_.Purpose| -|suppress_messages |takes a block as an argument and suppresses any output +|suppress_messages |Takes a block as an argument and suppresses any output generated by the block.| |say |Takes a message argument and outputs it as is. A second boolean argument can be passed to specify whether to indent or not.| |say_with_time |Outputs text along with how long it took to run its - block. If the block returns an integer it assumes it + block. If the block returns an integer it assumes it is the number of rows affected.| For example, this migration @@ -771,7 +769,7 @@ If Alice had done this instead, there would have been no problem: # db/migrate/20100513121110_add_flag_to_product.rb -class AddFlagToProduct < ActiveRecord::Migration +class AddFlagToProduct < ActiveRecord::Migration class Product < ActiveRecord::Base end @@ -791,7 +789,7 @@ end class AddFuzzToProduct < ActiveRecord::Migration class Product < ActiveRecord::Base end - + def change add_column :products, :fuzz, :string Product.reset_column_information @@ -807,7 +805,7 @@ h3. Schema Dumping and You h4. What are Schema Files for? Migrations, mighty as they may be, are not the authoritative source for your -database schema. That role falls to either +db/schema.rb+ or a SQL file which +database schema. That role falls to either +db/schema.rb+ or an SQL file which Active Record generates by examining the database. They are not designed to be edited, they just represent the current state of the database. @@ -821,10 +819,10 @@ loaded into the test database. Schema files are also useful if you want a quick look at what attributes an Active Record object has. This information is not in the model's code and is -frequently spread across several migrations, but the information is nicely -summed up in the schema file. The -"annotate_models":https://github.com/ctran/annotate_models gem automatically -adds and updates comments at the top of each model summarizing the schema if +frequently spread across several migrations, but the information is nicely +summed up in the schema file. The +"annotate_models":https://github.com/ctran/annotate_models gem automatically +adds and updates comments at the top of each model summarizing the schema if you desire that functionality. h4. Types of Schema Dumps @@ -867,8 +865,8 @@ reconstitute those statements from the database. If you are using features like this, then you should set the schema format to +:sql+. Instead of using Active Record's schema dumper, the database's structure will be -dumped using a tool specific to the database (via the +db:structure:dump+ Rake -task) into +db/structure.sql+. For example, for the PostgreSQL RDBMS, the +dumped using a tool specific to the database (via the +db:structure:dump+ Rake task) +into +db/structure.sql+. For example, for the PostgreSQL RDBMS, the +pg_dump+ utility is used. For MySQL, this file will contain the output of +SHOW CREATE TABLE+ for the various tables. Loading these schemas is simply a question of executing the SQL statements they contain. By definition, this will create a -- cgit v1.2.3