Ruby on Rails 復習。
○データベースの作成(例:MySQL)
create database [データベース名] default character set utf8;
[なんか名前]_development
[なんか名前]_test
[なんか名前]_production
↑この3つのデータベースを作る。
※ユーザー権限設定が必要であれば行う。
例:
grant all privileges on *.* to user_name@localhost identified by 'passwd' with grant option;
○migration -> データベースにテーブルを作成できる。データベース構造のバージョン管理もできる。
具体的に出来ること
* テーブルの作成・削除
* カラムの追加・削除・変更
* インデックスの追加・削除
○対応しているデータベース
* MySQL
* PostgreSQL
* Oracle
* SQLite
・ファイルの場所
db/migrate/○○.rb
・migration のヘルプの見方
1.Railsアプリのあるディレクトリに移動する。
2.ruby script/generate migration --help
・migration ファイルの作り方(Railsのトップディレクトリで)
ruby script/generate migration create_[テーブル名]
↓
これで、 db/migrate/001_create_[テーブル名].rb というファイルができる。
テーブルひとつに対してひとつ。
- db/migrate/001_create_[テーブル名].rb - 修正前
{{{
class CreateBirthdayLists < ActiveRecord::Migration
def self.up
end
def self.down
end
end
]}}
- db/migrate/001_create_[テーブル名].rb - 修正後の例
{{{
class CreateBirthdayLists < ActiveRecord::Migration
def self.up
create_table(:birthday_lists) do |table|
table.column(:company_directory_no, :integer, :null => false)
table.column(:name, :string, :limit => '100', :null => false)
table.column(:birthday, :date)
end
end
def self.down
drop_table(:birthday_lists)
end
end
]}}
※idは自動的に作成される
・migration ファイルの説明
self.up : migration のバージョンがあがるときに実行される
sefl.down : migration のバージョンが下がるときに実行される
・rake db:migrate
migration ファイルを作成したら実行しましょう。テーブルが出来ます。
・参考
http://www.atmarkit.co.jp/im/carc/serial/proto04/proto04.html