Home
Ruby on Rails
Cheatsheets
Provides a block that enables you to alter columns on an existing table using various shortcut methods...
change_table :table_name, {options} do |t| t.change :column_name, :new_column_type t.remove :column_name end
Creates a table on the database. Creates a table called :table_name and makes the table object available to a block that can then add columns to it by specifying column_types or utilising shortcut methods such as using belongs_to to specify foreign keys...
create_table :table_name, {table_options} do |t| t.string :name, {column_options} end
{table_options}
|
|
|
if |
|
|
|
if |
|
|
|
if |
|
|
|
overrides the default name of |
|
|
|
pass raw options to your underlying database, e.g. |
Destroys the specified table.
drop_table :table_name
Renames the specified table.
rename_table :old_table_name, :new_table_name
|
Rails |
db2 |
mysql |
openbase |
Oracle |
|
|
blob(32678) |
blob |
object |
blob |
|
|
decimal(1) |
tinyint(1) |
boolean |
number(10) |
|
|
date |
date |
date |
date |
|
|
timestamp |
datetime |
datetime |
date |
|
|
decimal |
decimal |
decimal |
decimal |
|
|
float |
float |
float |
number |
|
|
int |
int(11) |
integer |
number(38) |
|
|
varchar(255) |
varchar(255) |
char(4096) |
varchar2(255) |
|
|
clob(32768) |
text |
text |
clob |
|
|
time |
time |
time |
date |
|
|
timestamp |
datetime |
timestamp |
date |
|
Rails |
postgresql |
sqlite |
sqlserver |
Sybase |
|
|
bytea |
blob |
image |
image |
|
|
boolean |
boolean |
bit |
bit |
|
|
date |
date |
datetime |
datetime |
|
|
timestamp |
datetime |
datetime |
datetime |
|
|
decimal |
decimal |
decimal |
decimal |
|
|
float |
float |
float(8) |
float(8) |
|
|
integer |
integer |
int |
int |
|
|
* |
varchar(255) |
varchar(255) |
varchar(255) |
|
|
text |
text |
text |
text |
|
|
time |
datetime |
datetime |
time |
|
|
timestamp |
datetime |
datetime |
timestamp |
class CreateCustomers < ActiveRecord::Migration def self.up create_table :customers, :primary_key => :customer_id, :options => "auto_increment = 10000" do |t| t.integer :customer_id t.string :name, :limit => 30, :null => false t.integer :age t.boolean :premium, :default => 0 t.binary :photo, :limit => 2.megabytes t.timestamps t.text :notes, :default => "No notes recorded" end add_column :customers, :surname, :string, :limit => 50 add_column :orders, :price, :decimal, :precision => 8, :scale => 2 Customer.create :name => "David", :surname => "Smith", :age => "32", :premium => "1", :notes => "One of our top customers!" end def self.down drop_table :customers end end
|
db:create db:create:all |
Creates a single database specified in config/databases.yml for the current RAILS_ENV or creates all the databases |
|
db:drop db:drop:all |
Drops a single database specified in config/databases.yml for the current RAILS_ENV or drops all the databases |
|
db:fixtures:load |
Load fixtures from test/fixtures into the current environment's database |
|
db:migrate |
Run all unapplied migrations |
|
db:migrate:up db:migrate:down |
Move forward to the next migration, or back to the previous migration |
|
db:migrate VERSION=18 |
Migrate database to specific version |
|
db:migrate RAILS_ENV=production |
Use migrations to recreate tables in the testing or production databases |
|
db:schema:dump |
Create a db/schema.rb file that can be portably used against any database supported by ActiveRecord |
|
db:schema:load |
Load a schema.rb file into the database |
|
db:sessions:create |
Create a sessions table for use with CGI::Sessions::ActiveRecordStore |
|
db:sessions:clear |
Clear the sessions table |
|
db:structure:dump |
Dump database structure to SQL file |
|
db:reset |
Drops the database, creates the database and then runs migrations against the database. Takes a VERSION argument as well as RAILS_ENV |
|
db:rollback STEP=4 |
Takes a STEP argument to determine how many version to rollback, the default being one version |
|
db:test:prepare |
Clone your database structure into the test database |
|
db:version |
Tells you the current version your database is at |
rails_root
db
schema.rbmigrate
20080812205401_create_customers.rb20080812215424_add_photo.rb20080812235405_alter_surname.rb|
t.column |
t.change |
t.rename |
|
t.remove |
t.change_default |
t.references |
|
t.remove_references |
t.belongs_to |
t.remove_belongs_to |
|
t.timestamps |
t.index |
t.remove_index |
Creates a new column on the specified table.
add_column :table_name, :column_name, :column_type, {column_options}
{column_options}
|
|
|
if |
|
|
|
set a limit on the size of the field |
|
|
|
set a default value for the column |
|
|
|
Specifies the precision for a |
|
|
|
Specifies the scale for a |
Change the data type of the specified column
change_column :table_name, :column_name, :new_column_type
Renames the specified column.
rename_column :table_name, :old_column_name, :new_column_name
Removes the specified column.
remove_column :table_name, :column_name
Creates an index for the specified column.
add_index :table_name, :column_name, :unique => true
Remove an index from the specified column.
remove_index :table_name, :column_name
Takes a single string identifying a valid SQL command to execute directly.
execute "alter table line_items add constraint fk_line_item_products foreign key (product_id) references products(id)"
Use in the down method of a migration file to raise an exception when the up methods of the same migration file can not be reversed, e.g. changing a column type from :integer to :string.
raise ActiveRecord::IrreversibleMigration
script/generate migration new_migration_filename
field_name:column_type name:string age:integer date_of_birth:dateFixtures contain data which can be loaded into your database using migrations. For example, to load data into a table named customers...
ruby script/generate migration load_customers_datacustomers.yml file into your customers table|
customers.yml |
melissa:
name: Melissa
age: 18
david:
name: David
age: 23
|
|
migration.rb |
require 'active_record/fixtures' class LoadCustomerData def self.up down directory = File.join(File.dirname(__FILE__), "data") Fixtures.create_fixtures(directory, "customers") end def self.down Customer.delete_all end end |
A look at the new features and syntax available in migrations on Rails 2.0
More
A brilliant plugin that makes changing your database schema even easier and faster.
More
7 MONTHS AGO
Tomomi
Very Beneficial
Very Beneficial resource code.
6 MONTHS AGO
tom
migrate
beneficial
6 MONTHS AGO
Joe Lewis
Very nice - one small bug
Thank you for the organized approach to looking at migrations - much appreciated. One small item - I noticed in the PDF version, the rename_column item uses rename_table in the code example (rename_table :old_column_name, :new_column_name).
4 MONTHS AGO
Cwelle
Very nice, buy one type is missing
Very nice, buy one type is missing. It's :text. Representing TEXT in mysql.
4 MONTHS AGO
Dougle
Drops?
What about drops for migrating down
3 MONTHS AGO
txarli
Cortesy
what ahout a little bit of courtesy? So, when you find an excelent guide to migrations, the only thing to say is "What about drops for migrating down" pff...
3 MONTHS AGO
Dougle
Drops?
"So, when you find an excelent guide to migrations, the only thing to say is "What about drops for migrating down" pff..." Exactly what i mean.. it would be an "excellent" guide if it had drop syntax, that's not discourteous at all.
3 MONTHS AGO
Gideon
rename_table args reversed
Thanks for this very useful resource. The args for rename_table should be :old_table, :new_table (and rename_table is actually listed twice too).
2 MONTHS AGO
Ryan Shillington
rename_column is wrong too
You're missing the table argument from the rename_column syntax as well.
2 MONTHS AGO
mosquete
Minor mistake
In "Fixtures" section, item 4, "migration.rb" was swapped with "customers.yml".
ABOUT 2 HOURS AGO
shikha
Limit is not working with integer or bigint
i have tried so many times but :limit is not working. code: add_column(:table, :field, :bigint, {:limit => 13})