Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* mig_guide: Initial draft
- Loading branch information
Showing
7 changed files
with
506 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
== Anatomy Of A Migration == | ||
|
||
Before we dive into the details of a migration, here is a typical example: | ||
|
||
[source, ruby] | ||
------------------------ | ||
class CreateProducts < ActiveRecord::Migration | ||
def self.up | ||
create_table :products do |t| | ||
t.string :name | ||
t.text :description | ||
|
||
t.timestamps | ||
end | ||
end | ||
|
||
def self.down | ||
drop_table :products | ||
end | ||
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 ActiveRecord populates automatically will also be added. Reversing this migration is as simple as dropping the table. | ||
|
||
=== Migrations are classes | ||
A migration is a subclass of `ActiveRecord::Migration` that implements two class methods: +up+ (perform the required transformations) and +down+ (revert them). | ||
|
||
Methods are provided that perform common data definition tasks in a database independent way (we'll look at them in detail later): | ||
|
||
* `create_table` | ||
* `change_table` | ||
* `drop_table` | ||
* `add_column` | ||
* `remove_column` | ||
* `change_column` | ||
* `rename_column` | ||
* `add_index` | ||
* `remove_index` | ||
|
||
If you need to perform tasks specific to your database (for example create a foreign key constraint) then the `execute` function 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, migrations are wrapped in a transaction. If the database does not support this (for example Mysql and SQLite) 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. | ||
|
||
=== 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 migration class' name must match (the camelcased version of) the latter part of the file name. For example `20080906120000_create_products.rb` should define `CreateProducts` and `20080906120001_add_details_to_products.rb` should define `AddDetailsToProducts`. If you do feel the need to change the file name then you MUST 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. | ||
|
||
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. | ||
|
||
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 the existence of then trouble will still occur. | ||
|
||
=== Changing migrations === | ||
|
||
Occasionally you will make a mistake while 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. Just use some common sense. | ||
|
109 changes: 109 additions & 0 deletions
109
railties/doc/guides/migrations/creating_a_migration.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
== Creating A Migration == | ||
|
||
=== 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 those will also be created. For example, running | ||
|
||
`ruby script/generate model Product name:string description:text` will create a migration that looks like this | ||
|
||
[source, ruby] | ||
----------------------- | ||
class CreateProducts < ActiveRecord::Migration | ||
def self.up | ||
create_table :products do |t| | ||
t.string :name | ||
t.text :description | ||
|
||
t.timestamps | ||
end | ||
end | ||
|
||
def self.down | ||
drop_table :products | ||
end | ||
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 ActiveRecord are also created) will be added for you. | ||
|
||
=== 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: | ||
|
||
`ruby script/generate migration AddPartNumberToProducts` | ||
|
||
This will create an empty but appropriately named migration: | ||
|
||
[source, ruby] | ||
----------------------- | ||
class AddPartNumberToProducts < ActiveRecord::Migration | ||
def self.up | ||
end | ||
|
||
def self.down | ||
end | ||
end | ||
----------------------- | ||
|
||
If the migration name is of the form AddXXXToYYY or RemoveXXXFromY and is followed by a list of column names and types then a migration containing | ||
the appropriate add and remove column statements will be created. | ||
|
||
`ruby script/generate migration AddPartNumberToProducts part_number:string` | ||
|
||
will generate | ||
|
||
[source, ruby] | ||
----------------------- | ||
class AddPartNumberToProducts < ActiveRecord::Migration | ||
def self.up | ||
add_column :products, :part_number, :string | ||
end | ||
|
||
def self.down | ||
remove_column :products, :part_number | ||
end | ||
end | ||
----------------------- | ||
|
||
Similarly, | ||
|
||
`ruby script/generate migration RemovePartNumberFromProducts part_number:string` | ||
|
||
generates | ||
|
||
[source, ruby] | ||
----------------------- | ||
class RemovePartNumberFromProducts < ActiveRecord::Migration | ||
def self.up | ||
remove_column :products, :part_number | ||
end | ||
|
||
def self.down | ||
add_column :products, :part_number, :string | ||
end | ||
end | ||
----------------------- | ||
|
||
You aren't limited to one magically generated column, for example | ||
|
||
`ruby script/generate migration AddDetailsToProducts part_number:string price:decimal` | ||
|
||
generates | ||
|
||
[source, ruby] | ||
----------------------- | ||
class AddDetailsToProducts < ActiveRecord::Migration | ||
def self.up | ||
add_column :products, :part_number, :string | ||
add_column :products, :price, :decimal | ||
end | ||
|
||
def self.down | ||
remove_column :products, :price | ||
remove_column :products, :part_number | ||
end | ||
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Migrations | ||
========== | ||
|
||
Migrations are a convenient way for you to alter your database in a structured and organised manner. ActiveRecord takes care of working out which migrations have already been run so you can just update your source and run the migrations, leaving ActiveRecord to work out the details. | ||
|
||
Migrations also allow you to describe these transformation using ruby, in a database independent way (you can drop down to raw SQL for database specific features). | ||
|
||
Rails packages a number of rake tasks and generators for working with migrations and we'll talk about those too. | ||
|
||
include::anatomy_of_a_migration.txt[] | ||
include::creating_a_migration.txt[] | ||
include::writing_a_migration.txt[] | ||
include::rakeing_around.txt[] | ||
include::using_models_in_migrations.txt[] | ||
include::scheming.txt[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
== Running Migrations == | ||
|
||
Rails provides a set of rake tasks to work with migrations which boils down to running certain sets of migrations. The very first migration related rake task you 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. | ||
|
||
If you specify a target version. ActiveRecord 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 this is greater than the current version (i.e. it is migrating upwards) this will run all 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. | ||
|
||
Almost all the functionality provided by other rake tasks could be done using `db:migrate` but would be more tedious (partly because of the long version numbers you would have to lookup and enter). | ||
|
||
=== 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 track 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: | ||
|
||
`rake db:rollback STEP=3` | ||
|
||
will run the down method fron 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 | ||
|
||
`rake db:migrate:redo STEP=3` | ||
|
||
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). | ||
|
||
=== 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 | ||
|
||
`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 ActiveRecord believes that 20080906120000 has already been run. | ||
|
||
|
||
=== 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 | ||
------------------------- | ||
== 20080906170109 CreateProducts: migrating =================================== | ||
-- create_table(:products) | ||
-> 0.0021s | ||
-- add_index(:products, :name) | ||
-> 0.0026s | ||
== 20080906170109 CreateProducts: migrated (0.0059s) ========================== | ||
------------------------- | ||
Several methods are provided that allow you to control all this: | ||
|
||
* `suppress_messages` suppresses any output generated by its block | ||
* `say` outputs text (the second argument controls whether it is indented 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 | ||
|
||
[source, ruby] | ||
---------------------- | ||
class CreateProducts < ActiveRecord::Migration | ||
def self.up | ||
suppress_messages do | ||
create_table :products do |t| | ||
t.string :name | ||
t.text :description | ||
t.timestamps | ||
end | ||
end | ||
say "we created a table" | ||
suppress_messages {add_index :products, :name} | ||
say "and an index!", true | ||
say_with_time 'Waiting for a while' do | ||
sleep 10 | ||
250 | ||
end | ||
end | ||
|
||
def self.down | ||
drop_table :products | ||
end | ||
end | ||
---------------------- | ||
|
||
generates the following output | ||
---------------------- | ||
== 20080906170109 CreateProducts: migrating =================================== | ||
-- we created a table | ||
-> and an index! | ||
-- Waiting for a while | ||
-> 10.0001s | ||
-> 250 rows | ||
== 20080906170109 CreateProducts: migrated (10.0097s) ========================= | ||
---------------------- | ||
|
||
If you just want ActiveRecord to shut up then running `rake db:migrate VERBOSE=false` will suppress any output. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
== Schema dumping and you == | ||
|
||
Migrations, mighty as they may be, are not the final word in what an application's schema is. That role falls to either `schema.rb` or an sql file. | ||
There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire history of that application. It is much simpler 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 `schema.rb` or `development.sql`) and then loaded. | ||
|
||
There are two ways to dump the schema. This is set in `config/environment.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. 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 ActiveRecord 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: `schema.rb` cannot express database specific items such as foreign key constraints. 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 ActiveRecord's schema dumper the database's structure will dumped using a tool specific to that database (via the `db:structure:dump` rake task) into `db/#\{RAILS_ENV\}_structure.sql`. For example for postgresql the `pg_dump` utility is used and for mysql this file will contain the output of SHOW CREATE TABLE for the various tables. Loading this schema is simply a question of executing the sql statements contained inside. | ||
|
||
By definition this will be a perfect copy of the database's structure but this will usually prevent loading the schema into a database other than the one used to create it. |
44 changes: 44 additions & 0 deletions
44
railties/doc/guides/migrations/using_models_in_migrations.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
== 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. Consider for example a migration that uses the Product model to update a row in the corresponding table. Alice later updates the Product model, adding a new column and a validation on it. Bob comes back from holiday, updates the source and runs outstanding migrations with `rake db:migrate`, including the one that used the Product model. When the migration runs the source is up to date and so the Product model has the validation added by Alice. The database however is still old and so does not have that column and an error ensues. | ||
|
||
Frequently one only wishes to update rows in the database without writing out the sql by hand: we're not using anything actually specific to the model. One pattern for this is to define a copy of the model inside the migration itself, for example: | ||
|
||
[source, ruby] | ||
------------------------- | ||
class AddPartNumberToProducts < ActiveRecord::Migration | ||
class Product < ActiveRecord::Base | ||
end | ||
|
||
def self.up | ||
... | ||
end | ||
|
||
def self.down | ||
... | ||
end | ||
end | ||
------------------------- | ||
The migration has its own minimal copy of the `Product` model and no longer cares about the `Product` model defined in the application. | ||
|
||
=== Dealing with changing models === | ||
|
||
For performance reasons information about the columns a model has is cached. For example if you add a column to a table and then try and use the corresponding model to insert a new row it may try and use the old column information. You can force ActiveRecord to re-read the column information with the `reset_column_information` method, for example | ||
|
||
[source, ruby] | ||
------------------------- | ||
class AddPartNumberToProducts < ActiveRecord::Migration | ||
class Product < ActiveRecord::Base | ||
end | ||
|
||
def self.up | ||
add_column :product, :part_number, :string | ||
Product.reset_column_information | ||
... | ||
end | ||
|
||
def self.down | ||
... | ||
end | ||
end | ||
------------------------- |
Oops, something went wrong.