Posted By

chrisaiv on 07/19/10


Tagged

rails


Versions (?)

Rails: Common Migration, Table, Column Tasks


 / Published in: Rails
 

These methods are used for the easy creation and maintenance of database tables. Here\'s a good cheat sheet. http://dizzy.co.uk/rubyonrails/cheatsheets/rails-migrations

  1. ###############################
  2. #Common Migration Tasks
  3. ###############################
  4. #Generate a new migration template
  5. ruby script/generate migration AddNewColumnNameToModel column_name:[integer, string, boolean, text]
  6.  
  7. #Create a new migration for adding the "sessions" table
  8. 1. rake db:session:create
  9. 2. Uncomment #ActionController::Base.session_store = :active_record_store form /my_rails_app/config/initializers/session_store.rb
  10.  
  11. #Migrate to Version 0
  12. rake db:migrate VERSION=0
  13.  
  14. #Applies any pending migrations to the database
  15. db:migrate
  16.  
  17. #Rolls back one database migration and then reapplies it
  18. db:migrate:redo
  19.  
  20. #Drops the database and reapplies all migrations
  21. db:migrate:reset
  22.  
  23. #Applies the next pending migration
  24. db:up
  25.  
  26. #Rolls back the last applied migration
  27. db:down
  28.  
  29. #Creates the database from the environment specified
  30. db:create:all
  31. db:create
  32.  
  33. #Removes the database from the environment specified
  34. db:drop:all (Rails +2.0)
  35. db:drop
  36.  
  37. #Drops and re-creates the database
  38. db:reset
  39.  
  40. ###############################
  41. #Table modification methods
  42. ###############################
  43.  
  44. #Create a new table.
  45. create_table(name, options)
  46.  
  47. #Drop the specified table.
  48. drop_table(name)
  49.  
  50. #Rename the table from old_name to new_name.
  51. rename_table(old_name, new_name)
  52.  
  53. ###############################
  54. #Column modification methods
  55. ###############################
  56. #Add a new column to table.
  57. add_column(table, column, type, options)
  58.  
  59. #Rename the specified column from old_column_name to new_column_name.
  60. rename_column(table, old_column_name, new_column_name)
  61.  
  62. #Modify a column by changing it to a different type.
  63. change_column(table, column, type, options)
  64.  
  65. #Remove the column from the specified table. Index modification methods
  66. remove_column(table, column)
  67.  
  68. #Add a new index on the specified columns.
  69. add_index(table, columns, options)
  70.  
  71. #Drop the specified index.
  72. remove_index(table, index)

Report this snippet  

You need to login to post a comment.