1.use rails generate to do a testing migration
#rails generate migration doNothingYet
it will generate one migration on db/migrate like
20101117002320_do_nothing_yet.rb
2.use rails generate new model user
#rails generate model user
it will generate new migration on db/migrate like
20101117002626_create_users.rb
3.modify the db/migrate/20101117002626_create_users.rb
add field to user table like
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string "first_name", :limit=>25
t.string "last_name", :limit=>50
t.string "email", :default=>"", :null=>false
t.string "password", :limit=>40
t.timestamps
end
end
def self.down
drop_table :users
end
end
3.running a migration
#rake db:migrate
4.use mysql to confirm user table create status
#mysql -u root -p
#user cms_development;
#show tables;
#show columns from users;
5.use ls to show migration version
#ls db/schema.rb
20101117002320_do_nothing_yet.rb 20101117002626_create_users.rb
to up the previous version
#rake db:migrate VERSION=20101117002320
or to specifical version up
#rake db:migrate:up VERSION=20101117002626
#rake db:migrate:down VERSION=20101117002626
#rake db:migrate:redo VERSION=20101117002626
沒有留言:
張貼留言