Migration --- creating database tables
READ ruby guide on migrations here
migrations = way in RoR to alter database schema over time
Step 1: Create a Model class using generate tool in Rails (e.g. User Model class)
i.e. in RubyMine: Tools-> Run Rails Generator -> model
>>> give it the name User (in this example)
it will generate a file under app/models/model_name.rb AND also a db/migrate/****_model_name.rb file
Here is example when name of model is User
Step 2: edit the generated database migration file autogenerated for you in step 1 to add code to create table
SUPPOSE you have created a model called User it will create the following file for you. Edit it as shown in red to add code to create the corresponding database table.
db/migrate/20090215220309_create_users.rb:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.column :first_name, :string
t.column :last_name, :string
end
end
end
HERE IS A DIFFERENT example for a Model Student
db/migrate/20090215220309_create_students.rb:
class CreateStudents < ActiveRecord::Migration
def change
create_table :students do |t|
t.column :name, :string
t.column :birth, :date
t.column :gpa, :float
t.column :grad, :integer
end
end
end
OPTIONAL: Step 3: create script file to load data already know
OPTIONAL!!!!! Here there is a file load_data.rb that is an example that contains some initial data you want to load
COMMAND LINE: rails generate migration load_data
RubyMine IDE: Tools-> Run Rails Generator -> migration (then give the name load_data)
Example of a load_data.rb fileclass LoadData < ActiveRecord::Migration
|
---|
step 4: Run migrations (the latest one)
COMMAND LINE: rake db:migrate
RubyMine IDE: Tools->Run Rake Task -> db:migrate
Following our example of ---this generates the following database table named:
users
id | first_name | last_name |
---|---|---|
primary key | string | string |
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)
If we had done the optional load_data file seen above this would be the contents of the users database
1 | Justin | Bieber |
---|---|---|
2 | Paris | Hilton |
3 | Miley | Cyrus |
4 | Barack | Obama |
5 | Santa | Claus |
6 | John | Ousterhout |
SPECIAL WORD About the schema that is ALSO GENERATED when you perform migrations
From RoR guide:What are Schema Files for? (YOU DONT EDIT THEM!!!!!)
|
---|
Example schema.rb file created from aboveActiveRecord::Schema.define(version: 20150105235326) do
|
SPECIAL WORD About Changing Migration file and rerunning---create a new one instead
"You should always create a new migration file when adding/changing something in the database. This is the purpose of migrations."
From RoR guide:
|
---|
NOT a suggestion but on the command line there is a db:migrate:redo and rake db:schema:dump
|
Quickly generate new migrations file to add say a new column in a tablerails generate migration add_columnname_to_tablename example rails generate migration add_gpa_to_students |
ALTERNATIVE STEP 2 ---also add the timestamps macro
edit the generated database migration file autogenerated for you in step 1 to add code to create table but ADD the timestamp
The timestamps macro adds two columns, created_at and updated_at.
|
---|
SUPPOSE you have created a model called Student it will create the following file for you. Edit it as shown in red to add code to create the corresponding database table.
db/migrate/20090215220309_create_students.rb:
class CreateStudents < ActiveRecord::Migration
def change
create_table :students do |t|
t.column :name, :string
t.column :birth, :date
t.column :gpa, :float
t.column :grad, :integer
t.timestamps null: false
end
end
end
This generates the following database table named:
students
id | name | birth | gpa | grad | created_at | updated_at |
---|---|---|---|---|---|---|
primary key | string | date | float | integer | datetime (e.g. 2015-01-06 00:52:29.375922) |
datetime |