ROR Lab. Season 3                    - The 3th Round -                 Database                 Migrations                ...
General Migration                 • Use SQL(Database dependent)                 • Run SQL by DB console                 • ...
ROR Migration                 • Use ruby code(Database independent)                 • Run rake task                 • Othe...
Migrations are Classes                                                           rails 3.1 ~>          class CreateProduct...
Migration methods                                                   add_column                                            ...
Up & Down Methods (1)                 class CreateProducts < ActiveRecord::Migration                   def up             ...
Up & Down Methods (2)                 class AddReceiveNewsletterToUsers < ActiveRecord::Migration                   def up...
Up & Down Methods (3)                 class ExampleMigration < ActiveRecord::Migration                   def up           ...
Up & Down Methods (3)                 class ExampleMigration < ActiveRecord::Migration                   def up           ...
Change Method                 class CreateProducts < ActiveRecord::Migration                   def change                 ...
Supported Types                  :binary                :boolean                    :date               :datetime         ...
Migration Files Naming                 • db/migrate directory                 • YYYYMMDDHHMMSS_create_products.rb         ...
To Create                     Migration Files                 • $ rails generate model Post title:string                 •...
Add & Remove                      Columns                 • AddXXXToYYY                 • RemoveXXXFromYYY                ...
To Destroy                     Migration Files                 • $ rails destroy model Post title:string                 •...
Rake it~                 • $ rake db:migrate        “up” state                 • $ rake db:rollback “down” state          ...
Rake it~      $ rake db:migrate:status                                              mysql> select * from schema_migrations...
Rake it~                 $ rake db:version                     Current version: 20120818023340                 $ cat db/sc...
Rake it~                 $ rake db:abort_if_pending_migrations       $ rake db:migrate:status                             ...
schema_migrations                 • db/schema.rb                 • A database table : history of migrations               ...
Running Migrations                                                                      db/schema.rb                      ...
Running Migrations                                                                 database: medibook_development         ...
Running Migrations                                                               database: medibook_development           ...
Running Migrations                                                 database: medibook_development                         ...
output of migrations         • suppress_messages         • say          • indent option true/false         • say_with_time...
class CreateProducts < ActiveRecord::Migration         def change            suppress_messages do                 create_t...
Model in Migration                 • YourTable.reset_column_information                    # db/migrate/20100513121110_add...
App DB Init                                          using db/schema.rb            $ rake db:setup                        ...
Schema works                 • $ rake db:schema:dump                     called by db:migrate                     create s...
감사합니다.
Upcoming SlideShare
Loading in...5
×

Rails Database Migrations - RORLab Season 3-3

