2010年11月16日 星期二

migrate syntax

1.create new migrate name AlterUsers
#rails generate migrate AlterUsers

2.modify db/migrate/20101117011002_alter_users.rb
modify file like this

class AlterUsers < ActiveRecord::Migration
def self.up
rename_table("users","admin_users")
add_column("admin_users","username",:string,:limit=>25)
change_column("admin_users","email",:string,:limit=>100)
rename_column("admin_users","password","hashed_password")
add_column("admin_users","salt",:string,:limit=>40)
puts "*** about change table"
add_index("admin_users","username")
end

def self.down
remove_index("admin_users","username")
remove_column("admin_users","salt")
rename_column("admin_users","hashed_password","password")
change_column("admin_users","email",:string,:default=>"",:null=>false)
remove_column("admin_users","username")
rename_table("admin_users","users")
end
end

3.use rake to up the databases structure
#rake db:migrate:up VERSION=20101117011002_alter_users.rb

4.use mysql command to see the changes
#mysql -u root -p
#use simple_cms_development;
#show columns from admin_users;

5.use rake to rollback
#rake db:migrate:down VERSION=20101117011002_alter_users.rb

沒有留言:

張貼留言