目次
- マイグレーションとは
- データベースのカラムをバージョンアップ(up)
- データベースのカラムをバージョンダウン(down)
- データベースのカラムを変更(change)
- テーブルの作成(create_table)
- テーブル名を変更(rename_table)
- テーブルの削除(drop_table)
- テーブル定義を変更(change_table)
- テーブルを結合して作成(create_join_table)
- テーブルが存在するかチェック(table_exists?)
- join_tableを削除(drop_join_table)
- カラムの追加(add_column)
- カラム名の変更(rename_column)
- カラムの変更(change_column)
- カラムの削除(remove_column)
- 複数のカラムを削除(remove_columns)
- カラムの初期値を設定(change_column_default)
- カラムが存在するかチェック(column_exists?)
- インデックスの追加(add_index)
- インデックスの変更(rename_index)
- インデックスの削除(remove_index)
- インデックスが存在するかチェック(index_exists?)
- created_atとupdated_atを生成(timestamps)
- created_atとupdated_atを追加(add_timestamps)
- created_atとupdated_atの削除(remove_timestamps)
- リファレンスの生成(references)
- リファレンスの追加(add_reference)
- リファレンスの削除(remove_reference)
- マイグレーションファイルでSQLを実行(execute)
マイグレーション(migration)
マイグレーションとは
マイグレーションとは
直接SQLを使わずに、データベースのテーブルやカラムなどの構造を変更できる仕組み
特徴
- db/migrateディレクトリ以下にあるRubyで書かれたファイル
- ファイル名はタイムスタンプ(西暦、月、日、時、分、秒)とアンダースコア(_)で始まり、クラス名のすべての大文字を小文字にしたもの
- 別々のマイグレーションファイルに同じ名前のマイグレーションクラスを記述できない
- idという主キーを自動的に追加
- 実行するにはRakeタスク
- schema_infoテーブルに現在のバージョンが格納
簡単な例
class AddSsl < ActiveRecord::Migration
def up
add_column :accounts, :ssl_enabled, :boolean, default: true
end
def down
remove_column :accounts, :ssl_enabled
end
end
class TenderloveMigration < ActiveRecord::Migration
def change
create_table(:horses) do |t|
t.column :content, :text
t.column :remind_at, :datetime
end
end
end
利用可能なメソッド
- create_table
- drop_table
- change_table
- rename_table
- add_column
- rename_column
- change_column
- remove_column
- add_index
- remove_index
- remove_index
サポートしているデータベース
- MySQL
- PostgreSQL
- SQLite
- SQL Server
- Sybase
- Oracle
データベースのカラムをバージョンアップ(up)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
データベースのカラムをバージョンアップする。
Rails3.0以下のバージョンのself.upに相当するのは、changeメソッド。
Rails3.1以上で、upメソッドはロールバックできない処理をする際に主に使用する。
使い方
def up
end
例
pagesテーブルにnameカラムをstring型で作成
def up
create_table :pages do |t|
t.string :name
t.string :category
end
end
データベースのカラムをバージョンダウン(down)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
データベースのカラムをバージョンダウンする。
Rails3.0以下では、self.upとself.downをセットに記述していたが、Rails3.1ではchangeメソッドのみで両方の処理を実行する。
Rails3.1以上では、downメソッドはロールバックできない処理の際に主に使用する。
使い方
def down
end
例
基本的な使い方
def down
drop_table :pages
end
データベースのカラムを変更(change)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
データベースのカラムを変更する
使い方
change(カラム名, 型 [, オプション])
例
t.change(:name, :string, limit: 80)
t.change(:description, :text)
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 406
def change(column_name, type, options = {})
@base.change_column(@table_name, column_name, type, options)
endソースコード検索
テーブルの作成(create_table)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
テーブルを作成
使い方
create_table(テーブル名 [, オプション])
create_table テーブル名 [, オプション] do |t|
t.型 カラム名 [, カラムオプション]
end
オプション
| オプション | 説明 | デフォルト |
|---|---|---|
| :id | 主キーを自動生成 | true |
| :primary_key | 主キーのカラムの名前 | id |
| :options | テーブルオプション | |
| :temporary | 一時テーブルとして作成 | false |
| :force | テーブルを作成前に、既存のテーブルを削除 | false |
カラムオプション
| オプション | 説明 | デフォルト |
|---|---|---|
| :limit | カラムの桁数 | |
| :default | デフォルトの値 | |
| :null | nullを許可するか | true |
| :precision | 数値の桁数 | |
| :scale | 小数点以下の桁数 |
カラムの型
| データ方 | 説明 |
|---|---|
| string | 文字列 |
| text | 長い文字列 |
| integer | 整数 |
| float | 浮動小数 |
| decimal | 精度の高い小数 |
| datetime | 日時 |
| timestamp | より細かい日時 |
| time | 時間 |
| date | 日付 |
| binary | バイナリデータ |
| boolean | Boolean型 |
例
テーブルの作成
crate_table :products do |t|
t.string :name
end
空のカラムを禁止
create_tabe :products |t|
t.string :name, :null => false
end
文字コードを指定してテーブルを作成
create_table :suppliers, :options => 'ENGINE=InnoDB DEFAULT CHARSET=utf8'
主キーを指定してテーブルを作成
create_table(:objects, :primary_key => 'guid') do |t|
t.column :name, :string, :limit => 80
end
プライマリーキーの無いテーブルを作成
create_table(:categories_suppliers, :id => false) do |t|
t.column :category_id, :integer
t.column :supplier_id, :integer
end
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 158
def create_table(table_name, options = {})
td = table_definition
td.primary_key(options[:primary_key] || Base.get_primary_key(table_name.to_s.singularize)) unless options[:id] == false
yield td if block_given?
if options[:force] && table_exists?(table_name)
drop_table(table_name, options)
end
create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
create_sql << "#{quote_table_name(table_name)} ("
create_sql << td.to_sql
create_sql << ") #{options[:options]}"
execute create_sql
td.indexes.each_pair { |c,o| add_index table_name, c, o }
endソースコード検索
テーブル名を変更(rename_table)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
指定したテーブルの名前を変更する
使い方
rename_table(現在のテーブル名, 新しいテーブル名)
例
now_tableからnew_tableに変更
rename_table :now_table, :new_table
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 311
def rename_table(table_name, new_name)
raise NotImplementedError, "rename_table is not implemented"
endソースコード検索
テーブルの削除(drop_table)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
指定したテーブルを削除
使い方
drop_table(:テーブル名 [, オプション])
オプション
| オプション | 説明 | デフォルト |
|---|---|---|
| :id | 主キーを自動生成 | true |
| :primary_key | 主キーのカラムの名前 | id |
| :options | テーブルオプション | |
| :temporary | 一時テーブルとして作成 | false |
| :force | テーブルを作成前に、既存のテーブルを削除 | false |
例
productsテーブルを削除
drop_table :products
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 320
def drop_table(table_name, options = {})
execute "DROP TABLE #{quote_table_name(table_name)}"
endソースコード検索
テーブル定義を変更(change_table)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
テーブル定義を変更する
使い方
change_table(テーブル名 [, オプション]) do |t|
t.メソッド名 カラム名
end
オプション
| オプション | 説明 | デフォルト |
|---|---|---|
| :bulk | 変更内容を1つのALTER TABLEにまとめるか | false |
使用できるメソッド
| メソッド名 | 説明 |
|---|---|
| index | インデックス |
| change | カラムを変更 |
| change_default | カラムのデフォルト値を変更 |
| rename | カラムの名前を変更 |
| remove | カラムを削除 |
| remove_references | リファレンスの削除 |
| remove_index | インデックスの削除 |
| remove_timestamps | タイムスタンプの削除 |
例
pagesテーブルにtext型のmemoを追加、titleにインデックスを設定
change_table :pages do |t|
t.text :memo
t.index :title
end
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 298
def change_table(table_name, options = {})
if supports_bulk_alter? && options[:bulk]
recorder = ActiveRecord::Migration::CommandRecorder.new(self)
yield Table.new(table_name, recorder)
bulk_change_table(table_name, recorder.commands)
else
yield Table.new(table_name, self)
end
endソースコード検索
テーブルを結合して作成(create_join_table)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
2つのテーブルを結合して新しいテーブルを作成する
使い方
create_join_table(テーブル1, テーブル2 [, オプション]) do |td|
end
| オプション | 説明 |
|---|---|
| :table_name | テーブルの名前 |
| :column_options | カラムのオプション |
| :options | オプション |
| :temporary | 一時テーブルとして作成 |
| :force | テーブルを作成前に、既存のテーブルを削除 |
例
create_join_table(:assemblies, :parts, options: 'ENGINE=InnoDB DEFAULT CHARSET=utf8')
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 210
def create_join_table(table_1, table_2, options = {})
join_table_name = find_join_table_name(table_1, table_2, options)
column_options = options.delete(:column_options) || {}
column_options.reverse_merge!(null: false)
t1_column, t2_column = [table_1, table_2].map{ |t| t.to_s.singularize.foreign_key }
create_table(join_table_name, options.merge!(id: false)) do |td|
td.integer t1_column, column_options
td.integer t2_column, column_options
yield td if block_given?
end
endソースコード検索
テーブルが存在するかチェック(table_exists?)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
テーブルが存在するかチェックする
使い方
table_exists?(テーブル名)
例
table_exists?(:developers)
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 23
def table_exists?(table_name)
tables.include?(table_name.to_s)
endソースコード検索
join_tableを削除(drop_join_table)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
join_tableを削除する
使い方
drop_join_table(テーブル1, テーブル2 [, オプション])
| オプション | 説明 |
|---|---|
| :table_name | テーブルの名前 |
| :column_options | カラムのオプション |
| :options | オプション |
| :temporary | 一時テーブルとして作成 |
| :force | テーブルを作成前に、既存のテーブルを削除 |
例
drop_join_table(:assemblies, :parts, options: 'ENGINE=InnoDB DEFAULT CHARSET=utf8')
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 231
def drop_join_table(table_1, table_2, options = {})
join_table_name = find_join_table_name(table_1, table_2, options)
drop_table(join_table_name)
endソースコード検索
カラムの追加(add_column)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
指定したテーブルにカラムを追加
使い方
add_column(テーブル名 カラム名, タイプ [, オプション])
change_table テーブル名 do |t|
t.タイプ カラム名 [, オプション]
end
オプション
| オプション | 説明 | デフォルト |
|---|---|---|
| :limit | カラムの桁数を指定 | |
| :default | デフォルト値を指定 | |
| :null | nill値を許可するか | true |
| :precision | :decimal 型の精度を指定 | |
| :scale | :decimal 型の小数点以下の桁数 |
precisionとscaleについては、データベースによって差があるので注意が必要
例
postsテーブルにtitleカラムを作成
def self.up
add_column :posts, :title, :string
end
カラムの桁数が10桁
def self.up
add_column :posts, :title, :string, :limit => 10
end
null値を許可しない
def self.up
add_column :posts, :title, :string, :null => false
end
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 326
def add_column(table_name, column_name, type, options = {})
add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
add_column_options!(add_column_sql, options)
execute(add_column_sql)
endソースコード検索
カラム名の変更(rename_column)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
指定したテーブルのカラム名を変更
名前を変更しても、そのカラムに関連付けられている既存のデータは破棄されない
使い方
rename_column(テーブル名, 変更するカラム名, 新しいカラム名)
change_table テーブル名 do |t|
t.rename 変更するカラム名, 新しいカラム名
end
例
usersテーブルのe_mailカラムをmailに変更
class RenameEmailColumn < ActiveRecord::Migration
def self.up
rename_column :users, :e_mail, :mail
end
def self.down
rename_column :users, :mail, :e_mail
end
end
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 374
def rename_column(table_name, column_name, new_column_name)
raise NotImplementedError, "rename_column is not implemented"
endソースコード検索
カラムの変更(change_column)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
既存のカラムの定義を変更
使い方
カラムの変更
change_column(テーブル名, カラム名, データ型 [, オプション])
カラムのデフォルト値の変更
change_column_default(テーブル名, カラム名, デフォルト値)
オプション
| オプション | 説明 |
|---|---|
| :limit | カラムの桁数を指定 |
| :default | デフォルト値を指定 |
| :null | nill値を許可するか |
| :precision | :decimal 型の精度を指定 |
| :scale | :decimal 型の小数点以下の桁数 |
例
usersテーブルのnameカラムをtext型に変更
change_column(:users, :name, :text)
文字数の最大を80に変更
change_column(:users, :name, :string, :limit => 80)
null側を許可しないように変更
change_column(:users, :name, :string, :null => dalse)
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 358
def change_column(table_name, column_name, type, options = {})
raise NotImplementedError, "change_column is not implemented"
endソースコード検索
カラムの削除(remove_column)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
指定したテーブルのカラムを削除
使い方
remove_column(テーブル名, カラム名 [, 型, オプション])
オプション
| オプション | 説明 | デフォルト |
|---|---|---|
| :limit | カラムの桁数を指定 | |
| :default | デフォルト値を指定 | |
| :null | nill値を許可するか | true |
| :precision | :decimal 型の精度を指定 | |
| :scale | :decimal 型の小数点以下の桁数 |
例
usersテーブルのdescriptionカラムを削除
remove_column(:users, :description)
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 349
def remove_column(table_name, column_name, type = nil, options = {})
execute "ALTER TABLE #{quote_table_name(table_name)} DROP #{quote_column_name(column_name)}"
endソースコード検索
複数のカラムを削除(remove_columns)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
指定したテーブルの複数のカラムを削除
使い方
remove_columns(テーブル名, カラム名 [, ...])
オプション
| オプション | 説明 | デフォルト |
|---|---|---|
| :limit | カラムの桁数を指定 | |
| :default | デフォルト値を指定 | |
| :null | nill値を許可するか | true |
| :precision | :decimal 型の精度を指定 | |
| :scale | :decimal 型の小数点以下の桁数 |
例
remove_columns(:suppliers, :qualification, :experience)
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 335
def remove_columns(table_name, *column_names)
raise ArgumentError.new("You must specify at least one column name. Example: remove_columns(:people, :first_name)") if column_names.empty?
column_names.each do |column_name|
remove_column(table_name, column_name)
end
endソースコード検索
カラムの初期値を設定(change_column_default)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
カラムの初期値を設定する
使い方
change_column_default(テーブル名, カラム名, 初期値)
例
change_column_default(:suppliers, :qualification, 'new')
change_column_default(:accounts, :authorized, 1)
change_column_default(:users, :email, nil)
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 367
def change_column_default(table_name, column_name, default)
raise NotImplementedError, "change_column_default is not implemented"
endソースコード検索
カラムが存在するかチェック(column_exists?)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
指定したテーブルにカラムが存在するかチェックする
使い方
column_exists?(テーブル名, カラム名 [, 型, オプション])
オプション
| オプション | 説明 | デフォルト |
|---|---|---|
| :id | 主キーを自動生成 | true |
| :primary_key | 主キーのカラムの名前 | id |
| :options | テーブルオプション | |
| :temporary | 一時テーブルとして作成 | false |
| :force | テーブルを作成前に、既存のテーブルを削除 | false |
例
pagesテーブルのtitleカラムにインデックスが存在するか
column_exists? :pages, :title
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 70
def column_exists?(table_name, column_name, type = nil, options = {})
columns(table_name).any?{ |c| c.name == column_name.to_s &&
(!type || c.type == type) &&
(!options.key?(:limit) || c.limit == options[:limit]) &&
(!options.key?(:precision) || c.precision == options[:precision]) &&
(!options.key?(:scale) || c.scale == options[:scale]) &&
(!options.key?(:default) || c.default == options[:default]) &&
(!options.key?(:null) || c.null == options[:null]) }
endソースコード検索
インデックスの追加(add_index)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
指定したテーブルにインデックスを追加
使い方
add_index(テーブル名, インデックスを付与するカラム名 [, オプション])
change_tabe テーブル名 do |t|
t.index ンデックスを付与するフィールド名 [, オプション]
end
オプション
| オプション | 説明 |
|---|---|
| :name | インデックスの名前 |
| :unique | trueを指定するとユニークなインデックス |
| :length | インデックスに含まれるカラムの長さ |
例
usersテーブルのnameカラムのインデックスを生成
add_index :users, :name
ユニークなインデックスを生成
add_index :users, [:name, :employee_id], :unique => true
インデックス名をつけて生成
add_index :users, [:name, :employee_id], :unique => true, :name => 'by_branch_name'
インデックスの長さを10に指定して生成
add_index :users, :name, :name => 'by_name', :length => 10
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 424
def add_index(table_name, column_name, options = {})
index_name, index_type, index_columns, index_options = add_index_options(table_name, column_name, options)
execute "CREATE #{index_type} INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)} (#{index_columns})#{index_options}"
endソースコード検索
インデックスの変更(rename_index)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
指定したテーブルのインデックスを変更する
使い方
rename_index(テーブル名, 変更前の名前, 変更後の名前)
例
rename_index :people, 'index_people_on_last_name', 'index_users_on_last_name'
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 451
def rename_index(table_name, old_name, new_name)
# this is a naive implementation; some DBs may support this more efficiently (Postgres, for instance)
old_index_def = indexes(table_name).detect { |i| i.name == old_name }
return unless old_index_def
remove_index(table_name, :name => old_name)
add_index(table_name, old_index_def.columns, :name => new_name, :unique => old_index_def.unique)
endソースコード検索
インデックスの削除(remove_index)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
指定したテーブルのインデックスを削除
使い方
remove_index(テーブル名 [, オプション])
change_table テーブル名 do |t|
t.remove_index インデックス名
end
オプション
| オプション | 説明 |
|---|---|
| :name => インデックス名 | インデックス名 |
| :column => インデックスを構成するカラム名 | カラム名 |
例
usersテーブルのnameカラムのインディックスを削除
remove_index :users, :name
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 439
def remove_index(table_name, options = {})
remove_index!(table_name, index_name_for_remove(table_name, options))
endソースコード検索
インデックスが存在するかチェック(index_exists?)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
指定したテーブルにインデックスが存在するかチェックする
使い方
index_exists?(テーブル名, カラム名 [, オプション])
オプション
| オプション | 説明 |
|---|---|
| :name | インデックスの名前 |
| :unique | trueを指定するとユニークなインデックス |
| :length | インデックスに含まれるカラムの長さ |
例
pagesテーブルのtitleカラムにインデックスが存在するか
index_exists? :pages, :title
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 43
def index_exists?(table_name, column_name, options = {})
column_names = Array(column_name)
index_name = options.key?(:name) ? options[:name].to_s : index_name(table_name, :column => column_names)
if options[:unique]
indexes(table_name).any?{ |i| i.unique && i.name == index_name }
else
indexes(table_name).any?{ |i| i.name == index_name }
end
endソースコード検索
created_atとupdated_atを生成(timestamps)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
created_atとupdated_atを両方追加するメソッド
使い方
timestamps
例
usersテーブルにtimestampsを設定
create_table :users do |t|
t.timestamps
end
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 397
def timestamps
@base.add_timestamps(@table_name)
endソースコード検索
created_atとupdated_atを追加(add_timestamps)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
既存のテーブルにcreated_atとupdated_atを追加する
使い方
add_timestamps(テーブル名)
例
usersテーブルにcreated_atとupdated_atを追加
add_timestamps(:users)
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 610
def add_timestamps(table_name)
add_column table_name, :created_at, :datetime
add_column table_name, :updated_at, :datetime
endソースコード検索
created_atとupdated_atの削除(remove_timestamps)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
既存のテーブルのcreated_atとupdated_atを削除する
使い方
remove_timestamps(テーブル名)
例
usersテーブルのcreated_atとupdated_atを削除
remove_timestamps(:users)
ソースコード検索
リファレンスの生成(references)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
他のテーブルへの外部キーを表すカラムを生成
使い方
references
例
belongs_to :user_idと同じ
create_table :blogs do |t|
t.references("user")
end
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 460
def references(*args)
options = args.extract_options!
args.each do |ref_name|
@base.add_reference(@table_name, ref_name, options)
end
endソースコード検索
リファレンスの追加(add_reference)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
既存のテーブルにリファレンスを追加する
使い方
add_reference(テーブル名, リファレンス名 [, オプション])
オプション
| オプション | 説明 |
|---|---|
| :polymorphic | ポリモーフィックを付与 |
| :index | インデックスを付与 |
例
オプションなし
add_reference(:products, :user)
オプションあり
add_reference(:products, :supplier, polymorphic: true, index: true)
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 495
def add_reference(table_name, ref_name, options = {})
polymorphic = options.delete(:polymorphic)
index_options = options.delete(:index)
add_column(table_name, "#{ref_name}_id", :integer, options)
add_column(table_name, "#{ref_name}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
add_index(table_name, polymorphic ? %w[id type].map{ |t| "#{ref_name}_#{t}" } : "#{ref_name}_id", index_options.is_a?(Hash) ? index_options : nil) if index_options
endソースコード検索
リファレンスの削除(remove_reference)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
既存のテーブルのリファレンスを削除する
使い方
remove_reference(テーブル名, リファレンス名 [, オプション])
オプション
| オプション | 説明 |
|---|---|
| :polymorphic | ポリモーフィックを付与 |
| :index | インデックスを付与 |
例
オプションなし
remove_reference(:products, :user, index: true)
オプションあり
remove_reference(:products, :supplier, polymorphic: true)
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 513
def remove_reference(table_name, ref_name, options = {})
remove_column(table_name, "#{ref_name}_id")
remove_column(table_name, "#{ref_name}_type") if options[:polymorphic]
endソースコード検索
マイグレーションファイルでSQLを実行(execute)
適応バージョン
- 1.0.0
- 1.1.0
- 1.1.1
- 1.1.6
- 1.2.0
- 1.2.6
- 2.0.0
- 2.0.1
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.2
- 2.3.8
- 3.0.0
- 3.0.5
- 3.0.7
- 3.0.9
- 3.1.0
- 3.2.3
- 3.2.8
- 3.2.13
- 4.0.0
説明
マイグレーションファイルでSQLを実行する
使い方
execute SQL文
例
pagesテーブルの最新の10件を取得
execute "SELECT * FROM pages ORDER BY updated_at DESC LIMIT 10"
ソースコード
# File /usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/mysql2_adapter.rb, line 206
def execute(sql, name = nil)
# make sure we carry over any changes to ActiveRecord::Base.default_timezone that have been
# made since we established the connection
@connection.query_options[:database_timezone] = ActiveRecord::Base.default_timezone
super
end