Tag: マイグレーション
bundle exec rails generate migration create_books
class CreateHoges < ActiveRecord::Migration def change create_table :hoges do |t| t.string :name t.integer :age t.timestamps end end end
bundle exec rails generate migration add_image_to_books
class AddImageToBooks < ActiveRecord::Migration def change add_column :books, :image, :string end end
bundle exec rails generate migration change_title_type_of_books
class ChangeTitleTypeOfBooks < ActiveRecord::Migration def change change_column :books, :title, :text end end
class AddDateIndexToAccesses < ActiveRecord::Migration def change add_index :accesses, :date end end
class AddDescriptionToBooks < ActiveRecord::Migration def change add_column :books :description, :text, :limit => 4294967295 end end
bundle exec rails g model product name:string price:integer
bundle exec rails g scaffold product name:string price:integer
bundle exec rake db:migrate
bundle exec rake db:migrate RAILS_ENV=production
bundle exec rake db:rollback
bundle exec rake db:migrate:reset
bundle exec rake db:reset
現在の状態に基いてテーブルを作り直したい場合「db:reset」じゃないとだめ。手作業でテーブル定義を修正していてマイグレーションファイルが不完全な場合、db:resetじゃないとおかしなことになる。DBをリセットするrakeタスク二つ - maeshimaの日記
手順
bundle exec rake db:migrate:status
INSERT INTO schema_migrations (version) VALUES(20131031113857);
bundle exec rake db:schema:dump
$ bundle exec rake db:migrate:status Status Migration ID Migration Name -------------------------------------------------- up 20140927082920 Create books up 20140927084708 Create hoges up 20141121060148 Create software infos down 20150409052819 Create settings
$ bundle exec rake db:version Current version: 20141121060148
Current version: 20141208044134