287

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
287
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Rails Database Migrations - RORLab Season 3-3

  1. 1. ROR Lab. Season 3 - The 3th Round - Database Migrations March 29th, 2013 ChangHoon Jeong(@seapy) ROR Lab.13년 3월 30일 토요일
  2. 2. General Migration • Use SQL(Database dependent) • Run SQL by DB console • Telling other developer and run SQL their local DB • Run SQL when you deploy to production ROR Lab.13년 3월 30일 토요일
  3. 3. ROR Migration • Use ruby code(Database independent) • Run rake task • Other developer run rake task(it’s rails convention) • Run rake task when you deploy to production ROR Lab.13년 3월 30일 토요일
  4. 4. Migrations are Classes rails 3.1 ~> class CreateProducts < ActiveRecord::Migration class CreateProducts < ActiveRecord::Migration   def up   def change     create_table :products do |t|     create_table :products do |t|       t.string :name       t.string :name       t.text :description       t.text :description           t.timestamps       t.timestamps     end     end   end   end   end   def down     drop_table :products   end end subclass of ActiveRecord::Migration ROR Lab.13년 3월 30일 토요일
  5. 5. Migration methods add_column add_index • Database independent way change_column • Execute method allows you to change_table create_table execute arbitrary SQL drop_table • Migrations are wrapped in a remove_column transaction(PostgreSQL or remove_index SQLite3, not support MySQL) rename_column execute "SQL" ROR Lab.13년 3월 30일 토요일
  6. 6. Up & Down Methods (1) class CreateProducts < ActiveRecord::Migration   def up     create_table :products do |t|       t.string :name       t.text :description         t.timestamps     end   end updated_at, created_at     def down     drop_table :products   end end ROR Lab.13년 3월 30일 토요일
  7. 7. Up & Down Methods (2) class AddReceiveNewsletterToUsers < ActiveRecord::Migration   def up     change_table :users do |t|       t.boolean :receive_newsletter, :default => false     end     User.update_all ["receive_newsletter = ?", true]   end     def down     remove_column :users, :receive_newsletter   end end ROR Lab.13년 3월 30일 토요일
  8. 8. Up & Down Methods (3) class ExampleMigration < ActiveRecord::Migration   def up     create_table :products do |t|       t.references :category     end     #add a foreign key     execute <<-SQL category_id       ALTER TABLE products         ADD CONSTRAINT fk_products_categories         FOREIGN KEY (category_id)         REFERENCES categories(id)     SQL     add_column :users, :home_page_url, :string     rename_column :users, :email, :email_address   end     def down     rename_column :users, :email_address, :email     remove_column :users, :home_page_url     execute <<-SQL       ALTER TABLE products         DROP FOREIGN KEY fk_products_categories     SQL     drop_table :products   end end ROR Lab.13년 3월 30일 토요일
  9. 9. Up & Down Methods (3) class ExampleMigration < ActiveRecord::Migration   def up     create_table :products do |t|       t.references :category     end     #add a foreign key     execute <<-SQL category_id       ALTER TABLE products         ADD CONSTRAINT fk_products_categories         FOREIGN KEY (category_id)         REFERENCES categories(id)     SQL     add_column :users, :home_page_url, :string     rename_column :users, :email, :email_address   end     def down     rename_column :users, :email_address, :email     remove_column :users, :home_page_url     execute <<-SQL       ALTER TABLE products         DROP FOREIGN KEY fk_products_categories     SQL     drop_table :products   end end ROR Lab.13년 3월 30일 토요일
  10. 10. Change Method class CreateProducts < ActiveRecord::Migration   def change     create_table :products do |t|       t.string :name • create_table       t.text :description • add_column   • add_index       t.timestamps • rename_table     end • rename_column   end • rename_index end • add_timestamps • remove_timestamps ROR Lab.13년 3월 30일 토요일
  11. 11. Supported Types :binary :boolean :date :datetime :decimal :float • not supported by Active Record :integer :primary_key • t.column :name, typename :string :text :time :timestamp ROR Lab.13년 3월 30일 토요일
  12. 12. Migration Files Naming • db/migrate directory • YYYYMMDDHHMMSS_create_products.rb • UTC timestamp • The name of the migration class(CamelCased) ROR Lab.13년 3월 30일 토요일
  13. 13. To Create Migration Files • $ rails generate model Post title:string • $ rails generate scaffold Post title:string • $ rails generate migration AddContentToPosts content:text • $ rails generate migration RemoveContentFromPosts content:text ROR Lab.13년 3월 30일 토요일
  14. 14. Add & Remove Columns • AddXXXToYYY • RemoveXXXFromYYY $ rails g migration AddNameToUsers $ rails g migration RemoveNameFromUsers ROR Lab.13년 3월 30일 토요일
  15. 15. To Destroy Migration Files • $ rails destroy model Post title:string • $ rails destroy scaffold Post title:string • $ rails destroy migration AddContentToPosts content:text ROR Lab.13년 3월 30일 토요일
  16. 16. Rake it~ • $ rake db:migrate “up” state • $ rake db:rollback “down” state • $ rake db:migrate:status ROR Lab.13년 3월 30일 토요일
  17. 17. Rake it~ $ rake db:migrate:status mysql> select * from schema_migrations; +----------------+ | version | database: medibook_development +----------------+ | 20120531061820 | Status Migration ID Migration Name | 20120531105534 | | 20120531124444 | -------------------------------------------------- | 20120531125446 | up 20120531061820 Create users | 20120531133035 | up 20120531105534 Rolify create roles | 20120601102629 | up 20120531124444 Create boards | 20120603223525 | up 20120531125446 Create board types | 20120603224330 | up 20120531133035 Create posts | 20120603224625 | up 20120601102629 Create categories | 20120604064155 | | 20120604110743 | up 20120603223525 Create products | 20120702123904 | up 20120603224330 Add some fields to users | 20120702125358 | up 20120603224625 Add some fields to categories | 20120703005951 | up 20120604064155 Add category id to products | 20120704033651 | up 20120604110743 Add attachment product photo to products | 20120728014210 | up 20120702123904 Add view count to products | 20120728061841 | up 20120702125358 Create comments | 20120728102213 | | 20120729053924 | up 20120703005951 Create favourites | 20120804011723 | up 20120704033651 Add account to users | 20120804012821 | up 20120728014210 Add devise to users | 20120804013538 | up 20120728061841 Remove password digest from users | 20120808023400 | up 20120728102213 Create user groups | 20120810071351 | up 20120729053924 Create users groups +----------------+ up 20120804011723 Add user details to users 34 rows in set (0.00 sec) up 20120804012821 Create affiliations up 20120804013538 Add some fields to affiliations up 20120808023400 Add attachment company logo to affiliations up 20120810071351 Add active to users ROR Lab.13년 3월 30일 토요일
  18. 18. Rake it~ $ rake db:version Current version: 20120818023340 $ cat db/schema.rb ActiveRecord::Schema.define(:version => 20120831045454) do create_table "ad_images", :force => true do |t| t.integer "adimageable_id" t.string "adimageable_type" t.integer "user_id" t.string "image_file_name" t.string "image_content_type" t.integer "image_file_size" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end add_index "ad_images", ["adimageable_id"], :name => "index_ad_images_on_adimageable_id" add_index "ad_images", ["user_id"], :name => "index_ad_images_on_user_id" ROR Lab.13년 3월 30일 토요일
  19. 19. Rake it~ $ rake db:abort_if_pending_migrations $ rake db:migrate:status $ rake db:abort_if_pending_migrations database: db/development.sqlite3 You have 2 pending migrations: 20120818023340 CreateComments Status Migration ID Migration Name 20120818031154 CreateTags -------------------------------------------------- Run `rake db:migrate` to update your database then up 20120818022501 Create posts try again. down 20120818023340 Create comments down 20120818031154 Create tags ROR Lab.13년 3월 30일 토요일
  20. 20. schema_migrations • db/schema.rb • A database table : history of migrations $ rake db:migrate:status • up • down ROR Lab.13년 3월 30일 토요일
  21. 21. Running Migrations db/schema.rb db:schema:dump UP $ rake db:migrate $ rake db:migrate VERSION=20090906120000 $ rake db:migrate:up VERSION=20090906120000 DOWN $ rake db:rollback $ rake db:rollback STEP=3 ROR Lab.13년 3월 30일 토요일
  22. 22. Running Migrations database: medibook_development Status Migration ID -------------------------- UP up 20120531061820 up 20120531105534 up 20120531124444 up 20120531125446 $ rake db:migrate up 20120531133035 up 20120601102629 $ rake db:migrate VERSION=20120702125358 up 20120603223525 up 20120603224330 $ rake db:migrate:down VERSION=20120702125358 up 20120603224625 up 20120604064155 up 20120604110743 up 20120702123904 up 20120702125358 up 20120703005951 up 20120704033651 up 20120728014210 up 20120728061841 up 20120728102213 up 20120729053924 up 20120804011723 db:migrate:down up 20120804012821 up 20120804013538 up 20120808023400 up 20120810071351 ROR Lab.13년 3월 30일 토요일
  23. 23. Running Migrations database: medibook_development Status Migration ID -------------------------- UP up 20120531061820 up 20120531105534 up 20120531124444 up 20120531125446 $ rake db:migrate up 20120531133035 up 20120601102629 $ rake db:migrate VERSION=20120702125358 up 20120603223525 down 20120603224330 $ rake db:migrate:up VERSION=20120702125358 down 20120603224625 down 20120604064155 down 20120604110743 down 20120702123904 down 20120702125358 down 20120703005951 down 20120704033651 down 20120728014210 down 20120728061841 down 20120728102213 down 20120729053924 down 20120804011723 db:migrate:up down 20120804012821 down 20120804013538 down 20120808023400 down 20120810071351 ROR Lab.13년 3월 30일 토요일
  24. 24. Running Migrations database: medibook_development Status Migration ID -------------------------- UP up 20120531061820 up 20120531105534 $ rake db:rollback up 20120531124444 up 20120531125446 $ rake db:migrate:redo up 20120531133035 up 20120601102629 $ rake db:rollback STEP=3 up 20120603223525 up 20120603224330 $ rake db:migrate:redo STEP=3 up 20120603224625 up 20120604064155 up 20120604110743 up 20120702123904 up 20120702125358 up 20120703005951 up 20120704033651 up 20120728014210 up 20120728061841 up 20120728102213 up 20120729053924 up 20120804011723 up 20120804012821 up 20120804013538 up 20120808023400 up 20120810071351 ROR Lab.13년 3월 30일 토요일
  25. 25. output of migrations • suppress_messages • say • indent option true/false • say_with_time • return integer, number of rows affected ROR Lab.13년 3월 30일 토요일
  26. 26. class CreateProducts < ActiveRecord::Migration def change suppress_messages do create_table :products do |t| t.string :name end end say "Created a table" suppress_messages {add_index :products, :name} say "and an index!", true say_with_time Waiting for a while do sleep 10 250 end == CreateProducts: migrating ============ end -- Created a table end -> and an index! -- Waiting for a while -> 10.0013s -> 250 rows == CreateProducts: migrated (10.0054s) ==== ROR Lab.13년 3월 30일 토요일
  27. 27. Model in Migration • YourTable.reset_column_information # db/migrate/20100513121110_add_flag_to_product.rb   class AddFlagToProduct < ActiveRecord::Migration   class Product < ActiveRecord::Base   end     def change     add_column :products, :flag, :integer     Product.reset_column_information     Product.all.each do |product|       product.update_attributes!(:flag => false)     end   end end • Alice and Bob’s Story ROR Lab.13년 3월 30일 토요일
  28. 28. App DB Init using db/schema.rb $ rake db:setup $ rake db:reset 1. db:create 1. db:drop 2. db:schema:load 2. db:setup 3. db:seed https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake ROR Lab.13년 3월 30일 토요일
  29. 29. Schema works • $ rake db:schema:dump called by db:migrate create schema.rb • $ rake db:schema:load load schema.rb ROR Lab.13년 3월 30일 토요일
  30. 30. 감사합니다.
  31. 31.  13년 3월 30일 토요일
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×