Rails: Migration (page 1)

Rails Migrations
are a set of db instructions
written in Ruby
Allows you to migrate your db from one state to another
Contains both move up (to new state) and down (to a prev state)

Why use it?
Migrations keep the schema with the application code

The Migration keeps all the db schema changes… and allows sharing of schema changes.

Different users or different computers can easily set up the db to the same state

Inside the migration file it will create the table setups.  A string (i.e. t.string “first_name”) will be seen as a varchar and create a column with this name.

create_table format
t.[type]
example: t.string “Name”, options
Different types are: binary, boolean, date, datetime, decimal, float, integer, string, text, time.

Special Columns
created_at and updated_at are special columns in rails, you just call it out in the migration (i.e. t.imestamps) and these two columns are automatically created.

Running a migration in Rails
rake db:migrate
This runs all the migrations you have available… The output of which, looks something like this:

c:\Sites\simple_cms2>rake db:migrate
==  CreateSubjects: migrating =================================================
— create_table(:subjects)
   -> 0.0780s
==  CreateSubjects: migrated (0.0800s) ========================================

==  CreatePages: migrating ====================================================
— create_table(:pages)
   -> 0.0170s
— add_index(“pages”, “subject_id”)
   -> 0.0210s
— add_index(“pages”, “permalink”)
   -> 0.0770s
==  CreatePages: migrated (0.1200s) ===========================================

==  CreateSections: migrating =================================================
— create_table(:sections)
   -> 0.0230s
— add_index(“sections”, “page_id”)
   -> 0.0200s
==  CreateSections: migrated (0.0450s) ========================================

==  CreateUsers: migrating ====================================================
— create_table(:users)
   -> 0.0050s
==  CreateUsers: migrated (0.0050s) ===========================================

==  DoNothingYet: migrating ===================================================
==  DoNothingYet: migrated (0.0000s) ==========================================

After the migration, we can log into the database and check things out…
if we run the SQL:

show tables

we’ll see our tables…
If we check the table we did some migration specifications with by running the SQL query:
mysql> show fields from users;

We’ll get some output like this (which shows all the columns we set up in the Migration):
+————+————–+——+—–+———+—————-+
| Field      | Type         | Null | Key | Default | Extra          |
+————+————–+——+—–+———+—————-+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| first_name | varchar(25)  | YES  |     | NULL    |                |
| last_name  | varchar(50)  | YES  |     | NULL    |                |
| email      | varchar(255) | NO   |     |         |                |
| password   | varchar(40)  | YES  |     | NULL    |                |
| created_at | datetime     | NO   |     | NULL    |                |
| updated_at | datetime     | NO   |     | NULL    |                |
+————+————–+——+—–+———+—————-+

Leave a Reply

Your email address will not be published. Required fields are marked *